Python + WebDriver — No browser launched while using unittest module

后端 未结 3 2191
孤街浪徒
孤街浪徒 2021-01-24 20:51

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:



        
3条回答
  •  天命终不由人
    2021-01-24 21:37

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

    • While you pass the Key executable_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
      
      class GlossaryPage(unittest.TestCase):
      
          def setUp(self):
              self.driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
              self.driver.maximize_window()
              self.driver.implicitly_wait(10)
          def test_NoLorem(self):
              driver = self.driver
              driver.get("http://www.python.org")
          def tearDown(self):
              self.driver.quit()
      
      if __name__ == "__main__":
          unittest.main()
      

提交回复
热议问题