Playwright

Lesson 16 of 17

Project ยท 35 min

Project: test the shop

The task

This is the second project. You test the shop from the earlier lessons, from an empty file. The shop has a product list, a search box, a sort list, a cart with quantities, and totals with shipping. Orders over 50 dollars ship for free.

Do the steps by hand in the practice browser first, so that you know what the numbers should be. Then write a test, run it, and check your work.

What to use

You have the tools for a clean set of tests. Use them.

  • A page object for the shop, with the steps open, addToCart, and openCart.
  • A beforeEach hook that opens the shop and waits until the six products are there.
  • Assertions that wait. After an action that sends a request, check its result before the next step. Never use a fixed wait.
  • Exact values, such as toHaveText('$25.50'), so that a wrong total cannot pass.

Share the page object with the tests

A hook and a test do not share local variables. Make the page object in the hook and save it in a variable outside the hook, or ask for a fixture. The first way is shorter for a file of this size. The second way is better in a big project, because each test gets its own object, and you do not need a shared variable.

A last check

When all seven tasks pass, read your file once more as if you were a new colleague. Does each test name tell what the shop must do? Is the checking in the tests, and the clicking in the page object? A set of tests is a document that other people read.

Tasks

7 to do

  1. Task 1, not checked yet. Count the products in the cart. Add Blue Mug, and check that the cart count in the header is 1. Add Notebook, and check that it is 2.

  2. Task 2, not checked yet. Total the cart with shipping. Add Blue Mug and Notebook, and open the cart. Check that the cart has 2 rows, that the subtotal is $20.50, that the shipping is $5.00, and that the total is $25.50.

  3. Task 3, not checked yet. Ship for free over 50 dollars. Add the Backpack and open the cart. Check that the shipping is Free and that the total is $59.00.

  4. Task 4, not checked yet. Search without caring about case. Search for LAMP. Check that there is one product card, and that its heading is Desk Lamp.

  5. Task 5, not checked yet. Sort by price. Choose Price: low to high in the list Sort by, and check that the first heading of level 2 is Sticker Pack. Choose Price: high to low, and check that it is Backpack.

  6. Task 6, not checked yet. Remove a product. Add Notebook, open the cart, and click Remove. Check that the text Your cart is empty. is visible.

  7. Task 7, not checked yet. Use the quantity. Add Blue Mug and open the cart. Change the quantity of Blue Mug to 3. Check that the subtotal is $36.00.

Task 1 of 7

Count the products in the cart. Add Blue Mug, and check that the cart count in the header is 1. Add Notebook, and check that it is 2.

All tasks

example.spec.js

Ctrl+Enter runs your tests. Ctrl+Shift+Enter checks it. Press Esc, then Tab, to leave the editor.

Practice browser

The sample app is ready

The results of your tests show here. Press Run tests to watch them run in the practice browser.