nextjs-starter-medusa

Форк
0
526 строк · 22.1 Кб
1
import { seedDiscount, seedUser } from "../../data/seed"
2
import { test, expect } from "../../index"
3

4
test.describe("Discount tests", async () => {
5
  let discount = {
6
    id: "",
7
    code: "",
8
    rule_id: "",
9
    amount: 0,
10
  }
11
  test.beforeEach(async () => {
12
    discount = await seedDiscount()
13
  })
14

15
  test("Make sure discount works during transaction", async ({
16
    cartPage,
17
    checkoutPage,
18
    orderPage,
19
    productPage,
20
    storePage,
21
  }) => {
22
    let cartSubtotal = 0
23
    await test.step("Go through purchasing process, upto the cart page", async () => {
24
      await test.step("Navigate to a product page", async () => {
25
        await storePage.goto()
26
        const product = await storePage.getProduct("Sweatshirt")
27
        await product.locator.click()
28
        await productPage.container.waitFor({ state: "visible" })
29
      })
30

31
      await test.step("Add the product to the cart and goto checkout", async () => {
32
        await productPage.selectOption("M")
33
        await productPage.clickAddProduct()
34
        await productPage.cartDropdown.navCartLink.click()
35
        await productPage.cartDropdown.goToCartButton.click()
36
        await cartPage.container.waitFor({ state: "visible" })
37
        await cartPage.cartDropdown.close()
38
        cartSubtotal = Number(
39
          (await cartPage.cartTotal.getAttribute("data-value")) || ""
40
        )
41
      })
42
      await test.step("Navigate to the checkout page", async () => {
43
        await cartPage.checkoutButton.click()
44
        await checkoutPage.container.waitFor({ state: "visible" })
45
      })
46
    })
47

48
    await test.step("Enter in the discount and assert value works", async () => {
49
      await checkoutPage.discountButton.click()
50
      await expect(checkoutPage.discountInput).toBeVisible()
51
      await checkoutPage.discountInput.fill(discount.code)
52
      await checkoutPage.discountApplyButton.click()
53
      const paymentDiscount = await checkoutPage.getDiscount(discount.code)
54
      await expect(paymentDiscount.locator).toBeVisible()
55
      await expect(paymentDiscount.code).toHaveText(discount.code)
56
      expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
57
      expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
58
        (cartSubtotal - discount.amount).toString()
59
      )
60
    })
61

62
    let shippingTotal = 0
63
    await test.step("Go through checkout process", async () => {
64
      await test.step("Enter in the first step of the checkout process", async () => {
65
        await test.step("Enter in the shipping address info", async () => {
66
          await checkoutPage.shippingFirstNameInput.fill("First")
67
          await checkoutPage.shippingLastNameInput.fill("Last")
68
          await checkoutPage.shippingCompanyInput.fill("MyCorp")
69
          await checkoutPage.shippingAddressInput.fill("123 Fake street")
70
          await checkoutPage.shippingPostalCodeInput.fill("80010")
71
          await checkoutPage.shippingCityInput.fill("Denver")
72
          await checkoutPage.shippingProvinceInput.fill("Colorado")
73
          await checkoutPage.shippingCountrySelect.selectOption("United States")
74
        })
75

76
        await test.step("Enter in the contact info and open the billing info form", async () => {
77
          await checkoutPage.shippingEmailInput.fill("test@example.com")
78
          await checkoutPage.shippingPhoneInput.fill("3031112222")
79
          await checkoutPage.submitAddressButton.click()
80
        })
81
      })
82

83
      await test.step("Complete the rest of the payment process", async () => {
84
        await checkoutPage.selectDeliveryOption("FakeEx Standard")
85
        await checkoutPage.submitDeliveryOptionButton.click()
86
        shippingTotal = Number(
87
          (await checkoutPage.cartShipping.getAttribute("data-value")) || "0"
88
        )
89
        await checkoutPage.submitPaymentButton.click()
90
      })
91

92
      await test.step("Make sure the cart total is the expected value after selecting shipping", async () => {
93
        expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
94
          (cartSubtotal - discount.amount + shippingTotal).toString()
95
        )
96
      })
97

98
      await test.step("Finish completing the order", async () => {
99
        await checkoutPage.submitOrderButton.click()
100
        await orderPage.container.waitFor({ state: "visible" })
101
      })
102
    })
103
    const cartTotal = Number(cartSubtotal) + Number(shippingTotal)
104

105
    await test.step("Assert the order page shows the total was 0", async () => {
106
      expect(await orderPage.cartTotal.getAttribute("data-value")).toBe(
107
        (cartTotal - discount.amount).toString()
108
      )
109
      expect(await orderPage.cartSubtotal.getAttribute("data-value")).toBe(
110
        cartSubtotal.toString()
111
      )
112
      expect(await orderPage.cartDiscount.getAttribute("data-value")).toBe(
113
        discount.amount.toString()
114
      )
115
    })
116
  })
