0

I have following code here:

import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

class MyTest1(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()
        driver = cls.driver
        driver.get("https://somewebsite.com")
        print "login the website"

    def test_UI_login(self):
        driver = self.driver
        print "test some things here"

    def test_duplicate_client(self):
        driver = self.driver
        print "test some things here"


    def tearDown(cls):
        cls.driver.close()


if __name__ == '__main__':
    unittest.main()

Problem I am facing is , After first function test_UI_login, the firefox instance closes. How can I execute multiple test cases from the same Firefox instance in unittest using selenium.

2
  • Perhaps it would help if you also annotate tearDown with @classmethod? Commented Mar 21, 2016 at 8:49
  • It didn't work out if we annotate tearDown with @classmethod Commented Mar 21, 2016 at 9:30

2 Answers 2

0

As stated in this SO-answer

you must initialize the driver in __init__ .

Sign up to request clarification or add additional context in comments.

4 Comments

I tried as you suggested. But,I am getting below error, typeerror __init__() takes 1 positional argument but 2 were given class MyTest1(unittest.TestCase): def __init__(self): self.driver = webdriver.Firefox() driver = self.driver
You must declare __init__(cls, tests) Sorry for the misunderstanding, it's been a while since I used it. It looks like selenium adds an additional parameter in the latest versions.
Tried the method as suggested you(def __init__(cls, tests)), noticed below error 'MyTest1' object has no attribute '_testMethodName'
Oh, sorry. Forgot to mention that you have to implicitly call the superclasses init if you overwrite it. So adding ` unittest.TestCase.__init__(cls, tests)` as the first line in your init should work (At least it does for me) :-).
0

Assign the webdriver in __init__(), but don't forget to call super.__init__() to not break the tests mechanism:

    def __init__(self, *args):
        super().__init__(*args)
        self.driver = webdriver.Firefox()

Comments

Your Answer

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.