We will be covering Playwright on the blog for a while. We will be creating a simple example that addresses the need to use a screenshot of a page in a search result.
![](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F6j1HOSEpdKNVzJ0gN8Q2RI%2F6680737bcf153a8a4f53aa02d787cc70%2Fchristian-wiediger-8nXKXYdO-Wk-unsplash.jpg&w=3840&q=75)
What is Playwright?
Two tools are often mentioned as being able to meet the need to automate testing using the browser's functionality.
We will proceed with this comparison, assuming Playwright, rather than the other way around. The main reasons are,
- Playwright has more browser support
- Playwright's development is handled by Microsoft and is frequently maintained.
Somewhere along the way, I have come to have a secure image regarding open source projects that are supported by Microsoft. For now.
Create a Typescript project
In this blog, I have introduced samples using Next.js and Vite, but this time, I will create a project using express to make it work like an API.
This time, we will work in a Windows environment where Node.js is available. First, create a project.
mkdir playwright
cd playwright
npm init -y
![playwrifht01.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2FJmXc3l26do1MSlBudoZKp%2F93c6c09b7530bd0d1522b38dd7a4c08c%2Fplaywrifht01.png&w=3840&q=75)
Add the necessary modules.
npm install express
npm install -D typescript @types/node @types/express
Next, create tsconfig.json. In this case, the following code was used to create the file.
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
Since we are setting up a file under src, we first create the src/app.ts file as follows.
import express from "express";
import { hello } from "./routes/hello";
const app = express();
const port = 3000;
// API Test
app.get("/api/hello", hello);
// Handler when accessing the root path ('/')
app.get("/", (req, res) => {
res.send("Welcome to the Sitecore Search Utility");
});
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
The file that is being read in this file . /routes/hello file as src/routes/hello.ts.
// hello.ts
import { Request, Response } from "express";
export const hello = (req: Request, res: Response) => {
res.json({ message: "Hello World" });
};
Finally, rewrite the scripts section of package.json as follows
"scripts": {
"build": "tsc",
"dev": "node ./dist/app.js",
"start": "tsc && npm run dev",
"test": "echo \"Error: no test specified\" && exit 1"
},
Now you are ready to go. npm run build and you will see a compiled JavaScript file in the dist folder.
![playwright02.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F6lGTZoNKm2m66UIXu9ZRBp%2Fe8bb1428c01bcdea64e6ed9dcc09dd86%2Fplaywright02.png&w=3840&q=75)
Then run the command. When you run the command npm run start, you will see the following.
![playwright03.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F6YM22OAkIV4EW4ABxJf6o4%2Fb7bdde12b50d9acaca76e791ffbdcb6a%2Fplaywright03.png&w=3840&q=75)
If you actually access http://localhost:3000/ with a browser, you will see the following message
![playwright04.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2FZUxUYbvP7dpNQyiIe17eY%2F329808a576a4889769e91a104a6d01db%2Fplaywright04.png&w=3840&q=75)
What happens when you access http://localhost:3000/api/hello that you have already created? You will see the following results
![playwright05.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F1yOSH0UEQPkMNIZhthDs27%2Fc717d0be73a71e7004c7371d8dbb881c%2Fplaywright05.png&w=3840&q=75)
A simple express-based typescript sample is now available.
Summary
This article has been long, so I will end with an explanation of Playwright and an introduction to creating typescript samples. In the next article, we will build up to the point of taking a screenshot of a specified URL using Playwright.