How to Test Coupons and Discounts with Playwright
Webshops and subscription-based services often offer discounts through coupon codes. Applying a valid coupon code during checkout might reduce the price of one, several, or all items in the shopping cart.
Steps
While discount coupons will be applied in different ways depending on the service or shop they are relevant to, in most cases:
- Having selected one or more products will be a prerequisite for applying the coupon
- Entering a valid coupon will result in visible feedback, i.e. a reduction of the previous product/cart price
The following example, running against our test site, will add a variable number of items to the cart, then proceed to compare the total price before and after applying the coupon. The coupon is reducing the price of the whole shopping cart by 20%, therefore we will be asserting that the discounted price is reduced by the right amount. For this step we have chosen Chai, but any solid assertion library will do.
Run this example as follows:
PRODUCTS_NUMBER=3 node coupon.js
SET PRODUCTS_NUMBER=3
node coupon.js
Takeaways
- We can simply verify that coupons are accepted, or also check that they command the right discount.
- Assertion libraries are useful when non-trivial assertions are required.