Skip to content

Commit a7b6d66

Browse files
committed
Added sample code for test from behind proxy
1 parent 4b9989f commit a7b6d66

File tree

3 files changed

+213
-16
lines changed

3 files changed

+213
-16
lines changed

google_search_with_proxy.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const expect = require('chai').expect
2+
const { chromium } = require('playwright');
3+
4+
const cp = require('child_process');
5+
const clientPlaywrightVersion = cp.execSync('npx playwright --version').toString().trim().split(' ')[1];
6+
7+
const { bootstrap } = require('global-agent');
8+
9+
// Have to set this environment variable with required data before executing this script
10+
// export GLOBAL_AGENT_HTTP_PROXY=http://someuser:[email protected]:3128
11+
12+
bootstrap();
13+
14+
(async () => {
15+
const caps = {
16+
'browser': 'chrome', // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
17+
'os': 'osx',
18+
'os_version': 'catalina',
19+
'name': 'My first playwright test',
20+
'build': 'playwright-build-1',
21+
'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
22+
'browserstack.accessKey': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
23+
'client.playwrightVersion': clientPlaywrightVersion // Playwright version being used on your local project needs to be passed in this capability for BrowserStack to be able to map request and responses correctly
24+
};
25+
const browser = await chromium.connect({
26+
wsEndpoint: `wss://cdp.browserstack.com/playwright?caps=${encodeURIComponent(JSON.stringify(caps))}`,
27+
});
28+
const page = await browser.newPage();
29+
await page.goto('https://www.google.com/ncr');
30+
const element = await page.$('[aria-label="Search"]');
31+
await element.click();
32+
await element.type('BrowserStack');
33+
await element.press('Enter');
34+
const title = await page.title('');
35+
console.log(title);
36+
try {
37+
expect(title).to.equal("BrowserStack - Google Search", 'Expected page title is incorrect!');
38+
// following line of code is responsible for marking the status of the test on BrowserStack as 'passed'. You can use this code in your after hook after each test
39+
await page.evaluate(_ => {}, `browserstack_executor: ${JSON.stringify({action: 'setSessionStatus',arguments: {status: 'passed',reason: 'Title matched'}})}`);
40+
} catch {
41+
await page.evaluate(_ => {}, `browserstack_executor: ${JSON.stringify({action: 'setSessionStatus',arguments: {status: 'failed',reason: 'Title did not match'}})}`);
42+
}
43+
await browser.close();
44+
})();

package-lock.json

+168-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"devDependencies": {
33
"browserstack-local": "^1.4.8",
4+
"global-agent": "^3.0.0",
45
"playwright": "^1.11.1"
56
},
67
"dependencies": {

0 commit comments

Comments
 (0)