Skip to content

Commit 92dac1f

Browse files
authored
Merge pull request #196 from nirtal85/ruff
uv
2 parents abb3f4d + efacbe7 commit 92dac1f

File tree

12 files changed

+596
-157
lines changed

12 files changed

+596
-157
lines changed

.github/workflows/devRun.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v4
13-
- uses: psf/black@stable
14-
- uses: isort/isort-action@v1
1513
- name: Set up Python
1614
uses: actions/setup-python@v5
1715
with:
@@ -23,7 +21,7 @@ jobs:
2321
- name: Create venv & install dependencies
2422
run: |
2523
uv venv
26-
uv pip sync uv.lock
24+
uv sync --all-extras --dev
2725
- name: Install Playwright Browsers
2826
run: |
2927
PLAYWRIGHT_VERSION=$(grep -E '^playwright = "[^"]*"' pyproject.toml | sed -E 's/playwright = "([^"]*)".*$/\1/')

.github/workflows/nightly.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
- name: Create venv & install dependencies
4848
run: |
4949
uv venv
50-
uv pip sync uv.lock
50+
uv sync --all-extras --dev
5151
- name: Install Playwright Browsers
5252
run: |
5353
PLAYWRIGHT_VERSION=$(grep -E '^playwright = "[^"]*"' pyproject.toml | sed -E 's/playwright = "([^"]*)".*$/\1/')

.pre-commit-config.yaml

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,14 @@ repos:
3535
- id: conventional-pre-commit
3636
stages: [commit-msg]
3737
args: []
38-
- repo: https://github.com/psf/black
39-
rev: 25.1.0
40-
hooks:
41-
- id: black
42-
language_version: python3
43-
args: [ '--config', 'pyproject.toml' ]
44-
- repo: https://github.com/PyCQA/autoflake
45-
rev: v2.3.1
46-
hooks:
47-
- id: autoflake
48-
args:
49-
[
50-
'--in-place',
51-
'--remove-unused-variable',
52-
'--remove-all-unused-imports',
53-
'--expand-star-imports',
54-
'--ignore-init-module-imports',
55-
]
56-
- repo: https://github.com/PyCQA/isort
57-
rev: 6.0.1
58-
hooks:
59-
- id: isort
60-
args: [ '--settings-file', 'pyproject.toml' ]
61-
- repo: https://github.com/asottile/pyupgrade
62-
rev: v3.19.1
63-
hooks:
64-
- id: pyupgrade
38+
- repo: https://github.com/astral-sh/ruff-pre-commit
39+
rev: v0.11.4
40+
hooks:
41+
- id: ruff
42+
args: [ --fix ]
43+
continue_on_error: true
44+
- id: ruff-format
45+
continue_on_error: true
6546
- repo: https://github.com/codespell-project/codespell
6647
rev: v2.4.1
6748
hooks:
@@ -72,11 +53,10 @@ repos:
7253
rev: v0.24.1
7354
hooks:
7455
- id: validate-pyproject
75-
# Optional extra validations from SchemaStore:
76-
additional_dependencies: [ "validate-pyproject-schema-store[all]" ]
56+
additional_dependencies: ["validate-pyproject-schema-store[all]"]
7757
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
7858
rev: v2.14.0
7959
hooks:
8060
- id: pretty-format-toml
8161
exclude: poetry.lock
82-
args: [ --autofix ]
62+
args: [--autofix]

