Writing the first test with Playwright

 Writing the first test with Playwright


With a new year starting in a few days time, I wanted to learn something new. This time I chose to learn playwright. This blogpost I will explain the basic code of the test I have written.

x

As we can see from the above code we need to start the test with the import statement to import the playwright test module into our test, this will help us use any function within playwright test within the test code, so we need to write the first line as:

import { test, expect } = require('@playwright/test');

As with all test, our first task should be to create a test function. This test function will include the title of the test and the test script function. Within this test script function we pass a fixture named page.

test('tc_01 Assert page title', async ({ page }) => {

The next step is to call the URL of the web site that we want to run our test against. For this we use the goto function as shown below.

await page.goto('https://programsbuzz.com/');

Our first test, we assert the title of the page once we navigate using the goto function. To asset we use the expect function along with the toHaveTitle and we assert that the landing page has the specific given title.

await expect(page).toHaveTitle("ProgramsBuzz - Online Technical Courses");

Our first test, we also have to click on a button on the landing page. To click on a given button, there are multiple methods to follow. In this example we use the title of a button and pass that as a parameter within the click function.

await page.click('text=Ask Doubt');

To invoke the test run the below command from the location of your package.json

npx playwright test

The above is a simple example of the many features of Paywrite that I hope to learn and add to my bucket of web automation tools.



Comments