customer.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { expect } from "../setup";
  2. import {
  3. generateName,
  4. generateFirstName,
  5. generateLastName,
  6. generateEmail,
  7. generateDescription,
  8. } from "./faker";
  9. export async function register(page) {
  10. const credentials = {
  11. firstName: generateFirstName(),
  12. lastName: generateLastName(),
  13. email: generateEmail(),
  14. password: "admin123",
  15. };
  16. await page.goto("");
  17. await page.getByLabel("Profile").click();
  18. await page.getByRole("link", { name: "Sign Up" }).click();
  19. await page.getByPlaceholder("First Name").click();
  20. await page.getByPlaceholder("First Name").fill(credentials.firstName);
  21. await page.getByPlaceholder("Last Name").click();
  22. await page.getByPlaceholder("Last Name").fill(credentials.lastName);
  23. await page.getByPlaceholder("email@example.com").click();
  24. await page.getByPlaceholder("email@example.com").fill(credentials.email);
  25. await page.getByPlaceholder("Password", { exact: true }).click();
  26. await page
  27. .getByPlaceholder("Password", { exact: true })
  28. .fill(credentials.password);
  29. await page.getByPlaceholder("Confirm Password").click();
  30. await page.getByPlaceholder("Confirm Password").fill(credentials.password);
  31. const agreementLocator = page.locator("#agreement").nth(1);
  32. const isVisible = await agreementLocator.isVisible();
  33. if (isVisible) {
  34. await page.getByText("I agree with this statement.").click();
  35. }
  36. await page
  37. .locator("#main form div")
  38. .filter({ hasText: "Subscribe to newsletter" })
  39. .locator("label")
  40. .first()
  41. .click();
  42. await page.getByRole("button", { name: "Register" }).click();
  43. await expect(page.locator("body")).toContainText(
  44. "Account created successfully"
  45. );
  46. return credentials;
  47. }
  48. export async function loginAsCustomer(page) {
  49. const credentials = await register(page);
  50. await page.goto("");
  51. await page.getByLabel("Profile").click();
  52. await page.getByRole("link", { name: "Sign In" }).click();
  53. await page.getByPlaceholder("email@example.com").click();
  54. await page.getByPlaceholder("email@example.com").fill(credentials.email);
  55. await page.getByPlaceholder("email@example.com").press("Tab");
  56. await page.getByPlaceholder("Password").fill(credentials.password);
  57. const agreementLocator = page.locator("#agreement").nth(1);
  58. const isVisible = await agreementLocator.isVisible();
  59. if (isVisible) {
  60. await page.getByText("I agree with this statement.").click();
  61. }
  62. await page.getByRole("button", { name: "Sign In" }).click();
  63. return credentials;
  64. }
  65. export async function addAddress(page) {
  66. await page.getByLabel("Profile").click();
  67. await page.getByRole("link", { name: "Profile" }).click();
  68. await page.getByRole("link", { name: " Address " }).click();
  69. await page.getByRole("link", { name: "Add Address" }).click();
  70. await page.getByPlaceholder("Company Name").click();
  71. await page.getByPlaceholder("Company Name").fill(generateName());
  72. await page.getByPlaceholder("Company Name").press("Tab");
  73. await page.getByPlaceholder("First Name").fill(generateFirstName());
  74. await page.getByPlaceholder("First Name").press("Tab");
  75. await page.getByPlaceholder("Last Name").fill(generateLastName());
  76. await page.getByPlaceholder("Last Name").press("Tab");
  77. await page
  78. .getByPlaceholder("Email", { exact: true })
  79. .fill("test@example.com");
  80. await page.getByPlaceholder("Email", { exact: true }).press("Tab");
  81. await page.getByPlaceholder("Vat ID").press("Tab");
  82. await page.getByPlaceholder("Street Address").fill("Demo");
  83. await page.getByPlaceholder("Street Address").press("Tab");
  84. await page.getByLabel("Country").selectOption("DZ");
  85. await page.getByPlaceholder("State").click();
  86. await page.getByPlaceholder("State").fill("any");
  87. await page.getByPlaceholder("City").click();
  88. await page.getByPlaceholder("City").fill("any");
  89. await page.getByPlaceholder("Post Code").click();
  90. await page.getByPlaceholder("Post Code").fill("123456");
  91. await page.getByPlaceholder("Phone").click();
  92. await page.getByPlaceholder("Phone").fill("9876543210");
  93. await page
  94. .locator("#main form div")
  95. .filter({ hasText: "Set as Default" })
  96. .locator("label")
  97. .first()
  98. .click();
  99. await page
  100. .locator("#main form div")
  101. .filter({ hasText: "Set as Default" })
  102. .locator("label")
  103. .first()
  104. .click();
  105. await page.getByRole("button", { name: "Save" }).click();
  106. await expect(
  107. page.getByText("Address have been successfully added.").first()
  108. ).toBeVisible();
  109. }
  110. export async function addWishlist(page) {
  111. await page.locator(".action-items > span").first().click();
  112. await page
  113. .locator(
  114. "div:nth-child(9) > div:nth-child(2) > div > .-mt-9 > .action-items > span"
  115. )
  116. .first()
  117. .click();
  118. await expect(
  119. page.getByText("Item Successfully Added To Wishlist").first()
  120. ).toBeVisible();
  121. }
  122. export async function addReview(page) {
  123. const review = {
  124. title: generateName(),
  125. comment: generateDescription(),
  126. };
  127. await page
  128. .locator("#main div")
  129. .filter({ hasText: "New Products View All New" })
  130. .getByLabel("Arctic Touchscreen Winter")
  131. .click();
  132. await page.getByRole("button", { name: "Reviews" }).click();
  133. await page.locator("#review-tab").getByText("Write a Review").click();
  134. await page.locator("#review-tab span").nth(3).click();
  135. await page.locator("#review-tab span").nth(4).click();
  136. await page.getByPlaceholder("Title").fill(review.title);
  137. await page.getByPlaceholder("Comment").fill(review.comment);
  138. await page.getByRole("button", { name: "Submit Review" }).click();
  139. await expect(
  140. page.getByText("Review submitted successfully.").first()
  141. ).toBeVisible();
  142. return review;
  143. }