Skip to content

Commit

Permalink
add parallel support - fake minor
Browse files Browse the repository at this point in the history
  • Loading branch information
taktakpeops committed Sep 29, 2019
1 parent 70b7047 commit 7e47ceb
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 35 deletions.
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# jest-environment-browserstack

[![Actions Status](https://github.com/taktakpeops/jest-environment-browserstack/workflows/Node%20CI/badge.svg)](https://github.com/taktakpeops/jest-environment-browserstack/actions) [![npm version](http://img.shields.io/npm/v/jest-environment-browserstack.svg?style=flat)](https://npmjs.org/package/jest-environment-browserstack "View this project on npm")

[![Actions Status](https://github.com/taktakpeops/jest-environment-browserstack/workflows/Node%20CI/badge.svg)](https://github.com/taktakpeops/jest-environment-browserstack/actions) [![npm version](http://img.shields.io/npm/v/jest-environment-browserstack.svg?style=flat)](https://npmjs.org/package/jest-environment-browserstack 'View this project on npm')

Use Jest as test-runner for running your visual-tests and more using Browserstack.

Expand Down Expand Up @@ -90,13 +89,30 @@ my-visual-test.spec.js:
/**
* @jest-environment browserstack
*/
import { until, By } from 'selenium-webdriver';
import { By } from 'selenium-webdriver';

describe('my visual test', () => {
it('test something', () => {
global.__driver__.get('https://mysuperurl.ltd');
// do something
// do test
let driver;

beforeAll(async () => {
// you can override the default configuration
driver = await global.__driver__({
'bstack:options': {
sessionName: 'my test',
},
});
driver.get('https://mysuperurl.ltd');
});

afterAll(async () => {
// can be omitted
await driver.quit();
});

it('test something', async () => {
const myElement = await driver.findElement(By.css('.super.class'));
const text = await myElement.getText();
expect(text).toBe('super text');
});
});
```
Expand All @@ -121,7 +137,7 @@ The `test` script will run a basic e2e tests, a visual tests making a snapshot o

# Known limitations

For now, only one browser can be defined.
For now, cannot override Browserstack parameters at runtime.

# Bug and more

Expand Down
3 changes: 3 additions & 0 deletions examples/with-bt-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
"start": "PORT=8080 node scripts/start.js",
"build": "node scripts/build.js",
"test:jest": "jest --ci",
"test:visual": "jest --testNamePattern=VisualTest --update-snapshot",
"test:e2e": "jest --testNamePattern=UiTest",
"test:unit": "jest --testNamePattern=UnitTest",
"test": "npm-run-all -p -r start test:jest"
},
"eslintConfig": {
Expand Down
18 changes: 15 additions & 3 deletions examples/with-bt-local/src/App.e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@

const { By } = require('selenium-webdriver');

describe('ui testing', () => {
const driver = global.__driver__;
describe('ui testing UiTest', () => {
let driver;

beforeAll(async () => {
driver = await global.__driver__({
'bstack:options': {
sessionName: 'ui testing',
},
});

it('load the app', async () => {
await driver.get('http://localhost:8080');
}, 10000);

afterAll(async () => {
await driver.quit();
});

it('load the app', async () => {
const title = await driver.getTitle();

expect(title).toBe('React App');
Expand Down
2 changes: 1 addition & 1 deletion examples/with-bt-local/src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
it('renders without crashing UnitTest', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
Expand Down
18 changes: 15 additions & 3 deletions examples/with-bt-local/src/__visual_testing__/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ const { toMatchImageSnapshot } = require('jest-image-snapshot');

expect.extend({ toMatchImageSnapshot });

describe('ui testing', () => {
const driver = global.__driver__;
describe('visual testing VisualTest', () => {
let driver;

beforeAll(async () => {
driver = await global.__driver__({
'bstack:options': {
sessionName: 'visual testing',
},
});

it('load the app', async () => {
await driver.get('http://localhost:8080');
}, 10000);

afterAll(async () => {
await driver.quit();
});

it('check the app', async () => {
const title = await driver.getTitle();

expect(title).toBe('React App');
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions examples/with-bt-local/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,18 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A==

"@types/lodash.merge@~4.6.6":
version "4.6.6"
resolved "https://registry.yarnpkg.com/@types/lodash.merge/-/lodash.merge-4.6.6.tgz#b84b403c1d31bc42d51772d1cd5557fa008cd3d6"
integrity sha512-IB90krzMf7YpfgP3u/EvZEdXVvm4e3gJbUvh5ieuI+o+XqiNEt6fCzqNRaiLlPVScLI59RxIGZMQ3+Ko/DJ8vQ==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.141"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.141.tgz#d81f4d0c562abe28713406b571ffb27692a82ae6"
integrity sha512-v5NYIi9qEbFEUpCyikmnOYe4YlP8BMUdTcNCAquAKzu+FA7rZ1onj9x80mbnDdOW/K5bFf3Tv5kJplP33+gAbQ==

"@types/q@^1.5.1":
version "1.5.2"
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.2.tgz#690a1475b84f2a884fd07cd797c00f5f31356ea8"
Expand Down Expand Up @@ -5256,9 +5268,11 @@ jest-each@^24.9.0:
version "1.0.0"
dependencies:
"@jest/types" "~24.9.0"
"@types/lodash.merge" "~4.6.6"
"@types/selenium-webdriver" "~4.0.3"
browserstack-local "~1.4.2"
jest-environment-node "~24.9.0"
lodash.merge "~4.6.2"
selenium-webdriver "~4.0.0-alpha.5"

[email protected]:
Expand Down Expand Up @@ -5923,6 +5937,11 @@ lodash.memoize@^4.1.2:
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=

lodash.merge@~4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-environment-browserstack",
"version": "1.0.0",
"version": "1.1.0",
"description": "an environment for using Browserstack with Jest",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -31,9 +31,11 @@
"license": "MIT",
"dependencies": {
"@jest/types": "~24.9.0",
"@types/lodash.merge": "~4.6.6",
"@types/selenium-webdriver": "~4.0.3",
"browserstack-local": "~1.4.2",
"jest-environment-node": "~24.9.0",
"lodash.merge": "~4.6.2",
"selenium-webdriver": "~4.0.0-alpha.5"
},
"devDependencies": {
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/github.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { WebDriver, By } from 'selenium-webdriver';

describe('Github', () => {
let driver: WebDriver;

beforeAll(async () => {
// eslint-disable-next-line
// @ts-ignore
driver = await global.__driver__({
'bstack:options': {
sessionName: 'github',
},
});
await driver.get('https://github.com/taktakpeops/jest-environment-browserstack');
}, 10000);

afterAll(async () => {
await driver.quit();
});

it('has an author', async () => {
const authorSpan = await driver.findElement(By.css('.author'));
const author = await authorSpan.getText();
expect(author).toBe('taktakpeops');
});

it('has a repository name', async () => {
const repoSpan = await driver.findElement(By.css('h1.public strong'));
const repo = await repoSpan.getText();
expect(repo).toBe('jest-environment-browserstack');
});
});
14 changes: 0 additions & 14 deletions src/__tests__/index.spec.ts

This file was deleted.

31 changes: 31 additions & 0 deletions src/__tests__/npm.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { WebDriver, By } from 'selenium-webdriver';

describe('NPM', () => {
let driver: WebDriver;

beforeAll(async () => {
// eslint-disable-next-line
// @ts-ignore
driver = await global.__driver__({
'bstack:options': {
sessionName: 'npm',
},
});
await driver.get('https://www.npmjs.com/package/jest-environment-browserstack');
}, 10000);

afterAll(async () => {
await driver.quit();
});

it('get title from NPM', async () => {
const title = await driver.getTitle();
expect(title).toBe('jest-environment-browserstack - npm');
});

it('get name of the module', async () => {
const titleBlock = await driver.findElement(By.css('.w-100.ph0-l.ph3.ph4-m span[title="jest-environment-browserstack"]'));
const title = await titleBlock.getText();
expect(title).toBe('jest-environment-browserstack');
});
});
25 changes: 20 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Options, Local } from 'browserstack-local';
import { Builder, WebDriver } from 'selenium-webdriver';
import { randomBytes } from 'crypto';
import { Script } from 'vm';
import merge from 'lodash.merge';

import { BrowserstackCapabilities } from './types';

Expand All @@ -18,6 +19,8 @@ export default class BrowserstackEnvironment extends NodeEnvironment {

private btTunnelOpts: Options;

private readonly drivers: WebDriver[];

constructor(config: Config.ProjectConfig) {
super(config);

Expand Down Expand Up @@ -54,6 +57,7 @@ export default class BrowserstackEnvironment extends NodeEnvironment {
}

this.selHubUrl = seleniumHubUrl;
this.drivers = new Array<WebDriver>();
}

async setup(): Promise<void> {
Expand All @@ -63,24 +67,35 @@ export default class BrowserstackEnvironment extends NodeEnvironment {
await this.createBTTunnel();
}

this.global.__driver__ = await this.createWDDriver();
this.global.__driver__ = this.createWDDriver.bind(this);
}

async teardown(): Promise<void> {
await super.teardown();

await this.global.__driver__.quit();
await Promise.all(
this.drivers.map(async driver => {
try {
await driver.quit();
} catch (_) {
return Promise.resolve();
}
}),
);
await this.closeBTTunnel();
}

runScript(script: Script): any {
return super.runScript(script);
}

private async createWDDriver(): Promise<WebDriver> {
const driverFactory = new Builder().usingServer(this.selHubUrl).withCapabilities(this.btCapabilities);
private async createWDDriver(capabilities?: BrowserstackCapabilities): Promise<WebDriver> {
const driverFactory = new Builder().usingServer(this.selHubUrl).withCapabilities(merge(this.btCapabilities, capabilities));
const driver = await driverFactory.build();

this.drivers.push(driver);

return await driverFactory.build();
return driver;
}

private createBTTunnel(): Promise<void | Error> {
Expand Down

0 comments on commit 7e47ceb

Please sign in to comment.