117

118
  test("Make sure discount can be used when entered in from cart", async ({
119
    cartPage,
120
    checkoutPage,
121
    orderPage,
122
    productPage,
123
    storePage,
124
  }) => {
125
    let cartSubtotal = 0
126
    await test.step("Go through purchasing process, upto the cart page", async () => {
127
      await test.step("Navigate to a product page", async () => {
128
        await storePage.goto()
129
        const product = await storePage.getProduct("Sweatshirt")
130
        await product.locator.click()
131
        await productPage.container.waitFor({ state: "visible" })
132
      })
133

134
      await test.step("Add the product to the cart and goto checkout", async () => {
135
        await productPage.selectOption("M")
136
        await productPage.clickAddProduct()
137
        await productPage.cartDropdown.navCartLink.click()
138
        await productPage.cartDropdown.goToCartButton.click()
139
        await cartPage.container.waitFor({ state: "visible" })
140
        await cartPage.cartDropdown.close()
141
        cartSubtotal = Number(
142
          (await cartPage.cartTotal.getAttribute("data-value")) || ""
143
        )
144
      })
145
    })
146

147
    await test.step("Enter in the discount and assert value works", async () => {
148
      await cartPage.discountButton.click()
149
      await expect(cartPage.discountInput).toBeVisible()
150
      await cartPage.discountInput.fill(discount.code)
151
      await cartPage.discountApplyButton.click()
152
      const paymentDiscount = await cartPage.getDiscount(discount.code)
153
      await expect(paymentDiscount.locator).toBeVisible()
154
      await expect(paymentDiscount.code).toHaveText(discount.code)
155
      expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
156
      expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
157
        (cartSubtotal - discount.amount).toString()
158
      )
159
    })
160

161
    await test.step("Go to checkout and assert the value is still discounted", async () => {
162
      await cartPage.checkoutButton.click()
163
      await checkoutPage.container.waitFor({ state: "visible" })
164
      expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
165
        (cartSubtotal - discount.amount).toString()
166
      )
167
    })
168

169
    let shippingTotal = 0
170
    await test.step("Go through checkout process", async () => {
171
      await test.step("Enter in the first step of the checkout process", async () => {
172
        await test.step("Enter in the shipping address info", async () => {
173
          await checkoutPage.shippingFirstNameInput.fill("First")
174
          await checkoutPage.shippingLastNameInput.fill("Last")
175
          await checkoutPage.shippingCompanyInput.fill("MyCorp")
176
          await checkoutPage.shippingAddressInput.fill("123 Fake street")
177
          await checkoutPage.shippingPostalCodeInput.fill("80010")
178
          await checkoutPage.shippingCityInput.fill("Denver")
179
          await checkoutPage.shippingProvinceInput.fill("Colorado")
180
          await checkoutPage.shippingCountrySelect.selectOption("United States")
181
        })
182

183
        await test.step("Enter in the contact info and open the billing info form", async () => {
184
          await checkoutPage.shippingEmailInput.fill("test@example.com")
185
          await checkoutPage.shippingPhoneInput.fill("3031112222")
186
          await checkoutPage.submitAddressButton.click()
187
        })
188
      })
189

190
      await test.step("Complete the rest of the payment process", async () => {
191
        await checkoutPage.selectDeliveryOption("FakeEx Standard")
192
        await checkoutPage.submitDeliveryOptionButton.click()
193
        shippingTotal = Number(
194
          (await checkoutPage.cartShipping.getAttribute("data-value")) || "0"
195
        )
196
        await checkoutPage.submitPaymentButton.click()
197
        await checkoutPage.submitOrderButton.click()
198
        await orderPage.container.waitFor({ state: "visible" })
199
      })
200
    })
201
    const cartTotal = Number(cartSubtotal) + Number(shippingTotal)
202

