Skip to content Skip to sidebar Skip to footer

Python + Webdriver -- No Browser Launched While Using Unittest Module

Could you please help me with the next. I found out the issue and could not resolve it. When I am using next code, the browser has started and the test has passed: import unittest

Solution 1:

While working through Python's unittest module with Selenium you have to consider a few facts as follows :

  • While you pass the Keyexecutable_path provide the Value through single quotes along with the raw r switch.
  • While you define the @Tests name the tests starting with test e.g. def test_NoLorem(self):
  • While you invoke get() ensure you are passing a valid url e.g. http://www.python.org
  • While you invoke the quit() method within def tearDown(self): invoke the method through the WebDriver instance as self.driver.quit().
  • If you are using unittest module you have to call the Tests through if __name__ == "__main__":
  • Here is your own code with the required minor modifications :

    import unittest
    from selenium import webdriver
    
    classGlossaryPage(unittest.TestCase):
    
        defsetUp(self):
            self.driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
            self.driver.maximize_window()
            self.driver.implicitly_wait(10)
        deftest_NoLorem(self):
            driver = self.driver
            driver.get("http://www.python.org")
        deftearDown(self):
            self.driver.quit()
    
    if __name__ == "__main__":
        unittest.main()
    

Solution 2:

I'm not sure as I haven't tested it but I suspect the browser is still opened from the first code. So what I suggest here is to use driver.close() to terminate the process properly once you get out of script.

import unittest
from selenium import webdriver
driver = webdriver.Chrome('D:\chromedriver\chromedriver.exe')
driver.get("site URL")
driver.close()  # Must be there

Similarly, you can make modification in the test script to put self.driver.close() in the tearDown method.

Solution 3:

In unittest, you have to put the tested code in method called

test_<custom_text_here>

Moreover I believe you wanted to quit the driver and not unittest? Try to replace the line

unittest.quit()

With

self.driver.close()

Post a Comment for "Python + Webdriver -- No Browser Launched While Using Unittest Module"