Test Chrome Extensions using Playwright
Learn how to test Chrome extensions on Playwright tests in BrowserStack Automate
Chrome extensions enhance the browsing experience by adding features and functionality to the Chrome browser. BrowserStack offers a seamless integration that empowers developers to test Chrome extensions within their automated scripts.
Prerequisites
- BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
- Latest Playwright version is installed.
- NodeJS version 14 or higher is installed.
Test Chrome extensions
To test Chrome extension on BrowserStack, complete the following steps:
Step 1: Set BrowserStack credentials
Set the environment variables BROWSERSTACK_USERNAME
and BROWSERSTACK_ACCESS_KEY
with your credentials as follows:
export BROWSERSTACK_USERNAME="YOUR_USERNAME"
export BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"
Alternatively, your can add your credentials to the browserstack.username
and browserstack.accessKey
capabilities in the playwright-test.js
file.
Step 2: Upload Chrome extension
Prior to starting the test execution, upload the extension (in .zip format) using REST API. The REST API returns a response media_url
for successful upload.
curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" -X POST "https://api-cloud.browserstack.com/automate/upload-media" -F "file=@/path/to/file/test.zip"
You will get the Unique identifier returned upon successful upload of your file on BrowserStack. This value is used to specify the files to be used in your tests.
//sample respone
{
"media_url": "media://90c7a8h8dc82308108734e9a46c24d8f01de12881"
}
Step 3: Update the media_url
in your script
Update the media_url
obtained in Step 2 in your script.
const caps = {
//update the uploadMedia url
"browserstack.uploadMedia": [
"<media-url>",
]},
Step 4: Run your tests
Save the final script as playwright-test.js
:
'use strict';
const { chromium, firefox, webkit } = require('playwright');
const fs = require('fs')
const { promisify } = require('util');
const sleep = promisify(setTimeout);
const run = async (browser_name, os_name, os_version_name) => {
const tag = `Chrome Extension Testing`
const caps = {
build: `${tag}`,
name: `[${os_version_name}] ${browser_name}`, // test name
browser: browser_name,
os: os_name,
osVersion: os_version_name,
'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
'browserstack.accessKey': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
'browserstack.networkLogs': true,
"browserstack.uploadMedia": [
"<media-url>",
],
};
// For loading the extension
const browser = await chromium.connectOverCDP(`wss://cdp.browserstack.com/playwright?caps=${encodeURIComponent(JSON.stringify(caps))}`);
const context = await browser.contexts()[0];
const page = await context.newPage();
// Actual tests start here
await page.goto('https://en.wikipedia.org/wiki/Apple');
const imageActual = page.locator('a > img[alt="Malus domestica a1.jpg"]');
await imageActual.waitFor({ state: "visible" })
await imageActual.hover();
await sleep(2 * 1000);
const imageActual2 = page.locator('div > img');
await imageActual2.waitFor({ state: "visible" })
await page.close();
await context.close();
await browser.close();
};
Promise.all([
run('chrome', 'windows', '10'),
]).then(responses => {
for (const response of responses) {
console.log(response);
}
}).catch(error => {
console.error(`Failed to fetch: ${error}`)
});
From the root directory, run the following command:
node playwright-test.js
Step 5: View test results
View your tests on the BrowserStack Automate dashboard. To learn more about the dashboard, check the view test results document.
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
- RESOURCES
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!