203
    await test.step("Assert the order page shows the total was 0", async () => {
204
      expect(await orderPage.cartTotal.getAttribute("data-value")).toBe(
205
        (cartTotal - discount.amount).toString()
206
      )
207
      expect(await orderPage.cartSubtotal.getAttribute("data-value")).toBe(
208
        cartSubtotal.toString()
209
      )
210
      expect(await orderPage.cartDiscount.getAttribute("data-value")).toBe(
211
        discount.amount.toString()
212
      )
213
    })
214
  })
215

216
  test("Ensure adding and removing a discout does not impact checkout amount", async ({
217
    cartPage,
218
    checkoutPage,
219
    orderPage,
220
    productPage,
221
    storePage,
222
  }) => {
223
    let cartSubtotal = 0
224
    await test.step("Go through purchasing process, upto the cart page", async () => {
225
      await test.step("Navigate to a product page", async () => {
226
        await storePage.goto()
227
        const product = await storePage.getProduct("Sweatshirt")
228
        await product.locator.click()
229
        await productPage.container.waitFor({ state: "visible" })
230
      })
231

232
      await test.step("Add the product to the cart and goto checkout", async () => {
233
        await productPage.selectOption("M")
234
        await productPage.clickAddProduct()
235
        await productPage.cartDropdown.navCartLink.click()
236
        await productPage.cartDropdown.goToCartButton.click()
237
        await cartPage.container.waitFor({ state: "visible" })
238
        await cartPage.cartDropdown.close()
239
        cartSubtotal = Number(
240
          (await cartPage.cartTotal.getAttribute("data-value")) || ""
241
        )
242
      })
243
    })
244

245
    await test.step("Enter in the discount and assert value works", async () => {
246
      await cartPage.discountButton.click()
247
      await expect(cartPage.discountInput).toBeVisible()
248
      await cartPage.discountInput.fill(discount.code)
249
      await cartPage.discountApplyButton.click()
250
      const paymentDiscount = await cartPage.getDiscount(discount.code)
251
      await expect(paymentDiscount.locator).toBeVisible()
252
      await expect(paymentDiscount.code).toHaveText(discount.code)
253
      expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
254
      expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
255
        (cartSubtotal - discount.amount).toString()
256
      )
257
    })
258

259
    await test.step("Go to checkout and assert the value is still discounted", async () => {
260
      await cartPage.checkoutButton.click()
261
      await checkoutPage.container.waitFor({ state: "visible" })
262
      expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
263
        (cartSubtotal - discount.amount).toString()
264
      )
265
      const paymentDiscount = await checkoutPage.getDiscount(discount.code)
266
      await paymentDiscount.removeButton.click()
267
      await expect(paymentDiscount.locator).not.toBeVisible()
268
      expect(await checkoutPage.cartTotal.getAttribute("data-value")).not.toBe(
269
        (cartSubtotal - discount.amount).toString()
270
      )
271
    })
272

273
    let shippingTotal = ""
274
    await test.step("Go through checkout process", async () => {
275
      await test.step("Enter in the first step of the checkout process", async () => {
276
        await test.step("Enter in the shipping address info", async () => {
277
          await checkoutPage.shippingFirstNameInput.fill("First")
278
          await checkoutPage.shippingLastNameInput.fill("Last")
279
          await checkoutPage.shippingCompanyInput.fill("MyCorp")
280
          await checkoutPage.shippingAddressInput.fill("123 Fake street")
281
          await checkoutPage.shippingPostalCodeInput.fill("80010")
282
          await checkoutPage.shippingCityInput.fill("Denver")
283
          await checkoutPage.shippingProvinceInput.fill("Colorado")
284
          await checkoutPage.shippingCountrySelect.selectOption("United States")
285
        })
286

287
        await test.step("Enter in the contact info and open the billing info form", async () => {
288
          await checkoutPage.shippingEmailInput.fill("test@example.com")
289
          await checkoutPage.shippingPhoneInput.fill("3031112222")
290
          await checkoutPage.submitAddressButton.click()
291
        })
292
      })
293

294
      await test.step("Complete the rest of the payment process", async () => {
295
        await checkoutPage.selectDeliveryOption("FakeEx Standard")
296
        await checkoutPage.submitDeliveryOptionButton.click()
297
        shippingTotal =
298
          (await checkoutPage.cartShipping.getAttribute("data-value")) || ""
299
        await checkoutPage.submitPaymentButton.click()
300
        await checkoutPage.submitOrderButton.click()
301
        await orderPage.container.waitFor({ state: "visible" })
302
      })
303
    })
304
    const cartTotal = (Number(cartSubtotal) + Number(shippingTotal)).toString()
305

