Skip to content

Commit 9a0e047

Browse files
Adding playwright test documentation
1 parent 58b089c commit 9a0e047

18 files changed

+2276
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ If you are using Jest to run your Playwright tests, you can run all your playwri
9797
5. If you are trying to run your own Jest tests on BrowserStack, then you need to ensure that you have configured the `connectOptions` and `browsers` as shown in the `module.exports` of the config file.
9898
6. Run the sample jest script using `npm test` which runs the test `google.test.js` across 3 browsers in BrowserStack serially. Your can also configure Jest to run your tests in parallel.
9999

100+
## Playwright with Test Runner
101+
102+
If you are using Playwright Test Runner to run your Playwright tests, you can run all your playwright-test tests on BrowserStack as well. Follow the steps below to run the sample tests in this repository:
103+
104+
1. Clone this repository using `git clone https://github.com/browserstack/playwright-browserstack.git` (if not already done).
105+
2. Go inside the directory playwright-test using `cd playwright-test`
106+
3. Install the dependencies using `npm install`
107+
4. Put in your credentials in the file `fixtures.js` in the caps part.
108+
5. If you are trying to run your own tests on BrowserStack, then you need to ensure that you have configured the `projects` correctly in `playwright.config.js` file.
109+
6. Run the sample test script using `npm run test` which runs all the tests inside tests directory across 5 browsers in BrowserStack.
110+
7. Run the sample test script using `npm run test:local` and add `browserstack.local:true` in the file `fixture.js` in caps part which runs all the tests inside tests directory across 5 browsers in BrowserStack.
111+
8. If you want to run your tests locally, you just need to configure the `projects` without name `@browserstack` in `playwright.config.js` file.
112+
100113
## Facing issues?
101114

102115
If you are facing any issue with any of the above or any other issue in trying to run your Playwright tests on BrowserStack, you can [reach out to support](https://www.browserstack.com/contact#technical-support), select product as `Automate` and post your query there.

playwright-test/fixtures.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const base = require('@playwright/test');
2+
const cp = require('child_process');
3+
const clientPlaywrightVersion = cp
4+
.execSync('npx playwright --version')
5+
.toString()
6+
.trim()
7+
.split(' ')[1];
8+
const BrowserStackLocal = require('browserstack-local');
9+
10+
const caps = {
11+
browser: 'chrome',
12+
os: 'osx',
13+
os_version: 'catalina',
14+
name: 'My first playwright test',
15+
build: 'playwright-build-1',
16+
'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
17+
'browserstack.accessKey':
18+
process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
19+
'client.playwrightVersion': clientPlaywrightVersion,
20+
};
21+
22+
exports.bsLocal = new BrowserStackLocal.Local();
23+
24+
// replace YOUR_ACCESS_KEY with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
25+
exports.BS_LOCAL_ARGS = {
26+
key: process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
27+
};
28+
29+
const patchCaps = (name) => {
30+
let combination = name.split(/@browserstack/)[0];
31+
let [browerCaps, osCaps] = combination.split(/:/);
32+
let [browser, browser_version] = browerCaps.split(/@/);
33+
let osCapsSplit = osCaps.split(/ /);
34+
let os = osCapsSplit.shift();
35+
let os_version = osCapsSplit.join(' ');
36+
caps.browser = browser ? browser : 'chrome';
37+
caps.browser_version = browser_version ? browser_version : 'latest';
38+
caps.os = os ? os : 'osx';
39+
caps.os_version = os_version ? os_version : 'catalina';
40+
caps.name = name;
41+
};
42+
43+
exports.test = base.test.extend({
44+
browser: async ({ playwright, browser }, use, workerInfo) => {
45+
if (workerInfo.project.name.match(/browserstack/)) {
46+
patchCaps(workerInfo.project.name);
47+
const vBrowser = await playwright.chromium.connect({
48+
wsEndpoint:
49+
`wss://cdp.browserstack.com/playwright?caps=` +
50+
`${encodeURIComponent(JSON.stringify(caps))}`,
51+
});
52+
await use(vBrowser);
53+
} else {
54+
await use(browser);
55+
}
56+
},
57+
page: async ({ page, browser }, use, testInfo) => {
58+
if (testInfo.project.name.match(/browserstack/)) {
59+
const vPage = await browser.newPage();
60+
await use(vPage);
61+
const testResult = {
62+
action: 'setSessionStatus',
63+
arguments: {
64+
status: testInfo.status,
65+
reason: testInfo?.error?.message,
66+
},
67+
};
68+
await vPage.evaluate(() => {},
69+
`browserstack_executor: ${JSON.stringify(testResult)}`);
70+
} else {
71+
use(page);
72+
}
73+
},
74+
});

playwright-test/global-setup.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// global-setup.js
2+
const { bsLocal, BS_LOCAL_ARGS } = require('./fixtures');
3+
let localStarted = false;
4+
const { promisify } = require('util');
5+
const sleep = promisify(setTimeout);
6+
module.exports = async (config) => {
7+
console.log('Starting BrowserStackLocal ...');
8+
// Starts the Local instance with the required arguments
9+
bsLocal.start(BS_LOCAL_ARGS, (callback) => {
10+
console.log('BrowserStackLocal Started');
11+
localStarted = true;
12+
});
13+
while (!localStarted) {
14+
await sleep(1000);
15+
}
16+
};

playwright-test/global-teardown.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// global-teardown.js
2+
const { bsLocal } = require('./fixtures');
3+
let localStopped = false;
4+
const {promisify} = require('util');
5+
const sleep = promisify(setTimeout);
6+
module.exports = async (config) => {
7+
// Stop the Local instance after your test run is completed, i.e after driver.quit
8+
bsLocal.stop(() => {
9+
localStopped = true;
10+
console.log('Stopped BrowserStackLocal')
11+
});
12+
while(!localStopped){
13+
await sleep(1000);
14+
}
15+
};

0 commit comments

Comments
 (0)