From 6552bd91739b7f7f244ad9f509fd0d44971443c9 Mon Sep 17 00:00:00 2001 From: Carlos Kidman Date: Tue, 25 Oct 2022 13:28:02 -0600 Subject: [PATCH] Add pyc and pys fixtures (#293) Co-authored-by: Carlos Kidman --- conftest.py | 56 +++++++++++++++++++++++++++++++- pylenium/scripts/conftest.py | 56 +++++++++++++++++++++++++++++++- tests/test_flows.py | 39 +++++++++++----------- tests/ui/test_element_actions.py | 7 ---- 4 files changed, 130 insertions(+), 28 deletions(-) diff --git a/conftest.py b/conftest.py index 4d17e3b..7bb7309 100644 --- a/conftest.py +++ b/conftest.py @@ -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. @@ -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. @@ -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.""" diff --git a/pylenium/scripts/conftest.py b/pylenium/scripts/conftest.py index 4d17e3b..7bb7309 100644 --- a/pylenium/scripts/conftest.py +++ b/pylenium/scripts/conftest.py @@ -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. @@ -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. @@ -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.""" diff --git a/tests/test_flows.py b/tests/test_flows.py index 9b5d5e6..dbd1f69 100644 --- a/tests/test_flows.py +++ b/tests/test_flows.py @@ -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) diff --git a/tests/ui/test_element_actions.py b/tests/ui/test_element_actions.py index 5606477..05dc037 100644 --- a/tests/ui/test_element_actions.py +++ b/tests/ui/test_element_actions.py @@ -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")