I am currently testing a web app using pytest and Selenium. All pages have "Home" and "Log Out" links, so I have written a test like this:
def test_can_log_out(page):
link = page.find_element_by_partial_link_text('Log Out')
link.click()
assert 'YOU HAVE SUCCESSFULLY LOGGED OFF!' in starting_page.page_source
Now for the page fixture, I am simulating the login process. I have this broken into several fixtures:
Get the Selenium WebDriver instances
@pytest.fixture() def browser(request, data, headless): b = webdriver.Firefox(executable_path=DRIVERS_PATH + '/geckodriver') yield b b.quit()Log in to the web app
@pytest.fixture() def login(browser): browser.get('http://example.com/login) user_name = browser.find_element_by_name('user_name') user_name.send_keys('codeapprentice') password = browser.find_element_by_name('password') password.send_keys('password1234') submit = browser.find_element_by_name('submit') submit.click() return browserVisit a page
@pytest.fixture() def page(login): link = login.find_element_by_partial_link_text('Sub Page A') link.click() return login
This works very well and I can test logging out from this page. Now my question is that I have another page which can be visited from "Page A":
@pytest.fixture()
def subpage(page):
button = login.find_element_name('button')
button.click()
return page
Now I want to run the exact same test with this fixture, also. Of course, I can copy/paste and make a few changes:
def test_can_log_out_subpage(subpage):
link = page.find_element_by_partial_link_text('Log Out')
link.click()
assert 'YOU HAVE SUCCESSFULLY LOGGED OFF!' in starting_page.page_source
However, this violates the DRY principle. How can I reuse test_can_log_out() without this repetition?