DEV Community

Cover image for How to test websites: Using SIRV and Playwright for test driven development (TDD)
Michelle Duke
Michelle Duke

Posted on

How to test websites: Using SIRV and Playwright for test driven development (TDD)

I've been working on a few websites, for our wedding and for a food translation website for translating food while traveling. One of the fun things I've discovered is how to test these websites, and ensure everything is working.

What is test driven development (TDD)?

Two tools I found—or rather, was told about—called SIRV and Playwright, can help you run your website, and test parts of the site. Once you've done some tests and figured out how to make things better, you can make updates and changes based on those tests. This is called test driven development, where testing reveals gaps in your application and you make changes based on your testing.

Usually these gaps come in the form of a test failing. For example, if you have a form on your website and it fails when entering punctuation under dietary requirements, then you can change the form input to allow for punctuation marks. This makes your application better for your users and you now have new features based on the original tests you ran.

So how do you write tests for your applications?

What is SIRV and Playwright?

SIRV is a static site server. It's optimised middleware for serving requests to static assets. Thus, SIRV works best if you have a static site.

Playwright on the other hand is a testing method for web applications. Using these together, means that Playwright is the tool we will use to write and run tests. SIRV is the interface where we can see our application running, and see which tests are passing or failing.

Writing tests

To test your code, you'll need to write tests. In this example, I'm writing a test to see if I have a certain word or heading on a webpage. I used GitHub Copilot to help write a test to do this. The Playwright documentation gave me the correct starting point for writing the test.

In order to use Playwright for test writing, you'll need to import it. Here's the starting point for writing your test:

import { test, expect } from '@playwright/test';

I built a website for our wedding, and I wanted to test to see if the heading "Mish and Max" was detected on the page. So here's the code I used to test this:



test('Contact Mish and Max', async ({ page }) => {
    // Navigate to the page
    await page.goto('http://localhost:8080/contact.html');

    // Assert that the title says "Contact Mish and Max"
    await expect(page).toHaveTitle("Contact Mish and Max")
});


Enter fullscreen mode Exit fullscreen mode

You'll to add a new file to your project with the extension .spec.test. Make sure you save it, and save it each time you make changes. Now that we have the test written, let's run it.

Using SIRV and Playwright for testing

Firstly, you'll need Node for this to work. Follow the guide in the Node.js docs to install Node for Windows, MacOs, or Linux.

When you are ready to test your code, open a terminal in VS Code, or your editor of choice. I'm using VS Code so the demos here use VS Code. Open the terminal by selecting View > Terminal from the menu bar, or by pressing the Ctrl + ` keyboard shortcut.

Once you have the terminal, there are some commands you need to run:

npm install @playwright/test - this sets up the Playwright tests by installing the testing library.

npx playwright install - this downloads a testing version of Chrome, FireFox, Safari, and others.

npx sirv-cli . - this runs the local version of SIRV for testing.

Now that you have SIRV and Playwright setup and ready, we can run the tests. Open a new terminal in VS Code (or your editor of choice), and run the following command:

npx playwright test --ui This runs the test and opens a new window where we'll be able to run and see the tests running.

Here are all the commands above running:

installing playwright and SIRV

Playwright terminal test window:

Playwright test window

Reading and fixing the test

We can see in the test above that it fails on run. When we look into why this happens, we can determine how to fix it. By looking at the "Errors" tab, we can see what error occurred:

Error occurred

In this instance, it says:

Expected string: "Contact Mish and Max"
Received string: "Contact"

In other words, it expected to receive "Contact Mish and Max", but instead, it says "Contact". If I have a look at my *.html file, we can see I have two <title> tags:

Title tags

HMTL reads the first title tag only, even if there are other title tags. We can quickly fix this error by changing the first title tag to read <title>Contact Mish and Max</title>. Now we can run the test again:

Passing tests

This time, we see the ✔️ and the test passes 🎉.

Writing tests and writing code

Now that you know how to write and run tests with SIRV and Playwright, you can go ahead and write more complex tests. For example, on the same contact form above, I wrote a test—using the help of GitHub Copilot—to check if the contact form can be filled out and submitted:

writing test with GitHub Copilot

I can run this test, and step through each part of the test, seeing in the UI where the changes are being made on the website:

Running test in UI

Give this a try yourself, and let me know what tests you are writing, and if this guide was helpful.

Top comments (3)

Collapse
 
canmingir profile image
Can Mingir

This might be more BDD than TDD, what do you think?

Collapse
 
mishmanners profile image
Michelle Duke • Edited

I think it's TDD since it's testing individual components and then rewriting to make the test pass. BDD is testing overall behaviour, and to me seems like more customer journey mapping type testing involving the full user flow. Here's some interesting pieces I found:

"Test-driven development typically involves writing a test for a certain piece of functionality, running the test to see it fail and then writing the code to make the test pass."

"Behavior-driven development typically involves a developer, test engineer and a product manager (and potentially other stakeholders)."

"BDD is designed to test an application’s behavior from the end user’s standpoint, whereas TDD is focused on testing smaller pieces of functionality in isolation."

What are your thoughts? Thanks for this question, helped me learn even more about testing 😄

Collapse
 
canmingir profile image
Can Mingir

I had the same dilemma, BDD is black box, TDD is white box testing, but we are testing behavior of components in black box, but a component itself is in the white box. I think it is somewhere between, imao

btw, original BDD does not require or suggest involving product manager, still dev level. There is another one called ATDD, which includes product managers