|
| 1 | +package qa.autotest.framework.steps; |
| 2 | + |
| 3 | +import io.qameta.allure.Step; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import qa.autotest.domain.dto.CheckoutDto; |
| 7 | +import qa.autotest.domain.enums.SauceDemoProduct; |
| 8 | +import qa.autotest.framework.config.TestConfig; |
| 9 | +import qa.autotest.framework.pages.CartPage; |
| 10 | +import qa.autotest.framework.pages.CheckoutCompletePage; |
| 11 | +import qa.autotest.framework.pages.CheckoutStepOnePage; |
| 12 | +import qa.autotest.framework.pages.CheckoutStepTwoPage; |
| 13 | +import qa.autotest.framework.pages.InventoryPage; |
| 14 | + |
| 15 | +/** |
| 16 | + * Business-level checkout steps. |
| 17 | + * |
| 18 | + * <p>Covers the full checkout funnel — from cart to confirmation — in a single |
| 19 | + * method call. Tests describe the <em>scenario under test</em> rather than |
| 20 | + * every page-navigation detail. |
| 21 | + * |
| 22 | + * <p>Config-sourced checkout data ({@code checkoutFirstName}, etc.) is read |
| 23 | + * once via the injected {@link TestConfig}. Tests that need custom data |
| 24 | + * pass an explicit {@link CheckoutDto}. |
| 25 | + * |
| 26 | + * <p>Stateless: safe under parallel execution. |
| 27 | + */ |
| 28 | +@Slf4j |
| 29 | +@RequiredArgsConstructor |
| 30 | +public class CheckoutSteps { |
| 31 | + |
| 32 | + private final TestConfig config; |
| 33 | + |
| 34 | + /** |
| 35 | + * Navigates from the cart page to {@link CheckoutStepOnePage}. |
| 36 | + * |
| 37 | + * @param cartPage loaded cart page |
| 38 | + * @return loaded {@link CheckoutStepOnePage} |
| 39 | + */ |
| 40 | + @Step("Proceed to checkout info page") |
| 41 | + public CheckoutStepOnePage proceedToCheckoutInfo(CartPage cartPage) { |
| 42 | + log.info("Proceeding from cart to checkout step one"); |
| 43 | + return cartPage.proceedToCheckout().waitForPageLoad(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Fills checkout info from config and proceeds to the overview page. |
| 48 | + * |
| 49 | + * @param checkoutInfoPage loaded checkout step-one page |
| 50 | + * @return loaded {@link CheckoutStepTwoPage} |
| 51 | + */ |
| 52 | + @Step("Fill checkout info from config and continue") |
| 53 | + public CheckoutStepTwoPage fillInfoAndContinue(CheckoutStepOnePage checkoutInfoPage) { |
| 54 | + log.info("Filling checkout info from config"); |
| 55 | + return checkoutInfoPage |
| 56 | + .fillCheckoutInfo( |
| 57 | + config.checkoutFirstName(), |
| 58 | + config.checkoutLastName(), |
| 59 | + config.checkoutZipCode()) |
| 60 | + .clickContinue() |
| 61 | + .waitForPageLoad(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Fills checkout info from the supplied DTO and proceeds to the overview page. |
| 66 | + * |
| 67 | + * @param checkoutInfoPage loaded checkout step-one page |
| 68 | + * @param checkoutDto checkout data |
| 69 | + * @return loaded {@link CheckoutStepTwoPage} |
| 70 | + */ |
| 71 | + @Step("Fill checkout info from DTO and continue") |
| 72 | + public CheckoutStepTwoPage fillInfoAndContinue(CheckoutStepOnePage checkoutInfoPage, |
| 73 | + CheckoutDto checkoutDto) { |
| 74 | + log.info("Filling checkout info: {} {} {}", |
| 75 | + checkoutDto.getFirstName(), checkoutDto.getLastName(), checkoutDto.getZipCode()); |
| 76 | + return checkoutInfoPage |
| 77 | + .fillCheckoutInfo(checkoutDto) |
| 78 | + .clickContinue() |
| 79 | + .waitForPageLoad(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Runs the complete checkout funnel: |
| 84 | + * add product → open cart → fill info (from config) → overview → finish. |
| 85 | + * |
| 86 | + * <p>Use when the test under verification is <em>after</em> checkout |
| 87 | + * completes (e.g. order confirmation, empty cart). |
| 88 | + * |
| 89 | + * @param inventoryPage active inventory page |
| 90 | + * @param product product to purchase |
| 91 | + * @return loaded {@link CheckoutCompletePage} |
| 92 | + */ |
| 93 | + @Step("Complete checkout with product: {product}") |
| 94 | + public CheckoutCompletePage completeCheckout(InventoryPage inventoryPage, |
| 95 | + SauceDemoProduct product) { |
| 96 | + log.info("Running full checkout for product: {}", product.getDisplayName()); |
| 97 | + return inventoryPage |
| 98 | + .addProductToCartByName(product) |
| 99 | + .openCart() |
| 100 | + .proceedToCheckout() |
| 101 | + .fillCheckoutInfo( |
| 102 | + config.checkoutFirstName(), |
| 103 | + config.checkoutLastName(), |
| 104 | + config.checkoutZipCode()) |
| 105 | + .clickContinue() |
| 106 | + .waitForPageLoad() |
| 107 | + .clickFinish() |
| 108 | + .waitForPageLoad(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Runs the complete checkout funnel with a custom {@link CheckoutDto}. |
| 113 | + * |
| 114 | + * @param inventoryPage active inventory page |
| 115 | + * @param product product to purchase |
| 116 | + * @param checkoutDto checkout data |
| 117 | + * @return loaded {@link CheckoutCompletePage} |
| 118 | + */ |
| 119 | + @Step("Complete checkout with product: {product}, info: {checkoutDto}") |
| 120 | + public CheckoutCompletePage completeCheckout(InventoryPage inventoryPage, |
| 121 | + SauceDemoProduct product, |
| 122 | + CheckoutDto checkoutDto) { |
| 123 | + log.info("Running full checkout for product: {} with custom info", product.getDisplayName()); |
| 124 | + return inventoryPage |
| 125 | + .addProductToCartByName(product) |
| 126 | + .openCart() |
| 127 | + .proceedToCheckout() |
| 128 | + .fillCheckoutInfo(checkoutDto) |
| 129 | + .clickContinue() |
| 130 | + .waitForPageLoad() |
| 131 | + .clickFinish() |
| 132 | + .waitForPageLoad(); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Navigates from inventory through cart to the overview page without |
| 137 | + * finishing the order. Use when the test verifies the overview page itself |
| 138 | + * (totals, item list). |
| 139 | + * |
| 140 | + * @param inventoryPage active inventory page |
| 141 | + * @param products one or more products to add |
| 142 | + * @return loaded {@link CheckoutStepTwoPage} |
| 143 | + */ |
| 144 | + @Step("Reach checkout overview with products: {products}") |
| 145 | + public CheckoutStepTwoPage reachCheckoutOverview(InventoryPage inventoryPage, |
| 146 | + SauceDemoProduct... products) { |
| 147 | + log.info("Reaching checkout overview with {} product(s)", products.length); |
| 148 | + for (SauceDemoProduct product : products) { |
| 149 | + inventoryPage.addProductToCartByName(product); |
| 150 | + } |
| 151 | + return inventoryPage |
| 152 | + .openCart() |
| 153 | + .proceedToCheckout() |
| 154 | + .fillCheckoutInfo( |
| 155 | + config.checkoutFirstName(), |
| 156 | + config.checkoutLastName(), |
| 157 | + config.checkoutZipCode()) |
| 158 | + .clickContinue() |
| 159 | + .waitForPageLoad(); |
| 160 | + } |
| 161 | +} |
0 commit comments