vendredi 22 septembre 2017

Run a test with two different pytest fixtures

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:

  1. Get the Selenium WebDriver instances

    @pytest.fixture()
    def browser(request, data, headless):
        b = webdriver.Firefox(executable_path=DRIVERS_PATH + '/geckodriver')
        yield b
        b.quit()
    
    
  2. Log in to the web app

    @pytest.fixture()
    def login(browser):
        browser.get('http://ift.tt/1dQYKGD)
        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 browser
    
    
  3. Visit 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

I want to reuse test_can_log_out() with this second fixture since the test is identical. How can I do so?

Aucun commentaire:

Enregistrer un commentaire