forked from saleor/storefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
61 lines (50 loc) · 2.42 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { expect, type Page } from "@playwright/test";
export async function clickOnRandomProductElement({ page }: { page: Page }) {
const productLinks = page.getByTestId("ProductElement");
await productLinks.first().waitFor();
const count = await productLinks.count();
const nth = Math.floor(Math.random() * count);
return clickOnNthProductElement({ page, nth });
}
export async function clickOnNthProductElement({ page, nth }: { page: Page; nth: number }) {
const productLinks = page.getByTestId("ProductElement");
await productLinks.first().waitFor();
const randomProductLink = productLinks.nth(nth);
const name = await randomProductLink.getByRole("heading").textContent();
const priceRange = await randomProductLink.getByTestId("ProductElement_PriceRange").textContent();
const category = await randomProductLink.getByTestId("ProductElement_Category").textContent();
await randomProductLink.click();
await page.waitForURL("**/products/*");
expect(name, "Missing product name").toBeTruthy();
expect(priceRange, "Missing product priceRange").toBeTruthy();
expect(category, "Missing product category").toBeTruthy();
return { name: name!, priceRange: priceRange!, category: category! };
}
export async function getCurrentProductPrice({ page }: { page: Page }) {
const price = await page.getByTestId("ProductElement_Price").textContent();
expect(price, "Missing product price").toBeTruthy();
return Number.parseFloat(price!.replace(/[^0-9\.]/g, ""));
}
export async function selectRandomAvailableVariant({ page }: { page: Page }) {
// some products only have a single variant so this block can throw and it's expected
try {
await page.getByTestId("VariantSelector").waitFor({ timeout: 1000 });
const variant = page.getByTestId("VariantSelector").getByRole("radio", { disabled: false });
const count = await variant.count();
if (count > 0) {
await variant.nth(Math.floor(Math.random() * count)).click();
}
} catch {}
await page.waitForURL(/\?variant=.+/);
}
export async function addCurrentProductToCart({ page }: { page: Page }) {
expect(page.url()).toContain("/products/");
expect(page.url()).toContain("?variant=");
const checkoutButton = page.getByRole("button", { name: "Add to cart" });
await checkoutButton.click();
await checkoutButton.isEnabled();
}
export async function openCart({ page }: { page: Page }) {
await page.getByTestId("CartNavItem").click();
await page.getByText("Your Shopping Cart").waitFor();
}