Comments (5)
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.
Thanks but I'm looking for an example om how to use this:
from playwright-pytest.
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.
Thanks for the example. Where do you create the browser ? In conftest.py ?
from playwright-pytest.
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)
- [Feature] UI mode for Python/Pytest HOT 1
- [BUG] Can't run tests with multiple browsers when overriding page fixture
- [QUESTION] How to retrieve the version of a browser with playwright-pytest? HOT 3
- Inside pytest_playwright plugin browser_context_args() fixture is session scoped, but should be function scoped. HOT 4
- Change locale without launching a persistent contex HOT 2
- [BUG] Unable to set timezone_id or locale using pytest.mark fixture HOT 2
- Can't click or find elements on page without a slight pause/time.sleep() first HOT 5
- Rerunning skipped dependent test cases when a parent test fails HOT 1
- [BUG] Playwright fixture hangs on teardown with pytest-flask HOT 1
- Setting locale for a test affects the local for other tests HOT 1
- Coverage support HOT 1
- [BUG] Error when installing in Python 3.12 HOT 2
- Pytest.ini set --screenshot="only-on-failure" --video="retain-on-failure" Screenshots and recorded videos will also be generated after the use case is successfully executed.
- pytest-playwright--parameterization, cases order Not what I expected HOT 8
- playwright._impl._api_types.Error: net::ERR_CONNECTION_TIMED_OUT at https://www.google.com HOT 2
- [FEATURE] Support connecting to remote browsers HOT 1
- Request: `async` support HOT 3
- [FEATURE] Support pytest 8.x HOT 3
- [BUG]The execution sequence is incorrect when use parametrize HOT 1
- Request: extending `browser_type_launch_args` to work with `request`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from playwright-pytest.