Skip to content

Commit 553777a

Browse files
Merge pull request #1 from BrowserStackCE/develop
Playwright Integration
2 parents aac32b2 + a507887 commit 553777a

14 files changed

Lines changed: 716 additions & 0 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
package-lock.json

README.md

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
![Logo](https://www.browserstack.com/images/static/header-logo.jpg)
2+
3+
# BrowserStack Examples Playwright Framework <a href="https://playwright.dev/"><img src="https://playwright.dev/img/playwright-logo.svg" alt="Playwright" height="30" /></a>
4+
5+
## Introduction
6+
7+
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.
8+
9+
This BrowserStack Example repository demonstrates a test framework written in Playwright Framework with parallel testing capabilities. The Playwright test scripts are written for the open source [BrowserStack Demo web application](https://bstackdemo.com) ([Github](https://github.com/browserstack/browserstack-demo-app)). This BrowserStack Demo App is an e-commerce web application which showcases multiple real-world user scenarios. The app is bundled with offers data, orders data and products data that contains everything you need to start using the app and run tests out-of-the-box.
10+
11+
The Playwright tests are run on different platforms like on-prem and BrowserStack using various run configurations and test capabilities.
12+
13+
---
14+
15+
## Repository setup
16+
17+
- Clone the repository
18+
19+
- Ensure you have the following dependencies installed on the machine
20+
- Node.js
21+
22+
Install the requirements:
23+
```sh
24+
npm install
25+
```
26+
27+
28+
## About the tests in this repository
29+
30+
This repository contains the following Selenium tests:
31+
32+
| Module | Test name | Description |
33+
| --- | --- | --- |
34+
| e2e | E2E Test | This test scenario verifies successful product purchase lifecycle end-to-end. |
35+
| login | Check if Signin opens on clicking on favourites nav item | This test verifies the login workflow with different types of valid login users. |
36+
| login | Check Login with locked_user | This test verifies the login workflow error for a locked user. |
37+
| offers | Set GPS location to Mumbai and check offers | This test mocks the GPS location for Mumbai and verifies that the product offers applicable for the Mumbai location are shown. |
38+
| product | Apply Apple And Samsung Filter | This test verifies that the Apple products are only shown if the Apple vendor filter option is applied. |
39+
| product | Apply 'Lowest to Highest' Order By Filter | This test verifies that the product prices are in ascending order when the product sort "Lowest to Highest" is applied. |
40+
| user | Check Login with image_not_loading_user | This test verifies that the product images load for user: "image_not_loading_user" on the e-commerce application. Since the images do not load, the test case assertion fails.|
41+
| user | Check Order in existing_orders_user | This test verifies that existing orders are shown for user: "existing_orders_user" |
42+
43+
---
44+
45+
46+
## Test infrastructure environments
47+
48+
- [On-premise/self-hosted](#on-premise-self-hosted)
49+
- [BrowserStack](#browserstack)
50+
51+
52+
# On Premise / Self Hosted
53+
54+
## Running Your Tests
55+
56+
### Run a specific test on your own machine
57+
58+
- How to run the test?
59+
60+
To run a specific test scenario, use the following command with the additional 'test-name' argument:
61+
62+
```sh
63+
npx playwright test <spec-file> --headed --config=resources/conf/playwright.config.js --project <project-name>
64+
```
65+
66+
where, the argument `<spec-file>` can be any spec files from the repository.
67+
68+
E.g. "e2e.spec.js", "login.spec.js", "product.spec.js" or any of the other tests, as outlined in [About the tests in this repository](#About-the-tests-in-this-repository) section.
69+
70+
Also, the argument `<project-name>` can be any of the project names from the `playwright.conf.js` file.
71+
72+
Or, you can directly run the pre-confifured setup by running the below command:
73+
```sh
74+
npm run onPrem-endToEnd
75+
```
76+
77+
- Output
78+
79+
This run profile executes a specific test scenario on a single browser instance on your own machine.
80+
81+
82+
### Run the entire test suite in parallel on your own machine
83+
84+
To run the entire test suite on your own machine, use the following command:
85+
86+
```sh
87+
npx playwright test --headed --config=resources/conf/playwright.config.js --workers 2
88+
```
89+
90+
Or, you can directly run the pre-confifured setup by running the below command:
91+
92+
```sh
93+
npm run onPrem-parallel
94+
```
95+
96+
97+
- Output
98+
99+
This run profile executes the entire test collection in parallel on single/multiple browsers based on the configuration file, on your own machine.
100+
101+
102+
---
103+
104+
105+
# BrowserStack
106+
107+
[BrowserStack](https://browserstack.com) provides instant access to 2,000+ real mobile devices and browsers on a highly reliable cloud infrastructure that effortlessly scales as testing needs grow.
108+
109+
## Prerequisites
110+
111+
- Create a new [BrowserStack account](https://www.browserstack.com/users/sign_up) or use an existing one.
112+
- Identify your BrowserStack username and access key from the [BrowserStack Automate Dashboard](https://automate.browserstack.com/) and export them as environment variables using the below commands.
113+
114+
- For \*nix based and Mac machines:
115+
116+
```sh
117+
export BROWSERSTACK_USERNAME=<browserstack-username> &&
118+
export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
119+
```
120+
121+
- For Windows:
122+
123+
```shell
124+
set BROWSERSTACK_USERNAME=<browserstack-username>
125+
set BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
126+
```
127+
128+
Alternatively, you can also hardcode username and access_key objects in the [fixtures.js](./fixtures.js) file.
129+
130+
Note:
131+
- We have configured a list of test capabilities in the [playwright-bstack.config.js](resources/conf/playwright-bstack.config.js) file. You can certainly update them based on your device / browser test requirements.
132+
- The exact test capability values can be easily identified using the [Browserstack Capability Generator](https://browserstack.com/automate/capabilities) and the allowed Browsers and OS are mentioned [here](https://www.browserstack.com/docs/automate/playwright/browsers-and-os)
133+
134+
135+
## Running Your Tests
136+
137+
### Run a specific test on BrowserStack
138+
139+
In this section, we will run a single test on Chrome browser on Browserstack. To change test capabilities for this configuration, please refer to the `playwright-bstack.config.js` file.
140+
141+
- How to run the test?
142+
143+
144+
To run a specific test scenario, use the following command :
145+
Note: You can change the test you want to run by replacing the respective spec file.
146+
147+
```sh
148+
npx playwright test <spec-file> --config=resources/conf/playwright-bstack.config.js --project 'chrome@latest:Windows 10@browserstack'"
149+
150+
```
151+
152+
where, the argument `<spec-file>` can be any spec files from the repository.
153+
154+
E.g. "e2e.spec.js", "login.spec.js", "product.spec.js" or any of the other tests, as outlined in [About the tests in this repository](#About-the-tests-in-this-repository) section.
155+
156+
Also, the argument `<project-name>` can be any of the project names from the `playwright-bstack.conf.js` file.
157+
158+
Or, you can directly run the pre-confifured setup by running the below command:
159+
160+
```sh
161+
npm run bstack-single
162+
```
163+
164+
- Output
165+
166+
This run profile executes a single test on a single browser on BrowserStack. Please refer to your [BrowserStack dashboard](https://automate.browserstack.com/) for test results.
167+
168+
169+
### Run the entire test suite in parallel on BrowserStack browsers
170+
171+
In this section, we will run the tests in parallel on a single browser on Browserstack. Refer to `playwright-bstack.conf.js` file to change test capabilities for this configuration.
172+
173+
- How to run the test?
174+
175+
To run the entire test suite in parallel on a single BrowserStack browser, use the following command:
176+
177+
```sh
178+
npx playwright test --config=resources/conf/playwright-bstack.config.js --project '<project-name>' --workers 5
179+
```
180+
181+
Note: The `workers` argument mentions the number of tests you want to run in parallel at a time.
182+
183+
- Output
184+
185+
This run profile executes the entire test suite in parallel on a single BrowserStack browser. Please refer to your [BrowserStack dashboard](https://automate.browserstack.com/) for test results.
186+
187+
Or, you can directly run the pre-confifured setup by running the below command:
188+
189+
```sh
190+
npm run bstack-parallel-tests
191+
```
192+
193+
<b>Note:</b> If you want to run tests on multiple browsers, you just need to remove the `project` argument from the command.
194+
195+
You can directly run the above scenario using the following command:
196+
197+
```sh
198+
npm run bstack-parallel-browsers
199+
```
200+
201+
202+
203+
204+
### Run a tests on BrowserStack which need Local Environment access
205+
206+
## Prerequisites
207+
* Clone the BrowserStack demo application repository.
208+
```sh
209+
git clone https://github.com/browserstack/browserstack-demo-app
210+
```
211+
* Please follow the README.md on the BrowserStack demo application repository to install and start the dev server on localhost.
212+
213+
* In this section, we will run a single test case to test the BrowserStack Demo app hosted on your local machine i.e. localhost. Refer to the `playwright-bstack-local.conf.js` file for configuration and setup and teardown processes.
214+
215+
Note: You may need to provide additional BrowserStackLocal arguments to successfully connect your localhost environment with BrowserStack infrastructure. (e.g if you are behind firewalls, proxy or VPN).
216+
217+
* Further details for successfully creating a BrowserStackLocal connection can be found here:
218+
219+
* [Local Testing with Automate](https://www.browserstack.com/local-testing/automate)
220+
221+
## [Web application hosted on internal environment] Run a specific test on BrowserStack using BrowserStackLocal
222+
223+
In this section, we will run a single test on Chrome browser on Browserstack. To change test capabilities for this configuration, please refer to the `playwright-bstack-local.config.js` file.
224+
225+
To run a specific test scenario, use the following command :
226+
Note: You can change the test you want to run by replacing the respective spec file.
227+
228+
```sh
229+
npx playwright test <spec-file> --config=resources/conf/playwright-bstack-local.config.js --project 'chrome@latest:Windows 10@browserstack'"
230+
```
231+
232+
where, the argument `<spec-file>` can be any spec files from the repository.
233+
234+
E.g. "e2e.spec.js", "login.spec.js", "product.spec.js" or any of the other tests, as outlined in [About the tests in this repository](#About-the-tests-in-this-repository) section.
235+
236+
Also, the argument `<project-name>` can be any of the project names from the `playwright-bstack.conf.js` file.
237+
238+
Or, you can directly run the pre-confifured setup by running the below command:
239+
240+
```sh
241+
npm run bstack-local
242+
```
243+
244+
- Output
245+
246+
This run profile executes a single test on a single browser on BrowserStack. Please refer to your [BrowserStack dashboard](https://automate.browserstack.com/) for test results.
247+
248+
249+
### [Web application hosted on internal environment] Run a tests in parallel on BrowserStack using BrowserStackLocal
250+
251+
Refer the below snippet, here we will run the tests in parallel on a single browser on Browserstack. Refer to `playwright-bstack-local.conf.js` file to change test capabilities for this configuration.
252+
253+
```sh
254+
npx playwright test --config=resources/conf/playwright-bstack-local.config.js --project '<project-name>' --workers 5
255+
```
256+
257+
Refer the below snippet, here we will run the tests in parallel on a multiple browser on Browserstack. Refer to `playwright-bstack-local.conf.js` file to change test capabilities for this configuration.
258+
259+
```sh
260+
npx playwright test --config=resources/conf/playwright-bstack-local.config.js --workers 5
261+
```
262+
263+
Note: The `workers` argument mentions the number of tests you want to run in parallel at a time.
264+
265+
266+
Or, you can directly run the pre-confifured setup by running the below command:
267+
268+
```sh
269+
npm run bstack-local-parallel
270+
```
271+
272+
- Output
273+
274+
This run profile executes the entire test suite in parallel on a single BrowserStack browser. Please refer to your [BrowserStack dashboard](https://automate.browserstack.com/) for test results.
275+
276+
277+
## Additional Resources
278+
279+
- View your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)
280+
- Documentation for writing [Automate test scripts in Java](https://www.browserstack.com/automate/java)
281+
- Customizing your tests capabilities on BrowserStack using our [test capability generator](https://www.browserstack.com/automate/capabilities)
282+
- [Using Automate REST API](https://www.browserstack.com/automate/rest-api) to access information about your tests via the command-line interface
283+
- Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)
284+
- For testing public web applications behind IP restriction, [Inbound IP Whitelisting](https://www.browserstack.com/local-testing/inbound-ip-whitelisting) can be enabled with the [BrowserStack Enterprise](https://www.browserstack.com/enterprise) offering
285+

fixtures.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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, title) => {
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+
let arr = title.split('-')
40+
let newTitle = arr[arr.length - 1]
41+
caps.browser = browser ? browser : 'chrome';
42+
caps.browser_version = browser_version ? browser_version : 'latest';
43+
caps.os = os ? os : 'osx';
44+
caps.os_version = os_version ? os_version : 'catalina';
45+
caps.name = newTitle;
46+
};
47+
48+
const isHash = (entity) => Boolean(entity && typeof (entity) === "object" && !Array.isArray(entity));
49+
const nestedKeyValue = (hash, keys) => keys.reduce((hash, key) => (isHash(hash) ? hash[key] : undefined), hash);
50+
51+
exports.test = base.test.extend({
52+
page: async ({ page, playwright }, use, testInfo) => {
53+
// Use BrowserStack Launched Browser according to capabilities for cross-browser testing.
54+
if (testInfo.project.name.match(/browserstack/)) {
55+
56+
57+
page.close()
58+
patchCaps(testInfo.project.name, `${testInfo.file} - ${testInfo.title}`);
59+
const vBrowser = await playwright.chromium.connect({
60+
wsEndpoint:
61+
`wss://cdp.browserstack.com/playwright?caps=` +
62+
`${encodeURIComponent(JSON.stringify(caps))}`,
63+
});
64+
const vPage = await vBrowser.newPage({...testInfo.project.use});
65+
await use(vPage);
66+
67+
const testResult = {
68+
action: 'setSessionStatus',
69+
arguments: {
70+
status: testInfo.status,
71+
reason: nestedKeyValue(testInfo, ['error', 'message'])
72+
},
73+
};
74+
await vPage.evaluate(() => { },
75+
`browserstack_executor: ${JSON.stringify(testResult)}`);
76+
await vPage.close();
77+
await vBrowser.close();
78+
} else {
79+
use(page);
80+
}
81+
},
82+
});

0 commit comments

Comments
 (0)