.github/README.md renamed to README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
![YouTube Channel](https://img.shields.io/youtube/channel/subscribers/UCQjS-eoKl0a1nuP_dvpLsjQ?label=YouTube%20Channel)
55
![dev run](https://github.com/nirtal85/Playwright-Python-Example/actions/workflows/devRun.yml/badge.svg)
66
![nightly](https://github.com/nirtal85/Playwright-Python-Example/actions/workflows/nightly.yml/badge.svg)
7-
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
8-
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
7+
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
8+
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
99

1010
## 📃 Articles written about this project
1111

@@ -50,15 +50,15 @@ cd playwright-python
5050
pip install uv
5151
uv venv
5252
.\env\Scripts\activate
53-
uv pip sync uv.lock
53+
uv sync --all-extras --dev
5454
```
5555

5656
#### For Mac:
5757
```bash
5858
python3 -m pip install uv
5959
uv venv
60-
source .venv/bin/activate
61-
uv pip sync uv.lock
60+
source .venv/bin/activate
61+
uv sync --all-extras --dev
6262
```
6363

6464
### Install playwright

pyproject.toml

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
[build-system]
2-
requires = ["hatchling"]
32
build-backend = "hatchling.build"
4-
5-
[project]
6-
name = "playwright-python"
7-
version = "0.1.0"
8-
description = "Playwright Python example project with pytest and Allure report"
9-
authors = [{ name = "nirtal85", email = "[email protected]" }]
10-
requires-python = "~=3.11"
11-
readme = "README.md"
12-
keywords = [
13-
"playwright",
14-
"automation",
15-
"testing",
16-
"web",
17-
]
18-
dependencies = [
19-
"allure-pytest==2.13.5",
20-
"axe-playwright-python==0.1.4",
21-
"playwright==1.51.0",
22-
"pytest==8.3.5",
23-
"pytest-base-url==2.1.0",
24-
"pytest-playwright==0.7.0",
25-
"pytest-split==0.10.0",
26-
"requests==2.32.3",
27-
]
3+
requires = ["hatchling"]
284

295
[dependency-groups]
306
dev = [
31-
"black==25.1.0",
32-
"isort==6.0.1",
33-
"pre-commit==4.2.0",
7+
"ruff==0.11.4",
8+
"pre-commit==4.2.0"
349
]
3510

36-
[tool.isort]
37-
profile = "black"
38-
skip = ["env", "venv"]
11+
[project]
12+
authors = [{name = "nirtal85", email = "[email protected]"}]
13+
dependencies = [
14+
"allure-pytest==2.13.5",
15+
"axe-playwright-python==0.1.4",
16+
"playwright==1.51.0",
17+
"pytest==8.3.5",
18+
"pytest-base-url==2.1.0",
19+
"pytest-playwright==0.7.0",
20+
"pytest-split==0.10.0",
21+
"requests==2.32.3"
22+
]
23+
description = "Playwright Python example project with pytest and Allure report"
24+
keywords = [
25+
"playwright",
26+
"automation",
27+
"testing",
28+
"web"
29+
]
30+
name = "playwright-python"
31+
readme = "README.md"
32+
requires-python = "~=3.11"
33+
version = "0.1.0"
3934

4035
[tool.hatch.build.targets.sdist]
4136
include = ["playwright_python"]
4237

4338
[tool.hatch.build.targets.wheel]
4439
include = ["playwright_python"]
4540

41+
[tool.isort]
42+
profile = "black"
43+
skip = ["env", "venv"]
44+
4645
[tool.pytest.ini_options]
4746
addopts = [
4847
"--clean-alluredir",
@@ -68,3 +67,18 @@ log_cli_level = "INFO"
6867
markers = [
6968
"devRun: marks tests that run before merge to the main branch"
7069
]
70+
71+
[tool.ruff]
72+
exclude = [".venv", "env"]
73+
ignore = [
74+
"D203", # One blank line required before class docstring (conflicts with D211)
75+
"D213", # Multi-line docstring summary should start at the second line
76+
"COM812"
77+
]
78+
line-length = 100
79+
select = ["ALL"]
80+
target-version = "py311"
81+
82+
[tool.ruff.format]
83+
docstring-code-format = true
84+
quote-style = "double"

tests/accesability_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
class TestAccessibility:
5-
65
@allure.title("Test Accessibility with Default Counts")
76
def test_accessibility_default_counts(self, axe_playwright, page):
87
axe_playwright.check_accessibility(page)

tests/checkout_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66

77
class TestCheckout:
8-
9-
@pytest.mark.parametrize(
10-
"browser_context_args", [User.STANDARD_USER], indirect=True
11-
)
8+
@pytest.mark.parametrize("browser_context_args", [User.STANDARD_USER], indirect=True)
129
def test_checkout_counter(self, browser_context_args, page: Page):
1310
page.evaluate("localStorage.setItem('cart-contents', '[4,0]');")
1411
page.reload()

tests/conftest.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Dict
2-
31
import allure
42
import pytest
53
import requests
@@ -27,6 +25,7 @@ def goto(page: Page, request: SubRequest):
2725
2826
Example:
2927
@pytest.mark.parametrize('browser_context_args', ["standard_user"], indirect=True)
28+
3029
"""
3130
if request.getfixturevalue("browser_context_args").get("storage_state"):
3231
page.goto("/inventory.html")
@@ -43,27 +42,29 @@ def axe_playwright():
4342
4443
Returns:
4544
AxeHelper: An instance of AxeHelper with Axe initialized.
45+
4646
"""
47-
yield AxeHelper(Axe())
47+
return AxeHelper(Axe())
4848

4949

5050
@pytest.fixture(scope="function")
51-
def browser_context_args(
52-
browser_context_args: Dict, base_url: str, request: SubRequest
53-
):
51+
def browser_context_args(browser_context_args: dict, base_url: str, request: SubRequest) -> dict:
5452
"""This fixture allows setting browser context arguments for Playwright.
5553
5654
Args:
5755
browser_context_args (dict): Base browser context arguments.
5856
request (SubRequest): Pytest request object to get the 'browser_context_args' fixture value.
5957
base_url (str): The base URL for the application under test.
58+
6059
Returns:
6160
dict: Updated browser context arguments.
61+
6262
See Also:
6363
https://playwright.dev/python/docs/api/class-browser#browser-new-contex
6464
6565
Returns:
6666
dict: Updated browser context arguments.
67+
6768
"""
6869
context_args = {
6970
**browser_context_args,
@@ -85,7 +86,7 @@ def browser_context_args(
8586

8687

8788
@pytest.fixture(scope="session")
88-
def browser_type_launch_args(browser_type_launch_args: Dict, playwright: Playwright):
89+
def browser_type_launch_args(browser_type_launch_args: dict, playwright: Playwright) -> dict:
8990
"""Fixture to set browser launch arguments.
9091
9192
This fixture updates the browser launch arguments to start the browser maximized
@@ -103,6 +104,7 @@ def browser_type_launch_args(browser_type_launch_args: Dict, playwright: Playwri
103104
104105
See Also:
105106
https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch
107+
106108
"""
107109
playwright.selectors.set_test_id_attribute("data-test")
108110
return {**browser_type_launch_args, "args": ["--start-maximized"]}
@@ -113,6 +115,7 @@ def get_public_ip() -> str:
113115
114116
Returns:
115117
str: Public IP address.
118+
116119
"""
117120
return requests.get(
118121
"http://checkip.amazonaws.com",
@@ -129,6 +132,7 @@ def attach_playwright_results(page: Page, request: FixtureRequest):
129132
Args:
130133
page (Page): Playwright page object.
131134
request: Pytest request object.
135+
132136
"""
133137
yield
134138
if request.node.rep_call.failed:
@@ -158,6 +162,7 @@ def pytest_runtest_makereport(item: Item):
158162
159163
Yields:
160164
Outcome of the test execution.
165+
161166
"""
162167
outcome = yield
163168
rep = outcome.get_result()

tests/inventory_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66

77
class TestInventory:
8-
9-
@pytest.mark.parametrize(
10-
"browser_context_args", [User.STANDARD_USER], indirect=True
11-
)
8+
@pytest.mark.parametrize("browser_context_args", [User.STANDARD_USER], indirect=True)
129
def test_inventory_page(self, browser_context_args, page: Page):
1310
expect(page.get_by_test_id("title")).to_have_text("Products")

tests/login_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ def test_valid_login(self, base_url, page: Page):
3434
ids=["invalid_password", "locked_user"],
3535
)
3636
@allure.title("Login with invalid credentials test")
37-
def test_login_error(
38-
self, page: Page, username: str, password: str, expected_error: str
39-
):
37+
def test_login_error(self, page: Page, username: str, password: str, expected_error: str):
4038
self.login_page.login(username, password)
4139
expect(self.login_page.error_message).to_have_text(expected_error)

utilities/axe_helper.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import json
22
from collections import Counter
3-
from typing import Dict
43

54
import allure
65
from axe_playwright_python.sync_playwright import Axe
76
from playwright.sync_api import Page
87

98

109
class AxeHelper:
11-
1210
def __init__(self, axe: Axe):
1311
self.axe = axe
1412

1513
def check_accessibility(
16-
self, page: Page, maximum_allowed_violations_by_impact: Dict[str, int] = None
14+
self, page: Page, maximum_allowed_violations_by_impact: dict[str, int] = None
1715
) -> None:
1816
"""Checks accessibility of the page using playwright axe.
1917
@@ -33,15 +31,12 @@ def check_accessibility(
3331
}
3432
results = self.axe.run(page)
3533
violations_count = dict(
36-
Counter(
37-
[violation["impact"] for violation in results.response["violations"]]
38-
)
34+
Counter([violation["impact"] for violation in results.response["violations"]])
3935
)
4036
if violations_exceeded := {
4137
impact_level: violation_count
4238
for impact_level, violation_count in violations_count.items()
43-
if violation_count
44-
> maximum_allowed_violations_by_impact.get(impact_level, 0)
39+
if violation_count > maximum_allowed_violations_by_impact.get(impact_level, 0)
4540
}:
4641
allure.attach(
4742
body=json.dumps(results.response["violations"], indent=4),

0 commit comments

Comments
 (0)