QA Automation Web Automation

Web Automation Testing using Playwright

Web automation testing using Playwright

What is Playwright?

A Playwright is an automation testing tool from Microsoft. Playwright can be used for Web Automation Testing as well as API Automation Testing. The playwright has lots of new features and it requires less coding. It is an open-source framework. Playwright Script can be written in Python, Java, .NET, and Node.js Languages. A playwright is used to perform cross-browser testing, due to its wide test coverage, accuracy, and high speed of test execution. By using playwright APIs, we can perform End-to-End testing.  In Playwright Web elements/UI is available at the back before the test script starts interacting with it. 

The playwright has the built-in feature to deal with the web elements if web elements are not available on the page I.e the auto wait feature. Playwright performs the series of checks for an element and then only the required action is performed on the element when all the checks are passed, because of this feature the user doesn’t need to explicitly handle the waits as compared to other testing frameworks such as selenium, and cypress. 

Pre-requisite:

Python’s Latest version should be installed on the System. 

Playwright supports two variations of the API which are developed as per the Pythons Sync & Async Methods: 

Sync API: – 

In Sync nature playwright executes the code sequentially that means, if the first line of code statement finishes execution then only the second line of code statement starts execution. 

Async API: –

In an Async nature, the playwright executes all the code statements simultaneously at a time and provides results. The playwright provides more stable API calls and methods in async API.   

Features: – 

  • Test Generator: –  In Playwright we can generate the script of any language such as Python, Java, C#, and TypeScript by using the Playwright Command. 
Test generator
  • A playwright can simulate any device
simulate device
  • Playwright inspector: – 

Playwright Inspector is a tool that is used for debugging Playwright scripts. We must pass the playwright command as PWDEBUG=1 in the environment setup, this command will run the script in debug mode., When we run the script, it will launch the browser along with the playwright inspector and wait until we provide further instructions, I.e., we must just resume the script running from the playwright inspector. While inspecting we can pause & resume the executing script. 

When PWDEBUG=1 is set, the Playwright Inspector window will be opened, and the script will be paused on the first Playwright statement now we know what action is about to be performed and we can check more details on that action. 

For example, The 1st action statement in Playwright is page .goto(), and the Playwright Inspector will pause the Script when it encounters goto() method. 

  • Trace viewer: –  It helps to debug the code, it shows which action you have performed with the screenshot of the DOM, the duration of the action performed, and which selector is used along with the value. Trace View is recorded using the browser context session.
trace view Playwright automation testing

Once the trace view is recorded, we can view traces either by using the command or by using the playwright trace view URL 

trace view 02
  • Screenshot: –

Capturing the Screenshot in Playwright is made easier as compared to Selenium and Cypress, we need to use the screenshot () method.

We can capture the Fullscreen shot as well as the specific DOM element screenshots by passing the locator. 

screenshot capture
  • Screen Recording: – 

In playwright we can record the video of the running script in the browser context, we need to open the context session within a browser instance, and to save the video we need to close the context before the browser is closed. 

screen recording

Installation: – 

Run the Following Command to Install Playwright 

playwright automation testing installation

Run the Following Command to Install Playwright-pytest 

Playwright pytest installation

Configuration: –

  • Pytest.ini File: – In pytest.ini file we mention the base URL, the Execution speed of the script, and many configurations. 
Playwright pytest configuration
  • Conftest.py File: – This is the global configuration file for the Playwright, which you can configure according to the requirements such as which browser should be launched, In headed or headless mode. 
Playwright automation testing code

Folder Structure: –  

  1. Constants: – This Folder Contains Static Data such as User Name & Passwords. 
  2. Test Case:- Package (Python):- This Python Package Contains the Test Cases. By default, when we create the Python Package the __init__.py . 
  3. Page Class Package (Python):- This Python Package Contains the Pages. By default, when we create the Python Package the __init__.py . 
  4. Evidence Folder: – In this Folder, we can store the Screen Shots captured by the script as Evidence.
  5. Test Execution Report: – In this Folder, we can store the End-to-End Test Execution report. 

In the next blog, we will talk about working with different browser launch operations in Playwright. Stay tuned!