mercredi 4 janvier 2017

How to run test cases of multiple scripts in main script using single browser instance in python selenium webdriver?

I am new to selenium webdriver unit testing. I just want to run the multiple test classes written in different scipts in to one main script. I have following files(scripts) within single directory.

main_base_test_case.py

#import statements
...

class MainBaseTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()
        cls.driver.maximize_window()
        cls.driver.implicitly_wait(30)
        ...
        ...

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()

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

sign_in_test_case.py

#import statements
...

class LoginTest(MainBaseTestCase):
    def test_login(self):
        driver = self.driver
        ...
        ...

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

add_to_cart_test_case.py

#import statements
...

class AddToCartTest(MainBaseTestCase):
    def test_add_to_cart(self):
        driver = self.driver
        ...
        ...

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

fill_shipping_billing_info.py

#import statements
...

class FillingDetailsTest(MainBaseTestCase):
    def test_filling_details(self):
        driver = self.driver
        ...
        ...

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

Now i want to call above test classes in following main_test.py file. here i have written some code but its not working. it runs test cases not in proper order like login, add_to_cart to filling_details. and it opens new browser for every test classes. here is my file:

main_test.py

import unittest
from sign_up_test_case import SignUpTest
from sign_in_test_case import LoginTest
from add_to_cart_test_case import AddToCartTest
from fill_shipping_billing_info import FillingDetailsTest

def test_suite():
    test_suite = unittest.TestSuite()
    test_suite.addTest(LoginTest("test_login"))
    test_suite.addTest(AddToCartTest("test_add_to_cart"))
    test_suite.addTest(FillingDetailsTest("test_filling_details"))
    return test_suite  

if __name__ == "__main__":
    unittest.TextTestRunner().run(test_suite())

So please help me to sort out this issue. How do i call those classes into main file? Thank you.

Aucun commentaire:

Enregistrer un commentaire