Giter Site home page Giter Site logo

context fixture about playwright-pytest HOT 5 CLOSED

microsoft avatar microsoft commented on May 16, 2024
context fixture

from playwright-pytest.

Comments (5)

pluckhuang avatar pluckhuang commented on May 16, 2024

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

from playwright-pytest.

wpeterw avatar wpeterw commented on May 16, 2024

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

from playwright-pytest.

hampsterx avatar hampsterx commented on May 16, 2024

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 :)

from playwright-pytest.

wpeterw avatar wpeterw commented on May 16, 2024

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

from playwright-pytest.

mrudulp avatar mrudulp commented on May 16, 2024

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

from playwright-pytest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.