Pytest plugin
Playwright provides a dedicated Pytest plug-in to complete end-to-end testing of the Web UI
Use installation
To install pytest playwright:
pip install pytest-playwright
Write a simple page fixture:
# test_my_application.py def test_example(page): page.goto('https://www.baidu.com')
Enter the pytest command in the CLI:
#Without any parameters, it defaults to the headless mode of Google browser pytest # Add the -- headed parameter to start the default Google browser header mode pytest --headed # --Browser runs according to the specified browser pytest --browser firefox # Add multiple -- browser parameters to run in multiple browsers pytest --browser chromium --browser webkit
If you don't want to use such a default parameter, you can use pytest Custom modification in INI file
CLI parameters
- – headed: run tests in the headless mode (headless mode by default)
- – browser: runs tests only under the specified browser, and can be called multiple times at the same time to meet the requirements of multiple specified browsers
- – Browser channel: use Browser channel
- – slowmo: run tests at a reduced speed
- – device: analog device
- – output: storage of test output (default: test results)
- – tracing: whether the trace function is enabled. The parameters are on, off or retain on failure. The default value is off
- – video: whether to turn on the recording function. The parameters are on, off or retain on failure. The default value is off
- – screenshot: whether screenshots are automatically captured after each test. The parameters are on, off or retain on failure. The default value is off
Fixtures
This plug-in is a Playwright specific configuration for pytest. To use Fixtures, you need to use the Fixtures name as an argument to the test function
def test_my_app_is_working(fixture_name): # Test using fixture_name # ...
Function fields: fixtures are created when testing functions and destroyed at the end
- Context: new browser context for testing
- Page: a new browser page for testing
Session domain: fixtures are created when the function is tested and destroyed at the end
- Playwright: playwright instance
- browser_type: BrowserType instance of the current browser
- Browser: a browser instance started by Playwright
- browser_name: browser name (String)
- browser_channel: Browser channel (String)
- is_chromium, is_webkit, is_firefox: Boolean type to determine whether it is the specified browser type
Custom fixture options: for browser and context fixtures, use the following fixtures to define custom startup options
- browser_type_launch_args: overwrite browser_ type. Startup parameters of launch (* * kwargs). Returns a dictionary
- browser_context_args: override browser new_ Parameter options of context (* * kwargs). Returns a dictionary
example
- Skip browser of specified type:
import pytest @pytest.mark.skip_browser("chromium") def test_example(page): page.goto('https://www.baidu.com')
- Only specified browser types are allowed:
import pytest @pytest.mark.only_browser("chromium") def test_example(page): page.goto('https://www.baidu.com')
- Ignore HTTPS error:
import pytest @pytest.fixture(scope="session") def browser_context_args(browser_context_args): return { **browser_context_args, "ignore_https_errors": True }
- Custom viewport size:
import pytest @pytest.fixture(scope="session") def browser_context_args(browser_context_args): return { **browser_context_args, "viewport": { "width": 1920, "height": 1080, } }
- Equipment simulation
import pytest @pytest.fixture(scope="session") def browser_context_args(browser_context_args, playwright): iphone_11 = playwright.devices['iPhone 11 Pro'] return { **browser_context_args, **iphone_11, }
Or through the command parameter -- device="iPhone 11 Pro"