Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context fixture #44

Closed
wpeterw opened this issue Mar 3, 2021 · 5 comments
Closed

context fixture #44

wpeterw opened this issue Mar 3, 2021 · 5 comments
Labels
question Further information is requested

Comments

@wpeterw
Copy link

wpeterw commented Mar 3, 2021

Hi,

I'm looking for an example on how to use the context fixture. My goal is to parametrize a test with different devices. Is this possible with the pytest-playwright plugin ?

@pluckhuang
Copy link

Hi,

I'm looking for an example on how to use the context fixture. My goal is to parametrize a test with different devices. Is this possible with the pytest-playwright plugin ?

i know pytest has context fixture,maybe it is you want

@wpeterw
Copy link
Author

wpeterw commented Mar 3, 2021

Thanks but I'm looking for an example om how to use this:

@hampsterx
Copy link

Function scope: These fixtures are created when requested in a test function and destroyed when the test ends.

context: New browser context for a test.
page: New browser page for a test.

yep its easier to manage this yourself.

If you look at that fixture..

@pytest.fixture
def context(
    browser: Browser, browser_context_args: Dict
) -> Generator[BrowserContext, None, None]:
    context = browser.new_context(**browser_context_args)
    yield context
    context.close()

pretty simple, just need to close context when done so if creating a bunch for a test then need to keep track of em.

This is what I am doing for example..


@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
    return {
        **browser_context_args,
        "viewport": {"width": 1440, "height": 900},
    }


@pytest.fixture(scope="session")
def browser_type_launch_args(browser_type_launch_args):
    return {
        **browser_type_launch_args,
        "slow_mo": 50
    }


@pytest.fixture(scope="function")
def browser_context(request, browser, browser_context_args):
    _contexts = []

    def f(viewport=None):
        args = copy.copy(browser_context_args)
        if viewport:
            args['viewport'] = viewport

        log.debug("Create new context", extra=args)
        _contexts.append(browser.new_context(**args))
        return _contexts[-1]

    def teardown():
        for context in _contexts:
            context.close()

    request.addfinalizer(teardown)
    return f


@pytest.fixture(scope="function")
def desktop_context(browser, browser_context):
    def f():
        return browser_context(viewport={"width": 920, "height": 800})

    return f

hope that helps :)

@wpeterw
Copy link
Author

wpeterw commented Mar 5, 2021

Thanks for the example. Where do you create the browser ? In conftest.py ?

@mrudulp
Copy link

mrudulp commented Mar 11, 2021

you can import Browser Fixture which has session scope, so you dont need to create Browser so to say

@mxschmitt mxschmitt added the question Further information is requested label Mar 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants