Found many topics about this problem, but didn't find any easy described problem solving, so creating new topic with this problem and maybe will get much better answer to this problem.
I have created test case for LoginTest and test case for LegalPersonSearchTest. Both these test cases are in separate files login.py and legalPersonSearch.py
from utils.utils import *
from selenium import webdriver
from locators.locators import Locators
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import unittest
from pages.loginPage import LoginPage
class LoginTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
cls.driver.implicitly_wait(10)
cls.driver.maximize_window()
cls.driver.get(URL)
def test_valid_login(self):
driver = self.driver
login = LoginPage(driver)
login.fill_login_form(adminUsername, adminPassword)
login.click_login_button()
if __name__ == '__main__':
unittest.main()
I think the reason why new browser opens between tests, is declaring driver in second file.
from selenium import webdriver
import unittest
from pages.legalPersonSearchPage import LegalPersonSearchPage
class LegalPersonSearchTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
def test_valid_organization_search(self):
driver = self.driver
legal = LegalPersonSearchPage(driver)
legal.open_legal_person_search_page()
@classmethod
def tearDown(cls):
cls.driver.quit()
if __name__ == '__main__':
unittest.main()
How to modify LoginTest that browser open only one time, instead between tests?
I need to modify it, because after login (which is 1/2 of test) comes second part of test. It will complete and then browser closes and open new and start again with login.
As someone told me, then best practice is to keep login test part out of whole test.
To answer one of your questions --
drivergets opened twice because you have two differentsetUp()methods, one for each of your classes -LoginTestandLegalPersonSearchTest. When you runtest_valid_login, yoursetUpClass()method gets called, which initializes thedriver. Then, test runs. Thisdriverinstance never gets torn down, so window remains open.Then,
test_valid_organization_searchruns. This class ALSO has its ownsetUpClass()method, which initializes anotherdriverinstance. So now you have two drivers, both of which are opened in thesetUpClass()method of each class file.This set up is producing results that you have not intended. I think you are slightly misunderstanding what "best practice is to keep login test part out of whole test" means here.
This does not mean you need to write two separate tests -- one that logs in, and one that tests the rest of the functionality. This is actually bad practice, because you want your test cases to run independently of one another -- test cases should not rely on previous test cases to execute properly.
I think the solution you are looking for here is to write the login functionality into your
setUp()method so that login is always performed before a test case starts.This approach will prevent you from having the login part in with the test -- that's the original goal you mentioned here.
You can adjust your classes by removing the duplicate
setUp()method, and inheritingLoginTestinto yourLegalPersonSearchTestso that you are only setting up once:We have declared a class variable for
driverthat you can pass intoLegalPersonSearchTest. We have also removed the__main__call, because the only entry point we need is thetest_method inLegalPersonSearchTest:We have changed a few things here:
__main__-- we only need this in one place, on the test case level (LegalPersonSearchTest)setUp()method calls that were resulting in multiple driver instances getting created (mentioned in your problem description)LoginTestclass to be passed intoLegalPersonSearchTestclasssetUp()method onLoginTestto initialize the driver and login -- so that you do not have to include login code for any test!Update: Changes made following comments:
loginmethod fromLoginTestclass and moved login code undersetUp()-- this will get performed before every test case nowtest_valid_organization_searchto useself.driverinstead ofdriverwhen initializing PageObjectI've made many of these changes based on this sample Python test architecture that I pushed to my GitHub. The files I used as templates were
base_test_fixture.py(for yourLoginTest), andtest_web_driver.pyfor yourLegalPersonSearchTest.You may notice that
base_test_fixture.pyalso containsdef _testInitialize()anddef _testCleanup()-- these are methods that you can override in yourLegalPersonSearchTestclass to perform additional steps to be run before yourtest_method. The idea here is that if you have multiple.pyfiles containing test cases, but each.pyfile requires slightly different setup, and you need to do more than justsetUp()method calls for. You can override thetestInitialize()andtestCleanup()methods in the test case class, and these methods will execute aftersetUp()and before the actualtest_method.Let me know if you have any questions about this -- it's quite a bit to explain, but once you learn this architecture, you have a very powerful way of organizing and executing your test cases.