- 04 Jan 2024
- 2 Minutes to read
- Print
- DarkLight
Playwright
- Updated on 04 Jan 2024
- 2 Minutes to read
- Print
- DarkLight
Overview
Playwright stands as a Node.js library, leveraging a JavaScript runtime to simplify the process of web development. This versatile tool serves as a cross-browser framework, offering a unified API for seamless end-to-end automated testing of web applications. By employing Playwright, developers can conduct regular tests to ensure the smooth and expected performance of their web applications. What sets Playwright apart is its ability to simulate real-world events from a user's perspective, providing a comprehensive testing environment that closely mirrors user interactions.
In essence, Playwright plays a crucial role in enhancing the efficiency and reliability of web development testing. Its integration with Node.js and JavaScript not only facilitates a developer-friendly environment but also ensures a consistent testing approach across different browsers. With its focus on emulating user experiences, Playwright goes beyond traditional testing methods, offering a robust solution for developers to validate the functionality and performance of their web applications.
Playwright Execution guide with pCloudy
Prerequisite
- A pCloudy account is a prerequisite.
- Node must be installed in the client machine, and its version should be greater than 12.
- NPM should also be there
- Ensure compatibility with playwright version 1.39.0, as it is the supported version for the framework.
Configuration
Inside an Empty folder initiate a blank NodeJS project by the following command
npm init –y
Then Install the following dependencies by the following command
npm install playwright@1.39.0
Now create a new js file which should contain the below script name the file as playwrightDemo.js
const { chromium } = require('playwright'); (async () => { const capabilities = { 'browserName': 'chrome', // Browsers allowed: 'chrome', 'edge' 'pc:options': { 'instance_id':'Enter-Instance-Id', 'email': 'Enter-Email', 'apiKey': 'Enter-api-Key', 'clientName': 'Enter-client-Name', } } const browser = await chromium.connect({ headless: false, wsEndpoint: `wss://private-live-browsercloud-us.pcloudy.com/playwright?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`, // change the value of base url accordingly }); const context = await browser.newContext(); const page = await context.newPage(); await page.goto('https://device.pcloudy.com/'); await page.locator('#userId').type('test@pcloudy.com'); await page.locator('#password').type('test@pcloudy.com'); await page.locator('#loginSubmitBtn').click(); await page.close(); await browser.close(); })();
To get the “instance_id” for the script run the following api to fetch the details
Fetch token
Method : POST
Url - https://dev2-browsercloud-in.pcloudy.com/api/authenticateClient.phpBody : {"clientName":"Enter-client- Name","apiKey":"Enter-apikey"}
Get Instance Id
Method : POST
Url - https://dev2-browsercloud-in.pcloudy.com/api/getAvailableBrowsers.php
Body : {"browserCloudAuthToken": "Enter-the-token-fetched-from-above"}
From the above you will get multiple instance id for different OS replace the incance_id value with that which you want to use
Note - change the value of base url accordingly
Execute
To execute run the following command inside the project folder
Node playwrightDemo.js