- How to use this repo
- Requirements
- Chapter 1: Start a QA Project from Scratch
- Chapter 2: Refactoring and Fixtures for Usability
- Chapter 3: Create Data-Driven UI Tests with an API
Each month we cover a different "chapter" as we progress to a robust Test Automation Framework.
To go through any chapter, or catch up on a session you missed, open the Branches page and click on the one you want to see!
Then take a look at the README to see what to do.
π‘ Any time you see a dollar sign, like
$ something
, that means to run that command in the Terminal!
- bun.sh installed for typescript (free)
- Playwright
Takeaways
- Create a new TypeScript project with bun
- Setup VS Code to work with Playwright and TypeScript
- Break down an app so we can strategize how we iteravitely test and cover it
- Use Gherkin to define our features and scenarios
- Write our first Playwright tests and use their extensions and features to help out!
Extensions to install
- Playwright Test for VS Code
- ESLint
- Feature Syntax Highlight and Snippets (Cucumber/Gherkin)
Create the Workspace
-
Create a new directory/folder somewhere on your machine and then open it in VS Code
-
Then initialize the project using bun:
$ bun init
π‘ You should see some files and folders get created automatically!
Now that we're rolling in VS Code, we can install Playwright and configure it.
-
$ bun add playwright
-
$ bun add @playwright/test
-
Add
@playwright/test
to thetsconfig.json
"types": [ "bun-types", // add Bun global "@playwright/test" ]
-
Create a new
/tests
folder -
Open the Playwright extension and click
Record Test
. You can usehttps://saucedemo.com
-
Start performing some actions and see how each action is recorded
π‘ At this point, you're full on using Playwright! I recommend getting familiar with their docs
We are using Gherkin to help us define features and scenarios we want to describe what we mean. However, we are not going to be using Cucumber or any other BDD tool. BDD is great, but automation tools for it are not great in my opinion.
- Under
/tests
, make a new/sauce
folder - Under
/tests/sauce
, make a new/features
folder
Now you're ready for the challenges!
Get as far as you can with each challenge on your own, but if you need help, post any questions in the #autobots
channel of our QA Utah Slack Group and people will help!
We will be using the SwagLabs website, but this applies to any app.
- Within
/tests/sauces/features
, create aproduct.md
file - To start out, just use English and markdown to break down the app however it makes sense to you
- This requires you to explore SwagLabs, but that's exactly what we do in QA
Saying "test adding items to the cart" is much better than "test the whole app"! You have to strategically organize and strategize how you do your testing since you don't have an infinite amount of time or resources.
π― Your goal is to break down SwagLabs into smaller pieces. It's common to break it down by areas and/or features.
Example
# Features
* Login Page
* Products Page
* View Products
* Sort Products
* Add item to cart
...
Pick one of the features you defined in your products.md
file for this challenge.
- Within
/tests/sauces/features
, create a{name}.feature
file. For example,login.feature
- Use Gherkin to describe the Feature and write Scenarios to cover it
πΌπ₯ If you are new to writing Gherkin, checkout this BDD 101 "How to" article by the Automation Panda
π― Your goal is to describe the feature, provide simple acceptance criteria, and to write at least a couple scenarios to cover it.
Pick one of the scenarios you defined in your .feature
file for this challenge.
- Either create your own
{name}.spec.ts
file or record a new test - Create a test that covers your scenario!
π― Your goal is to create a test that covers your scenario. You can create as many tests as you want, but make sure you see them FAIL and PASS for the right reasons!
For more advanced attendees, use everything you've learned up to this point to try and achieve 100% Product Coverage of SwagLabs! Product Coverage (aka Test Coverage) means that you are testing everything that matters in an app or system. For this challenge:
- Finish the
product.md
file so it's ready to share - Finish creating the
.feature
files with the features and scenarios - Create all of the automated tests to cover your scenarios
π‘ Many companies dream of getting 100% coverage of their apps, but few actually achieve that... can you?
In the previous chapter, we ended with our checkout.spec.ts scripted test. In this chapter, we will be refactoring those steps into Page Objects and Fixtures that make it easier to use and maintain as our repo gets bigger and changes happen in the apps.
To show how we iteratively did our refactoring, you can see each "iteration" of the checkout.spec.ts
file:
-
Use fixtures to create a SEAM for authentication and page navigation
-
Start refactoring action flows into Page Objects
-
Refactor the checkout flow into a CheckoutPage Page Object
-
Refactor CartPage and CheckoutPage into fixtures
-
Group pages into a Pages object
Before starting on these challenges, your /tests/sauce
folder should look like this:
- π
/tests/sauce
- π features
- π pages
- π cart.ts
- π checkout.ts
- π pages.ts
- π§ͺ checkout.spec.ts
- βοΈ fixtures.ts
We have some remaining "scripted" lines in our test:
await page.goto('/inventory.html')
await page.locator('[data-test="add-to-cart-sauce-labs-backpack"]').click()
await page.locator('[data-test="add-to-cart-sauce-labs-bike-light"]').click()
Refactor these into a new InventoryPage
class and "hook it up" with the rest of what we've built!
Then use it in our test and get it to pass with no errors.
With your new knowledge and power, write a completely new test file that covers the different sorting scenarios on the Inventory (aka Products) page.
In this chapter, our Application Under Test (AUT) will be DemoQA.com since it has a frontend and an API we can use to get or set data. This opens up more advanced scenarios than the SauceDemo app we used in previous chapters:
- Do API Test Automation against their endpoints (see their Swagger Docs)
- Explore UI Test Automation on a different app (see their Bookstore App)
- Get and Set data using their API to drive what happens within the UI test
π‘ We call this
"data-driven testing"
because we control the data, environment, and app state to test our scenarios more quickly and reliably
To start off, let's set up the project so it's isolated.
-
Create a
demoqa
project in ourplaywright.config.ts
file so we have things likebaseURL
{ name: 'demoqa', use: { // ...devices['Desktop Chrome'], baseURL: 'https://www.demoqa.com', }, },
-
Next, create a new
demoqa
folder undertests
to store our relevant tests and files -
In VS Code, make sure to select
demoqa
as the active project in the Playwright extension
To show how we iteratively did our refactoring, you can see each "iteration" of the checkout.spec.ts
file:
-
Create an API Test to explore how to create a user with Playwright
-
Create an API Test to explore how to authorize a user to get a token
-
Make an interface and "service" functions
-
Generate fake data with
faker.js
to avoid "already exists" errors -
Use the account service to create an authorized user and login with it in the UI
-
Use the bookstore service to create an authorized user and give them some books
-
Use everything we've learned to make a data-driven test
Now that you have the fundamentals of Data-Driven testing using APIs, feel free to make this your own! These challenges will help solidify your understanding by applying what you've learned. As you've seen, moving "out of playwright" requires regular programming knowledge, so it might be a good idea to take some javascript/typescript courses first.
The "Follow Along" is helpful, but at a company, you wouldn't see iterative files like that. For this challenge, organize the tests into UI and API folders and give things better names. For example:
- π
/tests/demoqa
- π services
- π account.ts
- π bookstore.ts
- π ui
- π§ͺ profile.spec.ts
- π api
- π§ͺ account.spec.ts
- π§ͺ bookstore.spec.ts
- βοΈ fixtures.ts
- π services
This chapter highlighted how to do data-driven testing, but we skipped breaking down the app into features and scenarios π± In this challenge, take the time to break down the bookstore website into features and scenarios.
- Create a
/features
folder and put your markdown and.feature
files within it - Create page objects in a
/pages
folder, similar to what we did in Chapter 2 - BONUS: Write the tests to achieve 100% Product Coverage of the bookstore website!
Things should still feel organized. For example:
- π
/tests/demoqa
- π features
- π pages
- π books.ts
- π login.ts
- π profile.ts
- π services
- π account.ts
- π bookstore.ts
- π ui
- π§ͺ profile.spec.ts
- π api
- π§ͺ account.spec.ts
- π§ͺ bookstore.spec.ts
- βοΈ fixtures.ts