I
I
Ivan Petrov2017-01-27 19:33:11
Python
Ivan Petrov, 2017-01-27 19:33:11

How to competently make a page object + pytest so as not to pass a fixture to each page class?

I want to create the basis for the site that will be tested. I use Page objext pattern.
There is a main fixture in which a class is created to work with webdriver.
I want to create a base Page class from which other pages can inherit. Each page object should have the ability to work with the webdriver, for this you need to pass a browser fixture to the constructor. Accordingly, when creating heirs, they also need to pass this fixture to the constructor.
Is it normal or not? It bothers me a little that the fixture must be passed to each test function, and then to the page classes when creating an object.
conftest file, main fixture

import pytest
from app.browser import App


@pytest.fixture(scope="session")
def app(request):
    # TODO add authorization check
    app = App()
    request.addfinalizer(app.destroy)
    return app

Fixture class
from selenium import webdriver
from config import *


class App:

    def __init__(self):
        if browser == "firefox":
            self.wd = webdriver.Firefox()
        elif browser == "chrome":
            self.wd = webdriver.Chrome()
        elif browser == "ie":
            self.wd = webdriver.Ie()
        self.wd.implicitly_wait(5)

    def destroy(self):
        self.wd.quit()

base class Page. app is a fixture - browser
class Page():

    def __init__(self, app):
        self.wd = app.wd

test example:
from pages.index import Index

def test_open_page(app):
    app.wd.get('https://www.restaurantsupply.com')
    index = Index(app)
    assert 'Restaurant Supplies' in index.wd.title

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question