|
| 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 | + })(); |
0 commit comments