Skip to content

Commit

Permalink
Add pyc and pys fixtures (#293)
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos Kidman <carlos@qap.dev>
  • Loading branch information
ElSnoMan and Carlos Kidman committed Oct 25, 2022
1 parent ea70689 commit 6552bd9
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 28 deletions.
56 changes: 55 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ def py_config(_override_pylenium_config_values) -> PyleniumConfig:
return copy.deepcopy(_override_pylenium_config_values)


@pytest.fixture(scope="class")
def pyc_config(_override_pylenium_config_values) -> PyleniumConfig:
"""Get a fresh copy of the PyleniumConfig for each test class"""
return copy.deepcopy(_override_pylenium_config_values)


@pytest.fixture(scope="session")
def pys_config(_override_pylenium_config_values) -> PyleniumConfig:
"""Get a fresh copy of the PyleniumConfig for each test session"""
return copy.deepcopy(_override_pylenium_config_values)


@pytest.fixture(scope="function")
def test_case(test_results_dir: Path, py_config, request) -> TestCase:
"""Manages data pertaining to the currently running Test Function or Case.
Expand All @@ -236,7 +248,7 @@ def test_case(test_results_dir: Path, py_config, request) -> TestCase:


@pytest.fixture(scope="function")
def py(test_case: TestCase, py_config, request, rp_logger):
def py(test_case: TestCase, py_config: PyleniumConfig, request, rp_logger):
"""Initialize a Pylenium driver for each test.
Pass in this `py` fixture into the test function.
Expand Down Expand Up @@ -273,6 +285,48 @@ def test_go_to_google(py):
py.quit()


@pytest.fixture(scope="class")
def pyc(pyc_config: PyleniumConfig, request):
"""Initialize a Pylenium driver for an entire test class."""
py = Pylenium(pyc_config)
yield py
try:
if request.node.report.failed:
# if the test failed, execute code in this block
if pyc_config.logging.screenshots_on:
allure.attach(py.webdriver.get_screenshot_as_png(), "test_failed.png", allure.attachment_type.PNG)
elif request.node.report.passed:
# if the test passed, execute code in this block
pass
else:
# if the test has another result (ie skipped, inconclusive), execute code in this block
pass
except Exception:
...
py.quit()


@pytest.fixture(scope="session")
def pys(pys_config: PyleniumConfig, request):
"""Initialize a Pylenium driver for an entire test session."""
py = Pylenium(pys_config)
yield py
try:
if request.node.report.failed:
# if the test failed, execute code in this block
if pys_config.logging.screenshots_on:
allure.attach(py.webdriver.get_screenshot_as_png(), "test_failed.png", allure.attachment_type.PNG)
elif request.node.report.passed:
# if the test passed, execute code in this block
pass
else:
# if the test has another result (ie skipped, inconclusive), execute code in this block
pass
except Exception:
...
py.quit()


@pytest.fixture(scope="function")
def axe(py) -> PyleniumAxe:
"""The aXe A11y audit tool as a fixture."""
Expand Down
56 changes: 55 additions & 1 deletion pylenium/scripts/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ def py_config(_override_pylenium_config_values) -> PyleniumConfig:
return copy.deepcopy(_override_pylenium_config_values)


@pytest.fixture(scope="class")
def pyc_config(_override_pylenium_config_values) -> PyleniumConfig:
"""Get a fresh copy of the PyleniumConfig for each test class"""
return copy.deepcopy(_override_pylenium_config_values)


@pytest.fixture(scope="session")
def pys_config(_override_pylenium_config_values) -> PyleniumConfig:
"""Get a fresh copy of the PyleniumConfig for each test session"""
return copy.deepcopy(_override_pylenium_config_values)


@pytest.fixture(scope="function")
def test_case(test_results_dir: Path, py_config, request) -> TestCase:
"""Manages data pertaining to the currently running Test Function or Case.
Expand All @@ -236,7 +248,7 @@ def test_case(test_results_dir: Path, py_config, request) -> TestCase:


@pytest.fixture(scope="function")
def py(test_case: TestCase, py_config, request, rp_logger):
def py(test_case: TestCase, py_config: PyleniumConfig, request, rp_logger):
"""Initialize a Pylenium driver for each test.
Pass in this `py` fixture into the test function.
Expand Down Expand Up @@ -273,6 +285,48 @@ def test_go_to_google(py):
py.quit()


@pytest.fixture(scope="class")
def pyc(pyc_config: PyleniumConfig, request):
"""Initialize a Pylenium driver for an entire test class."""
py = Pylenium(pyc_config)
yield py
try:
if request.node.report.failed:
# if the test failed, execute code in this block
if pyc_config.logging.screenshots_on:
allure.attach(py.webdriver.get_screenshot_as_png(), "test_failed.png", allure.attachment_type.PNG)
elif request.node.report.passed:
# if the test passed, execute code in this block
pass
else:
# if the test has another result (ie skipped, inconclusive), execute code in this block
pass
except Exception:
...
py.quit()


@pytest.fixture(scope="session")
def pys(pys_config: PyleniumConfig, request):
"""Initialize a Pylenium driver for an entire test session."""
py = Pylenium(pys_config)
yield py
try:
if request.node.report.failed:
# if the test failed, execute code in this block
if pys_config.logging.screenshots_on:
allure.attach(py.webdriver.get_screenshot_as_png(), "test_failed.png", allure.attachment_type.PNG)
elif request.node.report.passed:
# if the test passed, execute code in this block
pass
else:
# if the test has another result (ie skipped, inconclusive), execute code in this block
pass
except Exception:
...
py.quit()


@pytest.fixture(scope="function")
def axe(py) -> PyleniumAxe:
"""The aXe A11y audit tool as a fixture."""
Expand Down
39 changes: 20 additions & 19 deletions tests/test_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@
from pylenium.driver import Pylenium


@pytest.fixture
def sauce(py: Pylenium) -> Pylenium:
@pytest.fixture(scope="session")
def sauce(pys: Pylenium) -> Pylenium:
"""Login to saucedemo.com as standard user."""
py.visit("https://www.saucedemo.com/")
py.get("#user-name").type("standard_user")
py.get("#password").type("secret_sauce")
py.get("#login-button").click()
yield py
py.get("#react-burger-menu-btn").click()
py.get("#logout_sidebar_link").should().be_visible().click()
pys.visit("https://www.saucedemo.com/")
pys.get("#user-name").type("standard_user")
pys.get("#password").type("secret_sauce")
pys.get("#login-button").click()
yield pys
pys.get("#react-burger-menu-btn").click()
pys.get("#logout_sidebar_link").should().be_visible().click()


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


def test_add_to_cart_xpath(sauce: Pylenium):
"""Add 6 different items to the cart. There should be 6 items in the cart."""
for button in sauce.findx("//*[contains(@id, 'add-to-cart')]"):
button.click()
sauce.getx("//a[@class='shopping_cart_link']").click()
assert sauce.findx("//*[@class='cart_item']").should().have_length(6)
def test_add_to_cart_xpath(self, sauce: Pylenium):
"""Add 6 different items to the cart. There should be 6 items in the cart."""
for button in sauce.findx("//*[contains(@id, 'add-to-cart')]"):
button.click()
sauce.getx("//a[@class='shopping_cart_link']").click()
assert sauce.findx("//*[@class='cart_item']").should().have_length(6)
7 changes: 0 additions & 7 deletions tests/ui/test_element_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,7 @@ def test_checkbox_buttons(py):


def test_upload_file(py: Pylenium, project_root):
# 1. Test visible upload
py.visit(f"{THE_INTERNET}/upload")
py.get("#file-upload").upload(f"{project_root}/LICENSE")
py.get("#file-submit").click()
assert py.contains("File Uploaded!")

# 2. Test hidden upload
py.visit("https://practice.automationbro.com/cart/")
py.get("#upfile_1").upload(f"{project_root}/logo.png")
py.get("#upload_1").click()
assert py.contains("uploaded successfully")

0 comments on commit 6552bd9

Please sign in to comment.