306
    await test.step("Assert the order page shows the total was not discounted", async () => {
307
      expect(await orderPage.cartTotal.getAttribute("data-value")).toBe(
308
        cartTotal
309
      )
310
      expect(await orderPage.cartSubtotal.getAttribute("data-value")).toBe(
311
        cartSubtotal.toString()
312
      )
313
    })
314
  })
315

316
  test("Make sure a fake discount displays an error message on the cart page", async ({
317
    cartPage,
318
    productPage,
319
    storePage,
320
  }) => {
321
    await test.step("Go through purchasing process, upto the cart page", async () => {
322
      await test.step("Navigate to a product page", async () => {
323
        await storePage.goto()
324
        const product = await storePage.getProduct("Sweatshirt")
325
        await product.locator.click()
326
        await productPage.container.waitFor({ state: "visible" })
327
      })
328

329
      await test.step("Add the product to the cart and goto checkout", async () => {
330
        await productPage.selectOption("M")
331
        await productPage.clickAddProduct()
332
        await productPage.cartDropdown.navCartLink.click()
333
        await productPage.cartDropdown.goToCartButton.click()
334
        await cartPage.container.waitFor({ state: "visible" })
335
        await cartPage.cartDropdown.close()
336
      })
337
    })
338
    await test.step("Enter in the fake discount", async () => {
339
      await cartPage.discountButton.click()
340
      await expect(cartPage.discountInput).toBeVisible()
341
      await cartPage.discountInput.fill("__FAKE_DISCOUNT_DNE_1111111")
342
      await cartPage.discountApplyButton.click()
343
      await expect(cartPage.discountErrorMessage).toBeVisible()
344
    })
345
  })
346

347
  test("Make sure a fake discount displays an error message on the checkout page", async ({
348
    cartPage,
349
    checkoutPage,
350
    productPage,
351
    storePage,
352
  }) => {
353
    await test.step("Go through purchasing process, upto the cart page", async () => {
354
      await test.step("Navigate to a product page", async () => {
355
        await storePage.goto()
356
        const product = await storePage.getProduct("Sweatshirt")
357
        await product.locator.highlight()
358
        await product.locator.click()
359
        await productPage.container.waitFor({ state: "visible" })
360
      })
361

362
      await test.step("Add the product to the cart and goto checkout", async () => {
363
        await productPage.selectOption("M")
364
        await productPage.clickAddProduct()
365
        await productPage.cartDropdown.navCartLink.click()
366
        await productPage.cartDropdown.goToCartButton.click()
367
        await cartPage.container.waitFor({ state: "visible" })
368
        await cartPage.cartDropdown.close()
369
        await cartPage.checkoutButton.click()
370
        await checkoutPage.container.waitFor({ state: "visible" })
371
      })
372
    })
373
    await test.step("Enter in the fake discount", async () => {
374
      await checkoutPage.discountButton.click()
375
      await expect(checkoutPage.discountInput).toBeVisible()
376
      await checkoutPage.discountInput.fill("__FAKE_DISCOUNT_DNE_1111111")
377
      await checkoutPage.discountApplyButton.click()
378
      await expect(checkoutPage.discountErrorMessage).toBeVisible()
379
    })
380
  })
381

382
  test("Adding a discount and then accessing the cart at a later point keeps the discount amount", async ({
383
    cartPage,
384
    checkoutPage,
385
    productPage,
386
    storePage,
387
  }) => {
388
    let cartSubtotal = 0
389
    await test.step("Go through purchasing process, upto the cart page", async () => {
390
      await test.step("Navigate to a product page", async () => {
391
        await storePage.goto()
392
        const product = await storePage.getProduct("Sweatshirt")
393
        await product.locator.click()
394
        await productPage.container.waitFor({ state: "visible" })
395
      })
396

397
      await test.step("Add the product to the cart and goto checkout", async () => {
398
        await productPage.selectOption("M")
399
        await productPage.clickAddProduct()
400
        await productPage.cartDropdown.navCartLink.click()
401
        await productPage.cartDropdown.goToCartButton.click()
402
        await cartPage.container.waitFor({ state: "visible" })
403
        await cartPage.cartDropdown.close()
404
        cartSubtotal = Number(
405
          (await cartPage.cartTotal.getAttribute("data-value")) || ""
406
        )
407
      })
408
    })
409

410
    await test.step("Enter in the giftcard and assert value works", async () => {
411
      await cartPage.discountButton.click()
412
      await cartPage.discountInput.fill(discount.code)
413
      await cartPage.discountApplyButton.click()
414
      const paymentDiscount = await cartPage.getDiscount(discount.code)
415
      expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
416
      expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
417
        (cartSubtotal - discount.amount).toString()
418
      )
419
    })
420

421
    await test.step("Navigate away from the cart page and return to it", async () => {
422
      await storePage.goto()
423
      const product = await storePage.getProduct("Sweatpants")
424
      await product.locator.click()
425
      await productPage.container.waitFor({ state: "visible" })
426
      await cartPage.goto()
427
      await cartPage.cartDropdown.close()
428
    })
429

430
    await test.step("Verify the giftcard is still on the cart page", async () => {
431
      const paymentDiscount = await cartPage.getDiscount(discount.code)
432
      await expect(paymentDiscount.locator).toBeVisible()
433
      await expect(paymentDiscount.code).toContainText(discount.code)
434
      expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
435
        (cartSubtotal - discount.amount).toString()
436
      )
437
    })
