Skip to content

Commit c8b8195

Browse files
committed
add playwright fixtures
1 parent b055ce4 commit c8b8195

5 files changed

Lines changed: 128 additions & 0 deletions

File tree

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,65 @@ def test_get_vocabulary(get_vocabulary):
283283

284284
```
285285

286+
### portal_factory
287+
288+
| | |
289+
| --- | --- |
290+
| Description | Factory to get portal with applied user roles. |
291+
| Required Fixture | **acceptance** |
292+
| Scope | **Function** |
293+
294+
```python
295+
import pytest
296+
from playwright.sync_api import expect
297+
from plone.app.testing.interfaces import (
298+
TEST_USER_NAME,
299+
TEST_USER_PASSWORD,
300+
)
301+
302+
303+
class TestPwEvents:
304+
@pytest.fixture(autouse=True)
305+
def setup(self, portal_factory):
306+
self.portal = portal_factory(username=TEST_USER_NAME, roles=['Member', 'Contributor'])
307+
self.plone_url = self.portal.absolute_url()
308+
309+
def test_events_listing(self) -> None:
310+
```
311+
312+
313+
### playwright_page_factory
314+
315+
| | |
316+
| --- | --- |
317+
| Description | Factory to get a Playwright page with a logged-in user. |
318+
| Required Fixture | **acceptance** |
319+
| Scope | **Function** |
320+
321+
```python
322+
import pytest
323+
from playwright.sync_api import expect
324+
from plone.app.testing.interfaces import (
325+
TEST_USER_NAME,
326+
TEST_USER_PASSWORD,
327+
)
328+
329+
330+
class TestPwEvents:
331+
@pytest.fixture(autouse=True)
332+
def setup(self, portal_factory, playwright_page_factory):
333+
self.page = playwright_page_factory(username=TEST_USER_NAME, password=TEST_USER_PASSWORD)
334+
self.portal = portal_factory(username=TEST_USER_NAME, roles=['Member', 'Contributor'])
335+
self.plone_url = self.portal.absolute_url()
336+
337+
def test_events_listing(self) -> None:
338+
page = self.page
339+
page.goto(f"{self.plone_url}")
340+
page.get_by_role("link", name="Add new…").click()
341+
page.get_by_role("link", name="Folder").click()
342+
```
343+
344+
286345
### setup_tool
287346

288347
| | |

news/34.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add two fixtures (portal_factory, playwright_page_factory) supporting test in Playwright
2+
[MrTango]

src/pytest_plone/fixtures/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from .content import get_behaviors
1212
from .content import get_fti
1313
from .env import generate_mo
14+
from .playwright import playwright_page_factory
15+
from .playwright import portal_factory
1416
from .vocabularies import get_vocabulary
1517

1618

@@ -24,7 +26,9 @@
2426
"get_vocabulary",
2527
"http_request",
2628
"installer",
29+
"playwright_page_factory",
2730
"portal",
31+
"portal_factory",
2832
"profile_last_version",
2933
"setup_tool",
3034
]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from playwright.sync_api import Page
2+
from plone import api
3+
from plone.app.testing.interfaces import SITE_OWNER_NAME
4+
from plone.app.testing.interfaces import SITE_OWNER_PASSWORD
5+
from plone.app.testing.interfaces import TEST_USER_NAME
6+
from zope.component.hooks import setSite
7+
8+
import base64
9+
import pytest
10+
import transaction
11+
12+
13+
def generate_basic_authentication_header_value(username: str, password: str) -> str:
14+
token = base64.b64encode(f"{username}:{password}".encode()).decode("ascii")
15+
return f"Basic {token}"
16+
17+
18+
@pytest.fixture()
19+
def portal_factory(acceptance, request):
20+
def factory(roles: list, username: str = TEST_USER_NAME):
21+
if not roles:
22+
roles = ["Member"]
23+
portal = acceptance["portal"]
24+
setSite(portal)
25+
with api.env.adopt_roles(["Manager", "Member"]):
26+
api.user.grant_roles(
27+
username=username,
28+
roles=roles,
29+
)
30+
transaction.commit()
31+
32+
def cleanup():
33+
with api.env.adopt_roles(["Manager", "Member"]):
34+
api.user.revoke_roles(
35+
username=username,
36+
roles=roles,
37+
)
38+
transaction.commit()
39+
40+
request.addfinalizer(cleanup)
41+
return portal
42+
43+
return factory
44+
45+
46+
@pytest.fixture()
47+
def playwright_page_factory(new_context):
48+
def factory(
49+
username: str = SITE_OWNER_NAME, password: str = SITE_OWNER_PASSWORD
50+
) -> Page:
51+
context = new_context(
52+
extra_http_headers={
53+
"Authorization": generate_basic_authentication_header_value(
54+
username, password
55+
),
56+
}
57+
)
58+
page = context.new_page()
59+
return page
60+
61+
return factory

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def testdir(pytester: Pytester) -> Pytester:
4242
"get_vocabulary",
4343
"http_request",
4444
"installer",
45+
"playwright_page_factory",
4546
"portal",
47+
"portal_factory",
4648
"profile_last_version",
4749
"setup_tool",
4850
]

0 commit comments

Comments
 (0)