checkout-flow.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import { Page, expect } from "@playwright/test";
  2. import { WebLocators } from "../locators/locator";
  3. import fs from "fs";
  4. /**
  5. * Reads product data from JSON file
  6. */
  7. function readProductData() {
  8. const product = JSON.parse(fs.readFileSync("product-data.json", "utf-8"));
  9. const productName = product.name;
  10. return productName;
  11. }
  12. export class ProductCheckout {
  13. readonly page: Page;
  14. readonly locators: WebLocators;
  15. constructor(page: Page) {
  16. this.page = page;
  17. this.locators = new WebLocators(page);
  18. }
  19. /**
  20. * Common product search flow
  21. */
  22. async searchProduct(productName: string) {
  23. await this.page.goto("");
  24. await this.page.waitForLoadState("networkidle");
  25. await this.locators.searchInput.fill(productName);
  26. await this.locators.searchInput.press("Enter");
  27. }
  28. /**
  29. * Common steps to reach checkout page
  30. */
  31. async proceedToCheckout() {
  32. await this.locators.ShoppingCartIcon.click();
  33. await this.locators.ContinueButton.click();
  34. await this.page.locator(".icon-radio-unselect").first().click();
  35. await this.locators.clickProcessButton.click();
  36. }
  37. /**
  38. * Common place order flow
  39. */
  40. async placeOrder() {
  41. await this.page.waitForTimeout(2000);
  42. await this.locators.clickPlaceOrderButton.click();
  43. await this.page.waitForTimeout(8000);
  44. }
  45. /**
  46. * Common guest checkout flow for all products
  47. */
  48. async guestCheckoutCommon() {
  49. await this.locators.ShoppingCartIcon.click();
  50. await this.locators.ContinueButton.click();
  51. await this.locators.companyName.fill("Web");
  52. await this.locators.firstName.fill("demo");
  53. await this.locators.lastName.fill("guest");
  54. await this.locators.shippingEmail.fill("demo@example.com");
  55. await this.locators.streetAddress.fill("north street");
  56. await this.locators.billingCountry.selectOption({ value: "IN" });
  57. await this.locators.billingState.selectOption({ value: "UP" });
  58. await this.locators.billingCity.fill("test city");
  59. await this.locators.billingZip.fill("123456");
  60. await this.locators.billingTelephone.fill("2365432789");
  61. await this.locators.clickProcessButton.click();
  62. await this.locators.chooseShippingMethod.click();
  63. await this.locators.choosePaymentMethod.click();
  64. await this.placeOrder();
  65. }
  66. /**
  67. * Common checkout flow
  68. */
  69. async customerCheckout() {
  70. const productName = readProductData();
  71. await this.searchProduct(productName);
  72. await this.locators.addToCartButton.click();
  73. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  74. await this.proceedToCheckout();
  75. await this.locators.chooseShippingMethod.click();
  76. await this.locators.choosePaymentMethod.click();
  77. await this.placeOrder();
  78. }
  79. /**
  80. * Simple product checkout flow
  81. */
  82. async simpleCheckoutFlatRate() {
  83. const productName = readProductData();
  84. await this.searchProduct(productName);
  85. await this.locators.addToCartButton.click();
  86. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  87. await this.proceedToCheckout();
  88. await this.locators.chooseFlatShippingMeathod.click();
  89. await this.locators.choosePaymentMethod.click();
  90. await this.placeOrder();
  91. }
  92. async simpleCheckoutCOD() {
  93. const productName = readProductData();
  94. await this.searchProduct(productName);
  95. await this.locators.addToCartButton.click();
  96. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  97. await this.proceedToCheckout();
  98. await this.locators.chooseFlatShippingMeathod.click();
  99. await this.locators.choosePaymentMethodCOD.click();
  100. await this.placeOrder();
  101. }
  102. async shippingChangeCheckoutSimple() {
  103. const productName = readProductData();
  104. await this.searchProduct(productName);
  105. await this.locators.addToCartButton.click();
  106. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  107. await this.locators.ShoppingCartIcon.click();
  108. await this.locators.ContinueButton.click();
  109. await this.locators.addNewAddress.click();
  110. await this.locators.companyName.fill("Web");
  111. await this.locators.firstName.fill("demo");
  112. await this.locators.lastName.fill("guest");
  113. await this.locators.shippingEmail.fill("demo@example.com");
  114. await this.locators.streetAddress.fill("north street");
  115. await this.locators.billingCountry.selectOption({ value: "IN" });
  116. await this.locators.billingState.selectOption({ value: "UP" });
  117. await this.locators.billingCity.fill("test city");
  118. await this.locators.billingZip.fill("123456");
  119. await this.locators.billingTelephone.fill("2365432789");
  120. await this.locators.clickSaveAddressButton.click();
  121. await this.locators.clickProcessButton.click();
  122. await this.locators.chooseShippingMethod.click();
  123. await this.locators.choosePaymentMethod.click();
  124. await this.placeOrder();
  125. }
  126. async guestCheckoutSimple() {
  127. const productName = readProductData();
  128. await this.searchProduct(productName);
  129. await this.locators.addToCartButton.click();
  130. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  131. await this.locators.ShoppingCartIcon.click();
  132. await this.locators.ContinueButton.click();
  133. await this.locators.companyName.fill("Web");
  134. await this.locators.firstName.fill("demo");
  135. await this.locators.lastName.fill("guest");
  136. await this.locators.shippingEmail.fill("demo@example.com");
  137. await this.locators.streetAddress.fill("north street");
  138. await this.locators.billingCountry.selectOption({ value: "IN" });
  139. await this.locators.billingState.selectOption({ value: "UP" });
  140. await this.locators.billingCity.fill("test city");
  141. await this.locators.billingZip.fill("123456");
  142. await this.locators.billingTelephone.fill("2365432789");
  143. await this.locators.clickProcessButton.click();
  144. await this.locators.chooseShippingMethod.click();
  145. await this.locators.choosePaymentMethod.click();
  146. await this.placeOrder();
  147. }
  148. /**
  149. * Configurable product checkout flow
  150. */
  151. async configCheckout() {
  152. const productName = readProductData();
  153. await this.searchProduct(productName);
  154. await this.locators.addToCartButton.click();
  155. await this.page.getByLabel("Color").selectOption("4");
  156. await this.page.getByLabel("Size").selectOption("8");
  157. await this.locators.addToCartButton.click();
  158. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  159. await this.proceedToCheckout();
  160. await this.locators.chooseShippingMethod.click();
  161. await this.locators.choosePaymentMethod.click();
  162. await this.placeOrder();
  163. }
  164. async shippingChangeCheckoutConfig() {
  165. const productName = readProductData();
  166. await this.searchProduct(productName);
  167. await this.locators.addToCartButton.click();
  168. await this.page.getByLabel("Color").selectOption("4");
  169. await this.page.getByLabel("Size").selectOption("8");
  170. await this.locators.addToCartButton.click();
  171. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  172. await this.locators.ShoppingCartIcon.click();
  173. await this.locators.ContinueButton.click();
  174. await this.locators.addNewAddress.click();
  175. await this.locators.companyName.fill("Web");
  176. await this.locators.firstName.fill("demo");
  177. await this.locators.lastName.fill("guest");
  178. await this.locators.shippingEmail.fill("demo@example.com");
  179. await this.locators.streetAddress.fill("north street");
  180. await this.locators.billingCountry.selectOption({ value: "IN" });
  181. await this.locators.billingState.selectOption({ value: "UP" });
  182. await this.locators.billingCity.fill("test city");
  183. await this.locators.billingZip.fill("123456");
  184. await this.locators.billingTelephone.fill("2365432789");
  185. await this.locators.clickSaveAddressButton.click();
  186. await this.locators.clickProcessButton.click();
  187. await this.locators.chooseShippingMethod.click();
  188. await this.locators.choosePaymentMethod.click();
  189. await this.placeOrder();
  190. }
  191. async guestCheckoutConfigurable() {
  192. const productName = readProductData();
  193. await this.searchProduct(productName);
  194. await this.locators.addToCartButton.click();
  195. await this.page.getByLabel("Color").selectOption("4");
  196. await this.page.getByLabel("Size").selectOption("8");
  197. await this.locators.addToCartButton.click();
  198. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  199. await this.guestCheckoutCommon();
  200. }
  201. async configCheckoutFlatRate() {
  202. const productName = readProductData();
  203. await this.searchProduct(productName);
  204. await this.locators.addToCartButton.click();
  205. await this.page.getByLabel("Color").selectOption("4");
  206. await this.page.getByLabel("Size").selectOption("8");
  207. await this.locators.addToCartButton.click();
  208. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  209. await this.proceedToCheckout();
  210. await this.locators.chooseFlatShippingMeathod.click();
  211. await this.locators.choosePaymentMethod.click();
  212. await this.placeOrder();
  213. }
  214. async configCheckoutCOD() {
  215. const productName = readProductData();
  216. await this.searchProduct(productName);
  217. await this.locators.addToCartButton.click();
  218. await this.page.getByLabel("Color").selectOption("4");
  219. await this.page.getByLabel("Size").selectOption("8");
  220. await this.locators.addToCartButton.click();
  221. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  222. await this.proceedToCheckout();
  223. await this.locators.chooseFlatShippingMeathod.click();
  224. await this.locators.choosePaymentMethodCOD.click();
  225. await this.placeOrder();
  226. }
  227. /**
  228. * Booking product checkout flow
  229. */
  230. async bookingCheckout() {
  231. const productName = readProductData();
  232. await this.searchProduct(productName);
  233. await this.locators.addToCartButton.click();
  234. /**
  235. * Select NEXT SUNDAY
  236. */
  237. const today = new Date();
  238. const daysUntilSunday = (7 - today.getDay()) % 7 || 7;
  239. const nextSunday = new Date(today);
  240. nextSunday.setDate(today.getDate() + daysUntilSunday);
  241. /**
  242. * Format YYYY-MM-DD
  243. */
  244. const formattedSunday = nextSunday.toISOString().split("T")[0];
  245. const dateInput = this.page.locator('input[name="booking[date]"]');
  246. await dateInput.fill(formattedSunday);
  247. await dateInput.press("Enter");
  248. const slotSelect = this.page.locator('select[name="booking[slot]"]');
  249. await this.page.waitForTimeout(2000);
  250. await slotSelect.click();
  251. await slotSelect.press("ArrowDown");
  252. await dateInput.press("Enter");
  253. await this.locators.addToCartButton.click();
  254. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  255. await this.proceedToCheckout();
  256. await this.locators.choosePaymentMethod.click();
  257. await this.placeOrder();
  258. }
  259. /**
  260. * Downloadable product checkout flow
  261. */
  262. async downloadableCheckout() {
  263. const productName = readProductData();
  264. await this.searchProduct(productName);
  265. await this.locators.addToCartButton.click();
  266. await this.page.waitForTimeout(2000);
  267. await this.locators.clickLink.click();
  268. await this.locators.addToCartButton.click();
  269. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  270. await this.proceedToCheckout();
  271. await this.locators.choosePaymentMethod.click();
  272. await this.placeOrder();
  273. }
  274. /**
  275. * Group product checkout flow
  276. */
  277. async groupCheckout() {
  278. await this.searchProduct("group");
  279. await this.locators.addToCartButton.click();
  280. await this.page.waitForTimeout(3000);
  281. await this.locators.addToCartButton.click();
  282. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  283. await this.proceedToCheckout();
  284. await this.locators.chooseShippingMethod.click();
  285. await this.locators.choosePaymentMethod.click();
  286. await this.placeOrder();
  287. }
  288. async groupCheckoutFlatRate() {
  289. await this.searchProduct("group");
  290. await this.locators.addToCartButton.click();
  291. await this.page.waitForTimeout(3000);
  292. await this.locators.addToCartButton.click();
  293. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  294. await this.proceedToCheckout();
  295. await this.locators.chooseFlatShippingMeathod.click();
  296. await this.locators.choosePaymentMethod.click();
  297. await this.placeOrder();
  298. }
  299. async groupCheckoutCOD() {
  300. await this.searchProduct("group");
  301. await this.locators.addToCartButton.click();
  302. await this.page.waitForTimeout(3000);
  303. await this.locators.addToCartButton.click();
  304. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  305. await this.proceedToCheckout();
  306. await this.locators.chooseFlatShippingMeathod.click();
  307. await this.locators.choosePaymentMethodCOD.click();
  308. await this.placeOrder();
  309. }
  310. async guestCheckoutGroup() {
  311. const productName = readProductData();
  312. await this.searchProduct(productName);
  313. await this.locators.addToCartButton.click();
  314. await this.page.waitForTimeout(3000);
  315. await this.locators.addToCartButton.click();
  316. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  317. await this.guestCheckoutCommon();
  318. }
  319. /**
  320. * Virtual product checkout flow
  321. */
  322. async virtualCheckout() {
  323. const productName = readProductData();
  324. await this.searchProduct(productName);
  325. await this.locators.addToCartButton.click();
  326. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  327. await this.proceedToCheckout();
  328. await this.locators.choosePaymentMethod.click();
  329. await this.placeOrder();
  330. }
  331. /**
  332. * Bundle product checkout flow
  333. */
  334. async bundleCheckout() {
  335. const productName = readProductData();
  336. await this.searchProduct(productName);
  337. await this.locators.addToCartButton.click();
  338. await this.page.waitForLoadState("networkidle");
  339. await this.locators.addToCartButton.click();
  340. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  341. await this.proceedToCheckout();
  342. await this.locators.chooseShippingMethod.click();
  343. await this.locators.choosePaymentMethod.click();
  344. await this.placeOrder();
  345. }
  346. async bundleCheckoutFlatRate() {
  347. const productName = readProductData();
  348. await this.searchProduct(productName);
  349. await this.locators.addToCartButton.click();
  350. await this.page.waitForLoadState("networkidle");
  351. await this.locators.addToCartButton.click();
  352. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  353. await this.proceedToCheckout();
  354. await this.locators.chooseFlatShippingMeathod.click();
  355. await this.locators.choosePaymentMethod.click();
  356. await this.placeOrder();
  357. }
  358. async bundleCheckoutCOD() {
  359. const productName = readProductData();
  360. await this.searchProduct(productName);
  361. await this.locators.addToCartButton.click();
  362. await this.page.waitForLoadState("networkidle");
  363. await this.locators.addToCartButton.click();
  364. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  365. await this.proceedToCheckout();
  366. await this.locators.chooseFlatShippingMeathod.click();
  367. await this.locators.choosePaymentMethodCOD.click();
  368. await this.placeOrder();
  369. }
  370. async guestCheckoutBundle() {
  371. const productName = readProductData();
  372. await this.searchProduct(productName);
  373. await this.locators.addToCartButton.click();
  374. await this.page.waitForLoadState("networkidle");
  375. await this.locators.addToCartButton.click();
  376. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  377. await this.guestCheckoutCommon();
  378. }
  379. async shippingChangeCheckoutBundle() {
  380. const productName = readProductData();
  381. await this.searchProduct(productName);
  382. await this.locators.addToCartButton.click();
  383. await this.page.waitForLoadState("networkidle");
  384. await this.locators.addToCartButton.click();
  385. await expect(this.locators.addCartSuccess.first()).toBeVisible();
  386. await this.locators.ShoppingCartIcon.click();
  387. await this.locators.ContinueButton.click();
  388. await this.locators.addNewAddress.click();
  389. await this.locators.companyName.fill("Web");
  390. await this.locators.firstName.fill("demo");
  391. await this.locators.lastName.fill("guest");
  392. await this.locators.shippingEmail.fill("demo@example.com");
  393. await this.locators.streetAddress.fill("north street");
  394. await this.locators.billingCountry.selectOption({ value: "IN" });
  395. await this.locators.billingState.selectOption({ value: "UP" });
  396. await this.locators.billingCity.fill("test city");
  397. await this.locators.billingZip.fill("123456");
  398. await this.locators.billingTelephone.fill("2365432789");
  399. await this.locators.clickSaveAddressButton.click();
  400. await this.locators.clickProcessButton.click();
  401. await this.locators.chooseShippingMethod.click();
  402. await this.locators.choosePaymentMethod.click();
  403. await this.placeOrder();
  404. }
  405. }