Skip to content

Commit 04fa0e2

Browse files
Changing test cases and dynamic local config
1 parent 6f6ac67 commit 04fa0e2

16 files changed

+65
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ If you are using Playwright Test Runner to run your Playwright tests, you can ru
107107
4. Put in your credentials in the file `fixtures.js` in the caps part.
108108
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.
109109
6. Run the sample test script using `npm 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.
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.
111111
8. If you want to run your tests locally, you just need to configure the `projects` without name `@browserstack` in `playwright.config.js` file.
112112

113113
## Facing issues?

playwright-test/fixtures.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const clientPlaywrightVersion = cp
77
.split(' ')[1];
88
const BrowserStackLocal = require('browserstack-local');
99

10+
// BrowserStack Specific Capabilities.
1011
const caps = {
1112
browser: 'chrome',
1213
os: 'osx',
@@ -16,6 +17,7 @@ const caps = {
1617
'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
1718
'browserstack.accessKey':
1819
process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
20+
'browserstack.local': process.env.BROWSERSTACK_LOCAL || false,
1921
'client.playwrightVersion': clientPlaywrightVersion,
2022
};
2123

@@ -26,6 +28,7 @@ exports.BS_LOCAL_ARGS = {
2628
key: process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
2729
};
2830

31+
// Patching the capabilities dynamically according to the project name.
2932
const patchCaps = (name) => {
3033
let combination = name.split(/@browserstack/)[0];
3134
let [browerCaps, osCaps] = combination.split(/:/);
@@ -45,6 +48,7 @@ const nestedKeyValue = (hash, keys) => keys.reduce((hash, key) => (isHash(hash)
4548

4649
exports.test = base.test.extend({
4750
browser: async ({ playwright, browser }, use, workerInfo) => {
51+
// Use BrowserStack Launched Browser according to capabilities for cross-browser testing.
4852
if (workerInfo.project.name.match(/browserstack/)) {
4953
patchCaps(workerInfo.project.name);
5054
const vBrowser = await playwright.chromium.connect({
@@ -54,10 +58,12 @@ exports.test = base.test.extend({
5458
});
5559
await use(vBrowser);
5660
} else {
61+
// Use Local Browser for testing.
5762
await use(browser);
5863
}
5964
},
6065
page: async ({ page, browser }, use, testInfo) => {
66+
// Overriding page function to mark the status on BrowserStack.
6167
if (testInfo.project.name.match(/browserstack/)) {
6268
const vPage = await browser.newPage();
6369
await use(vPage);
@@ -70,6 +76,7 @@ exports.test = base.test.extend({
7076
};
7177
await vPage.evaluate(() => {},
7278
`browserstack_executor: ${JSON.stringify(testResult)}`);
79+
await vPage.close();
7380
} else {
7481
use(page);
7582
}

playwright-test/global-setup.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
// global-setup.js
22
const { bsLocal, BS_LOCAL_ARGS } = require('./fixtures');
3-
let localStarted = false;
43
const { promisify } = require('util');
54
const sleep = promisify(setTimeout);
6-
module.exports = async (config) => {
5+
const redColour = '\x1b[31m';
6+
const whiteColour = '\x1b[0m';
7+
module.exports = async () => {
78
console.log('Starting BrowserStackLocal ...');
89
// Starts the Local instance with the required arguments
9-
bsLocal.start(BS_LOCAL_ARGS, (callback) => {
10-
console.log('BrowserStackLocal Started');
11-
localStarted = true;
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;
1220
});
13-
while (!localStarted) {
21+
while (!localResponseReceived) {
1422
await sleep(1000);
1523
}
1624
};

playwright-test/global-teardown.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// global-teardown.js
22
const { bsLocal } = require('./fixtures');
3-
let localStopped = false;
4-
const {promisify} = require('util');
3+
const { promisify } = require('util');
54
const sleep = promisify(setTimeout);
6-
module.exports = async (config) => {
5+
module.exports = async () => {
76
// 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);
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+
}
1417
}
1518
};

playwright-test/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"description": "Sample package to show jest playwright tests run on BrowserStack",
99
"scripts": {
1010
"test": "npx playwright test --config=./playwright.config.js",
11-
"test:local": "npx playwright test --config=./playwright-local.config.js"
11+
"test:local": "BROWSERSTACK_LOCAL=true npx playwright test --config=./playwright-local.config.js"
1212
},
1313
"devDependencies": {
1414
"playwright": "^1.13.1",

playwright-test/playwright-local.config.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const config = {
1010
globalSetup: require.resolve('./global-setup'),
1111
globalTeardown: require.resolve('./global-teardown'),
1212
timeout: 60000,
13+
use:{
14+
viewport: null
15+
},
1316
projects: [
1417
// -- BrowserStack Projects --
1518
// name should be of the format browser@browser_version:os os_version@browserstack
@@ -30,24 +33,22 @@ const config = {
3033
{
3134
name: 'edge@90:Windows 10@browserstack',
3235
use: {
33-
browserName: 'chromium',
34-
viewport: { width: 1280, height: 1024 }
36+
browserName: 'chromium'
3537
},
3638
},
3739
{
3840
name: 'playwright-firefox@latest:OSX Catalina@browserstack',
3941
use: {
4042
browserName: 'firefox',
41-
viewport: null,
4243
ignoreHTTPSErrors: true
4344
},
4445
},
4546
{
4647
name: 'playwright-webkit@latest:OSX Big Sur@browserstack',
4748
use: {
4849
browserName: 'webkit',
49-
...devices['iPhone 12 Pro Max'],
50-
viewport: { width: 1280, height: 1024 },
50+
// Config to use playwright emulated devices.
51+
// ...devices['iPhone 12 Pro Max'],
5152
},
5253
},
5354
// -- Local Projects --

playwright-test/playwright.config.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const config = {
77
testDir: 'tests',
88
testMatch: '**/*.spec.js',
99
timeout: 60000,
10+
use:{
11+
viewport: null
12+
},
1013
projects: [
1114
// -- BrowserStack Projects --
1215
// name should be of the format browser@browser_version:os os_version@browserstack
@@ -27,24 +30,22 @@ const config = {
2730
{
2831
name: 'edge@90:Windows 10@browserstack',
2932
use: {
30-
browserName: 'chromium',
31-
viewport: { width: 1280, height: 1024 }
33+
browserName: 'chromium'
3234
},
3335
},
3436
{
3537
name: 'playwright-firefox@latest:OSX Catalina@browserstack',
3638
use: {
3739
browserName: 'firefox',
38-
viewport: null,
3940
ignoreHTTPSErrors: true
4041
},
4142
},
4243
{
4344
name: 'playwright-webkit@latest:OSX Big Sur@browserstack',
4445
use: {
4546
browserName: 'webkit',
46-
...devices['iPhone 12 Pro Max'],
47-
viewport: { width: 1280, height: 1024 },
47+
// Config to use playwright emulated devices.
48+
// ...devices['iPhone 12 Pro Max'],
4849
},
4950
},
5051
// -- Local Projects --

playwright-test/tests/test_10.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ test.describe('feature foo', () => {
66
await page.goto('https://www.google.com/ncr');
77
const element = await page.$('[aria-label="Search"]');
88
await element.click();
9-
await element.type('BrowserStack');
9+
await element.type('BrowserStack Percy');
1010
await element.press('Enter');
1111
const title = await page.title('');
1212
console.log(title);
13-
expect(title).toEqual( 'BrowserStack - Google Search', 'Expected page title is incorrect!');
13+
expect(title).toEqual( 'BrowserStack Percy - Google Search', 'Expected page title is incorrect!');
1414
});
1515
});

playwright-test/tests/test_2.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ test.describe('feature foo', () => {
66
await page.goto('https://www.google.com/ncr');
77
const element = await page.$('[aria-label="Search"]');
88
await element.click();
9-
await element.type('BrowserStack');
9+
await element.type('BrowserStack Playwright');
1010
await element.press('Enter');
1111
const title = await page.title('');
1212
console.log(title);
13-
expect(title).toEqual( 'BrowserStack - Google Search', 'Expected page title is incorrect!');
13+
expect(title).toEqual( 'BrowserStack Playwright - Google Search', 'Expected page title is incorrect!');
1414
});
1515
});

playwright-test/tests/test_3.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ test.describe('feature foo', () => {
66
await page.goto('https://www.google.com/ncr');
77
const element = await page.$('[aria-label="Search"]');
88
await element.click();
9-
await element.type('BrowserStack');
9+
await element.type('BrowserStack Puppeteer');
1010
await element.press('Enter');
1111
const title = await page.title('');
1212
console.log(title);
13-
expect(title).toEqual( 'BrowserStack - Google Search', 'Expected page title is incorrect!');
13+
expect(title).toEqual( 'BrowserStack Puppeteer - Google Search', 'Expected page title is incorrect!');
1414
});
1515
});

0 commit comments

Comments
 (0)