Home Web Front-end JS Tutorial How to test websites: Using SIRV and Playwright for test driven development (TDD)

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

Aug 07, 2024 pm 03:23 PM

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")
});
Copy after login

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:

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

Playwright terminal test window:

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

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:

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

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 tags:</p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172301542124988.png" class="lazy" alt="How to test websites: Using SIRV and Playwright for test driven development (TDD)"></p> <p>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. Now we can run the test again:

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

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:

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

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:

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

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

The above is the detailed content of How to test websites: Using SIRV and Playwright for test driven development (TDD). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1256
24
Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript and the Web: Core Functionality and Use Cases JavaScript and the Web: Core Functionality and Use Cases Apr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript in Action: Real-World Examples and Projects JavaScript in Action: Real-World Examples and Projects Apr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding the JavaScript Engine: Implementation Details Understanding the JavaScript Engine: Implementation Details Apr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: Community, Libraries, and Resources Python vs. JavaScript: Community, Libraries, and Resources Apr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Python vs. JavaScript: Development Environments and Tools Python vs. JavaScript: Development Environments and Tools Apr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

The Role of C/C   in JavaScript Interpreters and Compilers The Role of C/C in JavaScript Interpreters and Compilers Apr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

From Websites to Apps: The Diverse Applications of JavaScript From Websites to Apps: The Diverse Applications of JavaScript Apr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

See all articles