1

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
from selenium import webdriver
driver = webdriver.Chrome('D:\chromedriver\chromedriver.exe')
driver.get("site URL")

BUT same with class and methods return message: "Process finished with exit code 0":

import unittest
from selenium import webdriver
class GlossaryPage(unittest.TestCase):
 def setUp(self):
 self.driver = webdriver.Chrome(executable_path='D:\chromedriver\chromedriver.exe')
 self.driver.maximize_window()
 self.driver.implicitly_wait(10)
 def NoLorem(self):
 driver = self.driver
 driver.get("site URL")
 def tearDown(self):
 unittest.quit()

How can I get the browser opened using 2nd case (with methods and class)?

Thanks a lot for any help.

Ratmir Asanov
6,4795 gold badges30 silver badges44 bronze badges
asked Apr 16, 2018 at 14:09
3
  • For me chromeOperator = GlossaryPage() and chromeOperator.setUp() works just fine. Commented Apr 16, 2018 at 14:18
  • if you repalce setUp by __init__ then, when creating an instance of GlossaryPage it will open a browser Commented Apr 16, 2018 at 14:21
  • no it's unittest specific Commented Apr 16, 2018 at 14:27

3 Answers 3

2

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()
    
Corey Goldberg
61.4k30 gold badges135 silver badges147 bronze badges
answered Apr 16, 2018 at 14:40
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Apr 16, 2018 at 14:16

3 Comments

Check the link, probably it will help
Help with what? I have no problem :)
Thought its OP who replied here. Never mind ignore my comments. and yes you are correct it will just keep open one at a time.
0

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()
answered Apr 16, 2018 at 14:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.