| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- import { Page, expect } from "@playwright/test";
- import { WebLocators } from "../locators/locator";
- import fs from "fs";
- /**
- * Reads product data from JSON file
- */
- function readProductData() {
- const product = JSON.parse(fs.readFileSync("product-data.json", "utf-8"));
- const productName = product.name;
- return productName;
- }
- export class ProductCheckout {
- readonly page: Page;
- readonly locators: WebLocators;
- constructor(page: Page) {
- this.page = page;
- this.locators = new WebLocators(page);
- }
- /**
- * Common product search flow
- */
- async searchProduct(productName: string) {
- await this.page.goto("");
- await this.page.waitForLoadState("networkidle");
- await this.locators.searchInput.fill(productName);
- await this.locators.searchInput.press("Enter");
- }
- /**
- * Common steps to reach checkout page
- */
- async proceedToCheckout() {
- await this.locators.ShoppingCartIcon.click();
- await this.locators.ContinueButton.click();
- await this.page.locator(".icon-radio-unselect").first().click();
- await this.locators.clickProcessButton.click();
- }
- /**
- * Common place order flow
- */
- async placeOrder() {
- await this.page.waitForTimeout(2000);
- await this.locators.clickPlaceOrderButton.click();
- await this.page.waitForTimeout(8000);
- }
- /**
- * Common guest checkout flow for all products
- */
- async guestCheckoutCommon() {
- await this.locators.ShoppingCartIcon.click();
- await this.locators.ContinueButton.click();
- await this.locators.companyName.fill("Web");
- await this.locators.firstName.fill("demo");
- await this.locators.lastName.fill("guest");
- await this.locators.shippingEmail.fill("demo@example.com");
- await this.locators.streetAddress.fill("north street");
- await this.locators.billingCountry.selectOption({ value: "IN" });
- await this.locators.billingState.selectOption({ value: "UP" });
- await this.locators.billingCity.fill("test city");
- await this.locators.billingZip.fill("123456");
- await this.locators.billingTelephone.fill("2365432789");
- await this.locators.clickProcessButton.click();
- await this.locators.chooseShippingMethod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- /**
- * Common checkout flow
- */
- async customerCheckout() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseShippingMethod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- /**
- * Simple product checkout flow
- */
- async simpleCheckoutFlatRate() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseFlatShippingMeathod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- async simpleCheckoutCOD() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseFlatShippingMeathod.click();
- await this.locators.choosePaymentMethodCOD.click();
- await this.placeOrder();
- }
- async shippingChangeCheckoutSimple() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.locators.ShoppingCartIcon.click();
- await this.locators.ContinueButton.click();
- await this.locators.addNewAddress.click();
- await this.locators.companyName.fill("Web");
- await this.locators.firstName.fill("demo");
- await this.locators.lastName.fill("guest");
- await this.locators.shippingEmail.fill("demo@example.com");
- await this.locators.streetAddress.fill("north street");
- await this.locators.billingCountry.selectOption({ value: "IN" });
- await this.locators.billingState.selectOption({ value: "UP" });
- await this.locators.billingCity.fill("test city");
- await this.locators.billingZip.fill("123456");
- await this.locators.billingTelephone.fill("2365432789");
- await this.locators.clickSaveAddressButton.click();
- await this.locators.clickProcessButton.click();
- await this.locators.chooseShippingMethod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- async guestCheckoutSimple() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.locators.ShoppingCartIcon.click();
- await this.locators.ContinueButton.click();
- await this.locators.companyName.fill("Web");
- await this.locators.firstName.fill("demo");
- await this.locators.lastName.fill("guest");
- await this.locators.shippingEmail.fill("demo@example.com");
- await this.locators.streetAddress.fill("north street");
- await this.locators.billingCountry.selectOption({ value: "IN" });
- await this.locators.billingState.selectOption({ value: "UP" });
- await this.locators.billingCity.fill("test city");
- await this.locators.billingZip.fill("123456");
- await this.locators.billingTelephone.fill("2365432789");
- await this.locators.clickProcessButton.click();
- await this.locators.chooseShippingMethod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- /**
- * Configurable product checkout flow
- */
- async configCheckout() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.getByLabel("Color").selectOption("4");
- await this.page.getByLabel("Size").selectOption("8");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseShippingMethod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- async shippingChangeCheckoutConfig() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.getByLabel("Color").selectOption("4");
- await this.page.getByLabel("Size").selectOption("8");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.locators.ShoppingCartIcon.click();
- await this.locators.ContinueButton.click();
- await this.locators.addNewAddress.click();
- await this.locators.companyName.fill("Web");
- await this.locators.firstName.fill("demo");
- await this.locators.lastName.fill("guest");
- await this.locators.shippingEmail.fill("demo@example.com");
- await this.locators.streetAddress.fill("north street");
- await this.locators.billingCountry.selectOption({ value: "IN" });
- await this.locators.billingState.selectOption({ value: "UP" });
- await this.locators.billingCity.fill("test city");
- await this.locators.billingZip.fill("123456");
- await this.locators.billingTelephone.fill("2365432789");
- await this.locators.clickSaveAddressButton.click();
- await this.locators.clickProcessButton.click();
- await this.locators.chooseShippingMethod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- async guestCheckoutConfigurable() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.getByLabel("Color").selectOption("4");
- await this.page.getByLabel("Size").selectOption("8");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.guestCheckoutCommon();
- }
- async configCheckoutFlatRate() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.getByLabel("Color").selectOption("4");
- await this.page.getByLabel("Size").selectOption("8");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseFlatShippingMeathod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- async configCheckoutCOD() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.getByLabel("Color").selectOption("4");
- await this.page.getByLabel("Size").selectOption("8");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseFlatShippingMeathod.click();
- await this.locators.choosePaymentMethodCOD.click();
- await this.placeOrder();
- }
- /**
- * Booking product checkout flow
- */
- async bookingCheckout() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- /**
- * Select NEXT SUNDAY
- */
- const today = new Date();
- const daysUntilSunday = (7 - today.getDay()) % 7 || 7;
- const nextSunday = new Date(today);
- nextSunday.setDate(today.getDate() + daysUntilSunday);
- /**
- * Format YYYY-MM-DD
- */
- const formattedSunday = nextSunday.toISOString().split("T")[0];
- const dateInput = this.page.locator('input[name="booking[date]"]');
- await dateInput.fill(formattedSunday);
- await dateInput.press("Enter");
- const slotSelect = this.page.locator('select[name="booking[slot]"]');
- await this.page.waitForTimeout(2000);
- await slotSelect.click();
- await slotSelect.press("ArrowDown");
- await dateInput.press("Enter");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- /**
- * Downloadable product checkout flow
- */
- async downloadableCheckout() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.waitForTimeout(2000);
- await this.locators.clickLink.click();
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- /**
- * Group product checkout flow
- */
- async groupCheckout() {
- await this.searchProduct("group");
- await this.locators.addToCartButton.click();
- await this.page.waitForTimeout(3000);
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseShippingMethod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- async groupCheckoutFlatRate() {
- await this.searchProduct("group");
- await this.locators.addToCartButton.click();
- await this.page.waitForTimeout(3000);
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseFlatShippingMeathod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- async groupCheckoutCOD() {
- await this.searchProduct("group");
- await this.locators.addToCartButton.click();
- await this.page.waitForTimeout(3000);
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseFlatShippingMeathod.click();
- await this.locators.choosePaymentMethodCOD.click();
- await this.placeOrder();
- }
- async guestCheckoutGroup() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.waitForTimeout(3000);
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.guestCheckoutCommon();
- }
- /**
- * Virtual product checkout flow
- */
- async virtualCheckout() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- /**
- * Bundle product checkout flow
- */
- async bundleCheckout() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.waitForLoadState("networkidle");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseShippingMethod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- async bundleCheckoutFlatRate() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.waitForLoadState("networkidle");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseFlatShippingMeathod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- async bundleCheckoutCOD() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.waitForLoadState("networkidle");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.proceedToCheckout();
- await this.locators.chooseFlatShippingMeathod.click();
- await this.locators.choosePaymentMethodCOD.click();
- await this.placeOrder();
- }
-
- async guestCheckoutBundle() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.waitForLoadState("networkidle");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.guestCheckoutCommon();
- }
- async shippingChangeCheckoutBundle() {
- const productName = readProductData();
- await this.searchProduct(productName);
- await this.locators.addToCartButton.click();
- await this.page.waitForLoadState("networkidle");
- await this.locators.addToCartButton.click();
- await expect(this.locators.addCartSuccess.first()).toBeVisible();
- await this.locators.ShoppingCartIcon.click();
- await this.locators.ContinueButton.click();
- await this.locators.addNewAddress.click();
- await this.locators.companyName.fill("Web");
- await this.locators.firstName.fill("demo");
- await this.locators.lastName.fill("guest");
- await this.locators.shippingEmail.fill("demo@example.com");
- await this.locators.streetAddress.fill("north street");
- await this.locators.billingCountry.selectOption({ value: "IN" });
- await this.locators.billingState.selectOption({ value: "UP" });
- await this.locators.billingCity.fill("test city");
- await this.locators.billingZip.fill("123456");
- await this.locators.billingTelephone.fill("2365432789");
- await this.locators.clickSaveAddressButton.click();
- await this.locators.clickProcessButton.click();
- await this.locators.chooseShippingMethod.click();
- await this.locators.choosePaymentMethod.click();
- await this.placeOrder();
- }
- }
|