Automation testing with Playwright

Why Playwright?

Cross-language: JavaScript, TypeScript, Python, .NET, Java
Cross-platform: Windows, Linux, macOS, and support headed and headless mode
Cross-browser: Chromium, Webkit, and Firefox
Auto-wait
Codegen: Record your actions and s…


This content originally appeared on DEV Community and was authored by Alleluia Izimpamvu

Why Playwright?

  • Cross-language: JavaScript, TypeScript, Python, .NET, Java
  • Cross-platform: Windows, Linux, macOS, and support headed and headless mode
  • Cross-browser: Chromium, Webkit, and Firefox
  • Auto-wait
  • Codegen: Record your actions and save them in your language
  • Trace Viewer: Capture all information to investigate the test failure
  • And many more...

Pre-requisites

  • Install Node.js 18 +
  • VScode(To have a better experience by using the playwright extension)
  • Basic programming skills

Installation

  1. Open the terminal and run the below command

npm init playwright@latest

2.Choose JavaScript(default is TypeScript)

Image description

3.Choose where you want to save your tests and hit Enter

Image description

4.Add a GitHub Actions to easily run tests on CI( Select false)

Image description

5.Install Playwright browsers and hit Enter

Image description

6.Install Playwright operating system dependencies(No is a default) hit Enter

Image description

7.Congratulations you have successfully installed Playwright

Write the first test scripts

We are going to use the playwright's example and

  • Import test and expect arguments from playwright
  • Open playwright URL
  • Verify that the page title has Playwright
  • Click on Get Started
  • Verify that Installation text is visible

Code Snippet

/*Importing test which help you to write test cases */
/*Importing expect which help to validate expected result */
const { test, expect } = require('@playwright/test');

/* Open Playwright url*/
test('has title', async ({ page }) => {
  await page.goto('https://playwright.dev/');

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/Playwright/);
});

test('get started link', async ({ page }) => {
  await page.goto('https://playwright.dev/');

  // Click the get started link.
  await page.getByRole('link', { name: 'Get started' }).click();

  // Expects page to have a heading with the name of Installation.
  await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});

Code Screenshot for better visibility

Image description

Run the test

Run: npx playwright test

This command will run all the tests under test Directory which is tests

Display playwright test report

Run: npx playwright show-report

Below is the sample of playwright HTML report

Image description

Happy testing!!🎭


This content originally appeared on DEV Community and was authored by Alleluia Izimpamvu


Print Share Comment Cite Upload Translate Updates
APA

Alleluia Izimpamvu | Sciencx (2024-06-27T16:16:37+00:00) Automation testing with Playwright. Retrieved from https://www.scien.cx/2024/06/27/automation-testing-with-playwright/

MLA
" » Automation testing with Playwright." Alleluia Izimpamvu | Sciencx - Thursday June 27, 2024, https://www.scien.cx/2024/06/27/automation-testing-with-playwright/
HARVARD
Alleluia Izimpamvu | Sciencx Thursday June 27, 2024 » Automation testing with Playwright., viewed ,<https://www.scien.cx/2024/06/27/automation-testing-with-playwright/>
VANCOUVER
Alleluia Izimpamvu | Sciencx - » Automation testing with Playwright. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/27/automation-testing-with-playwright/
CHICAGO
" » Automation testing with Playwright." Alleluia Izimpamvu | Sciencx - Accessed . https://www.scien.cx/2024/06/27/automation-testing-with-playwright/
IEEE
" » Automation testing with Playwright." Alleluia Izimpamvu | Sciencx [Online]. Available: https://www.scien.cx/2024/06/27/automation-testing-with-playwright/. [Accessed: ]
rf:citation
» Automation testing with Playwright | Alleluia Izimpamvu | Sciencx | https://www.scien.cx/2024/06/27/automation-testing-with-playwright/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.