438

439
    await test.step("Verify the giftcard is still on the checkout page", async () => {
440
      await cartPage.checkoutButton.click()
441
      await checkoutPage.container.waitFor({ state: "visible" })
442
      const paymentDiscount = await checkoutPage.getDiscount(discount.code)
443
      await expect(paymentDiscount.locator).toBeVisible()
444
      await expect(paymentDiscount.code).toContainText(discount.code)
445
      expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
446
        (cartSubtotal - discount.amount).toString()
447
      )
448
      expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
449
    })
450
  })
451

452
  test("Adding a discount and then adding another item to the cart keeps the discount", async ({
453
    cartPage,
454
    checkoutPage,
455
    productPage,
456
    storePage,
457
  }) => {
458
    let cartSubtotal = 0
459
    await test.step("Go through purchasing process, upto the cart page", async () => {
460
      await test.step("Navigate to a product page", async () => {
461
        await storePage.goto()
462
        const product = await storePage.getProduct("Sweatshirt")
463
        await product.locator.click()
464
        await productPage.container.waitFor({ state: "visible" })
465
      })
466

467
      await test.step("Add the product to the cart and goto checkout", async () => {
468
        await productPage.selectOption("M")
469
        await productPage.clickAddProduct()
470
        await productPage.cartDropdown.navCartLink.click()
471
        await productPage.cartDropdown.goToCartButton.click()
472
        await cartPage.container.waitFor({ state: "visible" })
473
        await cartPage.cartDropdown.close()
474
        cartSubtotal = Number(
475
          (await cartPage.cartTotal.getAttribute("data-value")) || ""
476
        )
477
      })
478
    })
479

480
    await test.step("Enter in the giftcard and assert value works", async () => {
481
      await cartPage.discountButton.click()
482
      await cartPage.discountInput.fill(discount.code)
483
      await cartPage.discountApplyButton.click()
484
      const paymentDiscount = await cartPage.getDiscount(discount.code)
485
      expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
486
      expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
487
        (cartSubtotal - discount.amount).toString()
488
      )
489
    })
490

491
    await test.step("Navigate away from the cart page and return to it", async () => {
492
      await storePage.goto()
493
      const product = await storePage.getProduct("Sweatpants")
494
      await product.locator.click()
495
      await productPage.container.waitFor({ state: "visible" })
496
      await productPage.selectOption("XL")
497
      await productPage.clickAddProduct()
498
      await productPage.cartDropdown.close()
499
      await cartPage.goto()
500
      cartSubtotal = Number(
501
        (await cartPage.cartSubtotal.getAttribute("data-value")) || ""
502
      )
503
    })
504

505
    await test.step("Verify the giftcard is still on the cart page", async () => {
506
      const paymentDiscount = await cartPage.getDiscount(discount.code)
507
      await expect(paymentDiscount.locator).toBeVisible()
508
      await expect(paymentDiscount.code).toContainText(discount.code)
509
      expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
510
        (cartSubtotal - discount.amount).toString()
511
      )
512
    })
513

514
    await test.step("Verify the giftcard is still on the checkout page", async () => {
515
      await cartPage.checkoutButton.click()
516
      await checkoutPage.container.waitFor({ state: "visible" })
517
      const paymentDiscount = await checkoutPage.getDiscount(discount.code)
518
      await expect(paymentDiscount.locator).toBeVisible()
519
      await expect(paymentDiscount.code).toContainText(discount.code)
520
      expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
521
        (cartSubtotal - discount.amount).toString()
522
      )
523
      expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
524
    })
525
  })
526
})
527

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.