Skip to content

Commit 6552bd9

Browse files
ElSnoManCarlos Kidman
andauthored
Add pyc and pys fixtures (#293)
Co-authored-by: Carlos Kidman <carlos@qap.dev>
1 parent ea70689 commit 6552bd9

File tree

4 files changed

+130
-28
lines changed

4 files changed

+130
-28
lines changed

conftest.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ def py_config(_override_pylenium_config_values) -> PyleniumConfig:
217217
return copy.deepcopy(_override_pylenium_config_values)
218218

219219

220+
@pytest.fixture(scope="class")
221+
def pyc_config(_override_pylenium_config_values) -> PyleniumConfig:
222+
"""Get a fresh copy of the PyleniumConfig for each test class"""
223+
return copy.deepcopy(_override_pylenium_config_values)
224+
225+
226+
@pytest.fixture(scope="session")
227+
def pys_config(_override_pylenium_config_values) -> PyleniumConfig:
228+
"""Get a fresh copy of the PyleniumConfig for each test session"""
229+
return copy.deepcopy(_override_pylenium_config_values)
230+
231+
220232
@pytest.fixture(scope="function")
221233
def test_case(test_results_dir: Path, py_config, request) -> TestCase:
222234
"""Manages data pertaining to the currently running Test Function or Case.
@@ -236,7 +248,7 @@ def test_case(test_results_dir: Path, py_config, request) -> TestCase:
236248

237249

238250
@pytest.fixture(scope="function")
239-
def py(test_case: TestCase, py_config, request, rp_logger):
251+
def py(test_case: TestCase, py_config: PyleniumConfig, request, rp_logger):
240252
"""Initialize a Pylenium driver for each test.
241253
242254
Pass in this `py` fixture into the test function.
@@ -273,6 +285,48 @@ def test_go_to_google(py):
273285
py.quit()
274286

275287

288+
@pytest.fixture(scope="class")
289+
def pyc(pyc_config: PyleniumConfig, request):
290+
"""Initialize a Pylenium driver for an entire test class."""
291+
py = Pylenium(pyc_config)
292+
yield py
293+
try:
294+
if request.node.report.failed:
295+
# if the test failed, execute code in this block
296+
if pyc_config.logging.screenshots_on:
297+
allure.attach(py.webdriver.get_screenshot_as_png(), "test_failed.png", allure.attachment_type.PNG)
298+
elif request.node.report.passed:
299+
# if the test passed, execute code in this block
300+
pass
301+
else:
302+
# if the test has another result (ie skipped, inconclusive), execute code in this block
303+
pass
304+
except Exception:
305+
...
306+
py.quit()
307+
308+
309+
@pytest.fixture(scope="session")
310+
def pys(pys_config: PyleniumConfig, request):
311+
"""Initialize a Pylenium driver for an entire test session."""
312+
py = Pylenium(pys_config)
313+
yield py
314+
try:
315+
if request.node.report.failed:
316+
# if the test failed, execute code in this block
317+
if pys_config.logging.screenshots_on:
318+
allure.attach(py.webdriver.get_screenshot_as_png(), "test_failed.png", allure.attachment_type.PNG)
319+
elif request.node.report.passed:
320+
# if the test passed, execute code in this block
321+
pass
322+
else:
323+
# if the test has another result (ie skipped, inconclusive), execute code in this block
324+
pass
325+
except Exception:
326+
...
327+
py.quit()
328+
329+
276330
@pytest.fixture(scope="function")
277331
def axe(py) -> PyleniumAxe:
278332
"""The aXe A11y audit tool as a fixture."""

pylenium/scripts/conftest.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ def py_config(_override_pylenium_config_values) -> PyleniumConfig:
217217
return copy.deepcopy(_override_pylenium_config_values)
218218

219219

220+
@pytest.fixture(scope="class")
221+
def pyc_config(_override_pylenium_config_values) -> PyleniumConfig:
222+
"""Get a fresh copy of the PyleniumConfig for each test class"""
223+
return copy.deepcopy(_override_pylenium_config_values)
224+
225+
226+
@pytest.fixture(scope="session")
227+
def pys_config(_override_pylenium_config_values) -> PyleniumConfig:
228+
"""Get a fresh copy of the PyleniumConfig for each test session"""
229+
return copy.deepcopy(_override_pylenium_config_values)
230+
231+
220232
@pytest.fixture(scope="function")
221233
def test_case(test_results_dir: Path, py_config, request) -> TestCase:
222234
"""Manages data pertaining to the currently running Test Function or Case.
@@ -236,7 +248,7 @@ def test_case(test_results_dir: Path, py_config, request) -> TestCase:
236248

237249

238250
@pytest.fixture(scope="function")
239-
def py(test_case: TestCase, py_config, request, rp_logger):
251+
def py(test_case: TestCase, py_config: PyleniumConfig, request, rp_logger):
240252
"""Initialize a Pylenium driver for each test.
241253
242254
Pass in this `py` fixture into the test function.
@@ -273,6 +285,48 @@ def test_go_to_google(py):
273285
py.quit()
274286

275287

288+
@pytest.fixture(scope="class")
289+
def pyc(pyc_config: PyleniumConfig, request):
290+
"""Initialize a Pylenium driver for an entire test class."""
291+
py = Pylenium(pyc_config)
292+
yield py
293+
try:
294+
if request.node.report.failed:
295+
# if the test failed, execute code in this block
296+
if pyc_config.logging.screenshots_on:
297+
allure.attach(py.webdriver.get_screenshot_as_png(), "test_failed.png", allure.attachment_type.PNG)
298+
elif request.node.report.passed:
299+
# if the test passed, execute code in this block
300+
pass
301+
else:
302+
# if the test has another result (ie skipped, inconclusive), execute code in this block
303+
pass
304+
except Exception:
305+
...
306+
py.quit()
307+
308+
309+
@pytest.fixture(scope="session")
310+
def pys(pys_config: PyleniumConfig, request):
311+
"""Initialize a Pylenium driver for an entire test session."""
312+
py = Pylenium(pys_config)
313+
yield py
314+
try:
315+
if request.node.report.failed:
316+
# if the test failed, execute code in this block
317+
if pys_config.logging.screenshots_on:
318+
allure.attach(py.webdriver.get_screenshot_as_png(), "test_failed.png", allure.attachment_type.PNG)
319+
elif request.node.report.passed:
320+
# if the test passed, execute code in this block
321+
pass
322+
else:
323+
# if the test has another result (ie skipped, inconclusive), execute code in this block
324+
pass
325+
except Exception:
326+
...
327+
py.quit()
328+
329+
276330
@pytest.fixture(scope="function")
277331
def axe(py) -> PyleniumAxe:
278332
"""The aXe A11y audit tool as a fixture."""

tests/test_flows.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22
from pylenium.driver import Pylenium
33

44

5-
@pytest.fixture
6-
def sauce(py: Pylenium) -> Pylenium:
5+
@pytest.fixture(scope="session")
6+
def sauce(pys: Pylenium) -> Pylenium:
77
"""Login to saucedemo.com as standard user."""
8-
py.visit("https://www.saucedemo.com/")
9-
py.get("#user-name").type("standard_user")
10-
py.get("#password").type("secret_sauce")
11-
py.get("#login-button").click()
12-
yield py
13-
py.get("#react-burger-menu-btn").click()
14-
py.get("#logout_sidebar_link").should().be_visible().click()
8+
pys.visit("https://www.saucedemo.com/")
9+
pys.get("#user-name").type("standard_user")
10+
pys.get("#password").type("secret_sauce")
11+
pys.get("#login-button").click()
12+
yield pys
13+
pys.get("#react-burger-menu-btn").click()
14+
pys.get("#logout_sidebar_link").should().be_visible().click()
1515

1616

17-
def test_add_to_cart_css(sauce: Pylenium):
18-
"""Add an item to the cart. The number badge on the cart icon should increment as expected."""
19-
sauce.get("[id*='add-to-cart']").click()
20-
assert sauce.get("a.shopping_cart_link").should().have_text("1")
17+
class TestSauceDemo:
18+
def test_add_to_cart_css(self, sauce: Pylenium):
19+
"""Add an item to the cart. The number badge on the cart icon should increment as expected."""
20+
sauce.get("[id*='add-to-cart']").click()
21+
assert sauce.get("a.shopping_cart_link").should().have_text("1")
2122

2223

23-
def test_add_to_cart_xpath(sauce: Pylenium):
24-
"""Add 6 different items to the cart. There should be 6 items in the cart."""
25-
for button in sauce.findx("//*[contains(@id, 'add-to-cart')]"):
26-
button.click()
27-
sauce.getx("//a[@class='shopping_cart_link']").click()
28-
assert sauce.findx("//*[@class='cart_item']").should().have_length(6)
24+
def test_add_to_cart_xpath(self, sauce: Pylenium):
25+
"""Add 6 different items to the cart. There should be 6 items in the cart."""
26+
for button in sauce.findx("//*[contains(@id, 'add-to-cart')]"):
27+
button.click()
28+
sauce.getx("//a[@class='shopping_cart_link']").click()
29+
assert sauce.findx("//*[@class='cart_item']").should().have_length(6)

tests/ui/test_element_actions.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,7 @@ def test_checkbox_buttons(py):
100100

101101

102102
def test_upload_file(py: Pylenium, project_root):
103-
# 1. Test visible upload
104103
py.visit(f"{THE_INTERNET}/upload")
105104
py.get("#file-upload").upload(f"{project_root}/LICENSE")
106105
py.get("#file-submit").click()
107106
assert py.contains("File Uploaded!")
108-
109-
# 2. Test hidden upload
110-
py.visit("https://practice.automationbro.com/cart/")
111-
py.get("#upfile_1").upload(f"{project_root}/logo.png")
112-
py.get("#upload_1").click()
113-
assert py.contains("uploaded successfully")

0 commit comments

Comments
 (0)