Skip to content

Commit

Permalink
feat: device emulation
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Nov 9, 2020
1 parent b1f1f4a commit 84349a6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
42 changes: 30 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Write end-to-end tests for your web apps with [Playwright](https://github.com/mi

## Usage

```
```txt
pip install pytest-playwright
```

Expand Down Expand Up @@ -38,6 +38,7 @@ pytest --browser chromium --browser webkit
```

If you want to add the CLI arguments automatically without specifying them, you can use the [pytest.ini](https://docs.pytest.org/en/stable/reference.html#ini-options-ref) file:

```ini
# content of pytest.ini
[pytest]
Expand All @@ -46,8 +47,8 @@ addopts = --headful --browser firefox

```


## Fixtures

This plugin configures Playwright-specific [fixtures for pytest](https://docs.pytest.org/en/latest/fixture.html). To use these fixtures, use the fixture name as an argument to the test function.

```py
Expand All @@ -74,7 +75,7 @@ def test_my_app_is_working(fixture_name):

## Examples

#### Configure Mypy typings for auto-completion
### Configure Mypy typings for auto-completion

```py
from playwright.sync_api import Page
Expand All @@ -84,7 +85,7 @@ def test_visit_admin_dashboard(page: Page):
# ...
```

#### Skip test by browser
### Skip test by browser

```py
import pytest
Expand All @@ -95,7 +96,7 @@ def test_visit_example(page):
# ...
```

#### Run on a specific browser
### Run on a specific browser

```py
import pytest
Expand All @@ -106,9 +107,9 @@ def test_visit_example(page):
# ...
```

#### Configure base-url
### Configure base-url

Start Pytest with the `base-url` argument.
Start Pytest with the `base-url` argument.

```sh
pytest --base-url http://localhost:8080
Expand All @@ -120,7 +121,7 @@ def test_visit_example(page):
# -> Will result in http://localhost:8080/admin
```

#### Ignore HTTPS errors
### Ignore HTTPS errors

conftest.py

Expand All @@ -135,8 +136,7 @@ def browser_context_args(browser_context_args):
}
```


#### Use custom viewport size
### Use custom viewport size

conftest.py

Expand All @@ -154,9 +154,26 @@ def browser_context_args(browser_context_args):
}
```

### Device emulation

conftest.py

```py
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,
}
```

## Debugging

#### Use with pdb
### Use with pdb

Use the `breakpoint()` statement in your test code to pause execution and get a [pdb](https://docs.python.org/3/library/pdb.html) REPL.

```py
Expand All @@ -166,7 +183,7 @@ def test_bing_is_working(page):
# ...
```

#### Screenshot on test failure
### Screenshot on test failure

You can capture screenshots for failed tests with a [pytest runtest hook](https://docs.pytest.org/en/6.1.0/reference.html?highlight=pytest_runtest_makereport#test-running-runtest-hooks). Add this to your `conftest.py` file.

Expand All @@ -187,6 +204,7 @@ def pytest_runtest_makereport(item, call) -> None:
```

## Deploy to CI

Use the [Playwright GitHub Action](https://github.com/microsoft/playwright-github-action) or [guides for other CI providers](https://playwright.dev/#path=docs%2Fci.md&q=) to deploy your tests to CI/CD

## Special thanks
Expand Down
17 changes: 7 additions & 10 deletions pytest_playwright/pytest_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,22 @@ def browser_type_launch_args() -> Dict:
def browser_context_args() -> Dict:
return {}

@pytest.fixture(scope="session")
def playwright():
pw = sync_playwright().start()
yield pw
pw.stop()

@pytest.fixture(scope="session")
def launch_browser(
pytestconfig: Any, browser_type_launch_args: Dict, browser_name: str
pytestconfig: Any, playwright: Any, browser_type_launch_args: Dict, browser_name: str
) -> Callable[..., Browser]:
def launch(**kwargs: Dict[Any, Any]) -> Browser:
headful_option = pytestconfig.getoption("--headful")
launch_options = {**browser_type_launch_args, **kwargs}
if headful_option:
launch_options["headless"] = False
pw = sync_playwright().start()
browser = getattr(pw, browser_name).launch(**launch_options)
browser._close = browser.close

def _handle_close() -> None:
browser._close()
pw.stop()

browser.close = _handle_close
browser = getattr(playwright, browser_name).launch(**launch_options)
return browser

return launch
Expand Down
22 changes: 21 additions & 1 deletion tests/test_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_browser_context_args(testdir: Any) -> None:
import pytest
@pytest.fixture(scope="session")
def browser_context_args(request):
def browser_context_args():
return {"userAgent": "foobar"}
"""
)
Expand Down Expand Up @@ -191,3 +191,23 @@ def test_one(self):
)
result = testdir.runpytest()
result.assert_outcomes(passed=1)

def test_device_emulation(testdir: Any) -> None:
testdir.makeconftest(
"""
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}
"""
)
testdir.makepyfile(
"""
def test_browser_context_args(page):
assert "iPhone" in page.evaluate("window.navigator.userAgent")
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=1)

0 comments on commit 84349a6

Please sign in to comment.