Minh-Thanh Hoang
Minh-Thanh Hoang

Reputation: 556

quit method in python unittests raises ConnectionResetError using Selenium

I have a class for Integration Test using Selenium for Chrome Webdriver like this

LoginTest.py
class LoginTest(StaticLiveServerTestCase):
    browser = CommonTest.set_up_webdriver()
    wait = WebDriverWait(browser, 20)

    @classmethod
    def setUpClass(cls):
        super(LoginTest, cls).setUpClass()
        cls.browser.maximize_window()
        cls.browser_url = cls.live_server_url
        cls.browser.get(cls.browser_url)

    @classmethod
    def tearDownClass(cls):
        super(LoginTest, cls).tearDownClass()
        time.sleep(3)
        cls.browser.quit() # Error here

    def setUp(self):
        super(LoginTest, self).setUp()
        ...

    def tearDown(self):
        super(LoginTest, self).tearDown()
CommonTest.py
class CommonTest:
    @staticmethod
    def set_up_webdriver():
        options = webdriver.chrome.options.Options()
        options.add_argument("--disable-dev-shm-usage")
        options.add_argument("--no-sandbox")

        return webdriver.Chrome(executable_path=Environment.CHROME_DRIVER_WINDOWS_PATH, options=options,
                                    service_log_path=ConstTest.TEST_LOG_PATH)

When I remove line cls.browser.quit() my Test working OK. But It doesn't close Chrome Browser. If I add line cls.browser.quit() It will get error:

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

I have search for error ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host many time and add many solution but It's still got that error

Upvotes: 0

Views: 141

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193108

When using as per the current implementation the service will be stoped once your script ends as, subprocess.Popen doesn't send signal on __del__, so the attempt is to close the launched process when __del__ is triggered.

def __del__(self):
    # `subprocess.Popen` doesn't send signal on `__del__`;
    # so we attempt to close the launched process when `__del__`
    # is triggered.
    try:
        self.stop()
    except Exception:
        pass

Upvotes: 1

Related Questions