Skip to content

Commit 761b35b

Browse files
authored
Merge pull request #6 from karanshah-browserstack/playwright-test
Adding playwright test documentation
2 parents 17dfa40 + b529662 commit 761b35b

24 files changed

+2355
-6001
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 test` which runs all the tests inside tests directory across 5 browsers in BrowserStack.
110+
7. If you want to test against localhost or other private websites, then run the sample test script using `npm run test:local` 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.

codeceptjs-example/package-lock.json

Lines changed: 40 additions & 5978 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codeceptjs-example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
"@codeceptjs/examples": "^1.2.1",
1616
"@codeceptjs/ui": "^0.4.3",
1717
"codeceptjs": "^3.0.6",
18-
"playwright": "1.13.1"
18+
"playwright": "~1.13.1"
1919
}
2020
}

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"devDependencies": {
33
"browserstack-local": "^1.4.8",
44
"global-agent": "^3.0.0",
5-
"playwright": "1.13.1"
5+
"playwright": "~1.13.1"
6+
},
7+
"engines": {
8+
"node": ">=12.7.0"
69
},
710
"dependencies": {
811
"chai": "^4.3.4"

playwright-jest/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

playwright-jest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
"devDependencies": {
1212
"jest": "^27.0.3",
1313
"jest-playwright-preset": "^1.6.0",
14-
"playwright": "1.13.1"
14+
"playwright": "~1.13.1"
1515
}
1616
}

playwright-test/fixtures.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
// BrowserStack Specific Capabilities.
11+
const caps = {
12+
browser: 'chrome',
13+
os: 'osx',
14+
os_version: 'catalina',
15+
name: 'My first playwright test',
16+
build: 'playwright-build-1',
17+
'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
18+
'browserstack.accessKey':
19+
process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
20+
'browserstack.local': process.env.BROWSERSTACK_LOCAL || false,
21+
'client.playwrightVersion': clientPlaywrightVersion,
22+
};
23+
24+
exports.bsLocal = new BrowserStackLocal.Local();
25+
26+
// replace YOUR_ACCESS_KEY with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
27+
exports.BS_LOCAL_ARGS = {
28+
key: process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
29+
};
30+
31+
// Patching the capabilities dynamically according to the project name.
32+
const patchCaps = (name) => {
33+
let combination = name.split(/@browserstack/)[0];
34+
let [browerCaps, osCaps] = combination.split(/:/);
35+
let [browser, browser_version] = browerCaps.split(/@/);
36+
let osCapsSplit = osCaps.split(/ /);
37+
let os = osCapsSplit.shift();
38+
let os_version = osCapsSplit.join(' ');
39+
caps.browser = browser ? browser : 'chrome';
40+
caps.browser_version = browser_version ? browser_version : 'latest';
41+
caps.os = os ? os : 'osx';
42+
caps.os_version = os_version ? os_version : 'catalina';
43+
caps.name = name;
44+
};
45+
46+
const isHash = (entity) => Boolean(entity && typeof(entity) === "object" && !Array.isArray(entity));
47+
const nestedKeyValue = (hash, keys) => keys.reduce((hash, key) => (isHash(hash) ? hash[key] : undefined), hash);
48+
49+
exports.test = base.test.extend({
50+
browser: async ({ playwright, browser }, use, workerInfo) => {
51+
// Use BrowserStack Launched Browser according to capabilities for cross-browser testing.
52+
if (workerInfo.project.name.match(/browserstack/)) {
53+
patchCaps(workerInfo.project.name);
54+
const vBrowser = await playwright.chromium.connect({
55+
wsEndpoint:
56+
`wss://cdp.browserstack.com/playwright?caps=` +
57+
`${encodeURIComponent(JSON.stringify(caps))}`,
58+
});
59+
await use(vBrowser);
60+
} else {
61+
// Use Local Browser for testing.
62+
await use(browser);
63+
}
64+
},
65+
page: async ({ page, browser }, use, testInfo) => {
66+
// Overriding page function to mark the status on BrowserStack.
67+
if (testInfo.project.name.match(/browserstack/)) {
68+
const vPage = await browser.newPage();
69+
await use(vPage);
70+
const testResult = {
71+
action: 'setSessionStatus',
72+
arguments: {
73+
status: testInfo.status,
74+
reason: nestedKeyValue(testInfo, ['error', 'message'])
75+
},
76+
};
77+
await vPage.evaluate(() => {},
78+
`browserstack_executor: ${JSON.stringify(testResult)}`);
79+
await vPage.close();
80+
} else {
81+
use(page);
82+
}
83+
},
84+
});

playwright-test/global-setup.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// global-setup.js
2+
const { bsLocal, BS_LOCAL_ARGS } = require('./fixtures');
3+
const { promisify } = require('util');
4+
const sleep = promisify(setTimeout);
5+
const redColour = '\x1b[31m';
6+
const whiteColour = '\x1b[0m';
7+
module.exports = async () => {
8+
console.log('Starting BrowserStackLocal ...');
9+
// Starts the Local instance with the required arguments
10+
let localResponseReceived = false;
11+
bsLocal.start(BS_LOCAL_ARGS, (err) => {
12+
if (err) {
13+
console.error(
14+
`${redColour}Error starting BrowserStackLocal${whiteColour}`
15+
);
16+
} else {
17+
console.log('BrowserStackLocal Started');
18+
}
19+
localResponseReceived = true;
20+
});
21+
while (!localResponseReceived) {
22+
await sleep(1000);
23+
}
24+
};

playwright-test/global-teardown.js

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

0 commit comments

Comments
 (0)