diff --git a/tests/conftest.py b/tests/conftest.py index 132c19f..35d8ba1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -94,6 +94,27 @@ def page(context): page_instance.close() +@pytest.fixture(scope="function") +def authenticated_page(page, config, test_data): + """ + Create a new page and authenticate a valid user. + This fixture is used by tests that require an already authenticated SauceDemo session. + """ + from pages.login_page import LoginPage + + login_page = LoginPage(page) + + # Navigate to application + login_page.navigate(config.get("application.base_url")) + + # Retrieve the first valid user from test data + valid_user = test_data.get("users.valid_user")[0] + + # Login with valid credentials + login_page.login(username=valid_user["username"], password=valid_user["password"]) + return page + + @pytest.fixture(scope="session") def test_data(): """ diff --git a/tests/e2e/test_cart.py b/tests/e2e/test_cart.py index 5860248..c1feaac 100644 --- a/tests/e2e/test_cart.py +++ b/tests/e2e/test_cart.py @@ -1,6 +1,6 @@ from pages.cart_page import CartPage from pages.inventory_page import InventoryPage -from pages.login_page import LoginPage +# from pages.login_page import LoginPage import pytest import allure import json @@ -27,25 +27,25 @@ def load_inventory_test_data(): @pytest.mark.regression @pytest.mark.e2e @pytest.mark.parametrize("product_data", inventory_test_data.values(), ids=lambda product: product["name"]) -def test_product_is_displayed_in_cart(page, config, test_data, product_data): +def test_product_is_displayed_in_cart(authenticated_page, product_data): """ Verify that each selected product appears in the shopping cart. """ - with allure.step("Navigate to SauceDemo application"): - login_page = LoginPage(page) - login_page.navigate(config.get("application.base_url")) + # with allure.step("Navigate to SauceDemo application"): + # login_page = LoginPage(page) + # login_page.navigate(config.get("application.base_url")) - with allure.step("Retrieve valid user credentials"): - valid_user = test_data.get("users.valid_user")[0] - username = valid_user["username"] - password = valid_user["password"] + # with allure.step("Retrieve valid user credentials"): + # valid_user = test_data.get("users.valid_user")[0] + # username = valid_user["username"] + # password = valid_user["password"] - with allure.step("Login with valid credentials"): - login_page.login(username=username, password=password) + # with allure.step("Login with valid credentials"): + # login_page.login(username=username, password=password) with allure.step("Verify inventory page is loaded"): - inventory_page = InventoryPage(page) + inventory_page = InventoryPage(authenticated_page) assert inventory_page.is_loaded() with allure.step(f"Retrieve product name: {product_data['name']}"): @@ -58,7 +58,7 @@ def test_product_is_displayed_in_cart(page, config, test_data, product_data): inventory_page.open_cart() with allure.step("Verify cart page is loaded"): - cart_page = CartPage(page) + cart_page = CartPage(authenticated_page) assert cart_page.is_loaded() with allure.step(f"Verify '{product_name}' is present in cart"): diff --git a/tests/e2e/test_checkout.py b/tests/e2e/test_checkout.py index 6ae042e..f5962ba 100644 --- a/tests/e2e/test_checkout.py +++ b/tests/e2e/test_checkout.py @@ -3,7 +3,7 @@ from pages.checkout_overview_page import CheckoutOverviewPage from pages.checkout_page import CheckoutPage from pages.inventory_page import InventoryPage -from pages.login_page import LoginPage +# from pages.login_page import LoginPage import pytest import allure import json @@ -58,25 +58,25 @@ def load_product_test_data(): @pytest.mark.regression @pytest.mark.e2e @pytest.mark.parametrize("product_data, customer_data", checkout_test_cases, ids=checkout_test_ids) -def test_complete_checkout_flow(page, config, test_data, product_data, customer_data): +def test_complete_checkout_flow(authenticated_page, product_data, customer_data): """ Verify that a user can complete a complete product purchase flow using data-driven product and customer combinations. """ - with allure.step("Navigate to SauceDemo application"): - login_page = LoginPage(page) - login_page.navigate(config.get("application.base_url")) + # with allure.step("Navigate to SauceDemo application"): + # login_page = LoginPage(page) + # login_page.navigate(config.get("application.base_url")) - with allure.step("Retrieve valid user credentials"): - valid_user = test_data.get("users.valid_user")[0] - username = valid_user["username"] - password = valid_user["password"] + # with allure.step("Retrieve valid user credentials"): + # valid_user = test_data.get("users.valid_user")[0] + # username = valid_user["username"] + # password = valid_user["password"] - with allure.step(f"Login with user: {valid_user['test_id']}"): - login_page.login(username=username, password=password) + # with allure.step(f"Login with user: {valid_user['test_id']}"): + # login_page.login(username=username, password=password) with allure.step("Verify inventory page is loaded"): - inventory_page = InventoryPage(page) + inventory_page = InventoryPage(authenticated_page) assert inventory_page.is_loaded() with allure.step(f"Retrieve product name: {product_data['name']}"): @@ -89,7 +89,7 @@ def test_complete_checkout_flow(page, config, test_data, product_data, customer_ inventory_page.open_cart() with allure.step("Verify cart page is loaded"): - cart_page = CartPage(page) + cart_page = CartPage(authenticated_page) assert cart_page.is_loaded() with allure.step(f"Verify {product_name} is present in cart"): @@ -99,7 +99,7 @@ def test_complete_checkout_flow(page, config, test_data, product_data, customer_ cart_page.click_checkout() with allure.step("Verify checkout information page is loaded"): - checkout_page = CheckoutPage(page) + checkout_page = CheckoutPage(authenticated_page) assert checkout_page.is_loaded() with allure.step(f"Enter customer information for {customer_data['test_id']}"): @@ -109,7 +109,7 @@ def test_complete_checkout_flow(page, config, test_data, product_data, customer_ checkout_page.click_continue() with allure.step("Verify checkout overview page is loaded"): - checkout_overview_page = CheckoutOverviewPage(page) + checkout_overview_page = CheckoutOverviewPage(authenticated_page) assert checkout_overview_page.is_loaded() with allure.step(f"Verify {product_name} is present in order"): @@ -119,6 +119,6 @@ def test_complete_checkout_flow(page, config, test_data, product_data, customer_ checkout_overview_page.click_finish() with allure.step("Verify order confirmation message"): - checkout_complete_page = CheckoutCompletePage(page) + checkout_complete_page = CheckoutCompletePage(authenticated_page) assert (checkout_complete_page.get_confirmation_message() == "Thank you for your order!") diff --git a/tests/e2e/test_inventory.py b/tests/e2e/test_inventory.py index 0d9bc77..43d71b9 100644 --- a/tests/e2e/test_inventory.py +++ b/tests/e2e/test_inventory.py @@ -1,5 +1,5 @@ from pages.inventory_page import InventoryPage -from pages.login_page import LoginPage +# from pages.login_page import LoginPage import pytest import allure import json @@ -28,25 +28,25 @@ def load_inventory_test_data(): @pytest.mark.smoke @pytest.mark.e2e @pytest.mark.parametrize("product_data", inventory_test_data.values(), ids=lambda product: product["name"]) -def test_add_product_to_cart(page, config, test_data, product_data): +def test_add_product_to_cart(authenticated_page, product_data): """ Verify that a user can add different products to the shopping cart. """ - with allure.step("Navigate to SauceDemo application"): - login_page = LoginPage(page) - login_page.navigate(config.get("application.base_url")) + # with allure.step("Navigate to SauceDemo application"): + # login_page = LoginPage(page) + # login_page.navigate(config.get("application.base_url")) - with allure.step("Retrieve valid user credentials"): - valid_user = test_data.get("users.valid_user")[0] - username = valid_user["username"] - password = valid_user["password"] + # with allure.step("Retrieve valid user credentials"): + # valid_user = test_data.get("users.valid_user")[0] + # username = valid_user["username"] + # password = valid_user["password"] - with allure.step("Login with valid credentials"): - login_page.login(username=username, password=password) + # with allure.step("Login with valid credentials"): + # login_page.login(username=username, password=password) with allure.step("Verify inventory page is loaded"): - inventory_page = InventoryPage(page) + inventory_page = InventoryPage(authenticated_page) assert inventory_page.is_loaded() with allure.step(f"Retrieve product name: {product_data['name']}"):