Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
Zero Cho committed Jan 23, 2019
2 parents b0628e8 + f224788 commit 292849c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 41 deletions.
40 changes: 18 additions & 22 deletions Gulpfile.js
@@ -1,26 +1,19 @@
var path = require('path');
var gulp = require('gulp');
var babel = require('gulp-babel');
var sequence = require('gulp-sequence');
var del = require('del');
var nodeVersion = require('node-version');
var execa = require('execa');


var PACKAGE_PARENT_DIR = path.join(__dirname, '../');
var PACKAGE_SEARCH_PATH = (process.env.NODE_PATH ? process.env.NODE_PATH + path.delimiter : '') + PACKAGE_PARENT_DIR;


gulp.task('clean', function () {
function clean () {
return del('lib');
});

gulp.task('lint', function () {
// TODO: eslint supports node version 4 or higher.
// Remove this condition once we get rid of node 0.10 support.
if (nodeVersion.major === '0')
return null;
}

function lint () {
var eslint = require('gulp-eslint');

return gulp
Expand All @@ -32,14 +25,14 @@ gulp.task('lint', function () {
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
}

gulp.task('build', ['lint', 'clean'], function () {
function build () {
return gulp
.src('src/**/*.js')
.pipe(babel())
.pipe(gulp.dest('lib'));
});
}

function testMocha () {
if (!process.env.BROWSERSTACK_USERNAME || !process.env.BROWSERSTACK_ACCESS_KEY)
Expand All @@ -62,17 +55,17 @@ function testMocha () {
return execa(mochaCmd, mochaOpts, { stdio: 'inherit' });
}

gulp.task('test-mocha', ['build'], function () {
function testMochaRest () {
process.env.BROWSERSTACK_USE_AUTOMATE = 0;

return testMocha();
});
}

gulp.task('test-mocha-automate', ['build'], function () {
function testMochaAutomate () {
process.env.BROWSERSTACK_USE_AUTOMATE = 1;

return testMocha();
});
}

function testTestcafe () {
if (!process.env.BROWSERSTACK_USERNAME || !process.env.BROWSERSTACK_ACCESS_KEY)
Expand All @@ -93,16 +86,19 @@ function testTestcafe () {
return execa(testCafeCmd, testCafeOpts, { stdio: 'inherit' });
}

gulp.task('test-testcafe', ['build'], function () {
function testTestcafeRest () {
process.env.BROWSERSTACK_USE_AUTOMATE = '0';

return testTestcafe();
});
}

gulp.task('test-testcafe-automate', ['build'], function () {
function testTestcafeAutomate () {
process.env.BROWSERSTACK_USE_AUTOMATE = '1';

return testTestcafe();
});
}

gulp.task('test', sequence('test-mocha', 'test-mocha-automate', 'test-testcafe', 'test-testcafe-automate'));
exports.clean = clean;
exports.lint = lint;
exports.build = gulp.parallel(gulp.series(clean, build), lint);
exports.test = gulp.series(exports.build, testMochaRest, testMochaAutomate, testTestcafeRest, testTestcafeAutomate);
40 changes: 39 additions & 1 deletion README.md
Expand Up @@ -49,6 +49,14 @@ Proxy options can be passed via envrionment variables.
- `BROWSERSTACK_FORCE_PROXY` - if it's not empty, forces all traffic of Browserstack local binary to go through the proxy,
- `BROWSERSTACK_FORCE_LOCAL` - if it's not empty, forces all traffic of Browserstack local binary to go through the local machine

Example:
```
export BROWERSTACK_PROXY="user:p@ssw0rd@proxy.com:8080"
export BROWERSTACK_LOCAL_PROXY="admin:12345678@192.168.0.2:8080"
export BROWSERSTACK_FORCE_PROXY="1"
export BROWSERSTACK_FORCE_LOCAL="1"
testcafe browserstack:chrome test.js
```
## Browserstack JS Testing and Browserstack Automate
Browserstack offers two APIs for browser testing:
- [Browserstack JS Testing](https://www.browserstack.com/javascript-testing-api)
Expand All @@ -62,20 +70,50 @@ Browserstack offers two APIs for browser testing:
TestCafe uses the JS Testing API by default. In order to use Browserstack Automate,
set the `BROWSERSTACK_USE_AUTOMATE` environment variable to `1`.

Example:
```
export BROWSERSTACK_USE_AUTOMATE="1"
testcafe browserstack:chrome test.js
```

## Setting display resolution

To set the display resolution, use the `BROWSERSTACK_DISPLAY_RESOLUTION` environment variable.
Valid resolutions can be found [here](https://github.com/browserstack/api#resolution).

Remember that this only sets the display resolution and does not resize the browser window. You'll still need to use TestCafe's [window resizing API](https://devexpress.github.io/testcafe/documentation/test-api/actions/resize-window.html) to do so.

Example:
```
export BROWSERSTACK_DISPLAY_RESOLUTION="1024x768"
testcafe browserstack:chrome test.js
```

## Extra capabilities

Browserstack has a few more custom capabilities. You can find the expected values [here](https://www.browserstack.com/automate/capabilities).
Browserstack has a some custom capabilities. You can find the expected values [here](https://www.browserstack.com/automate/capabilities).

- `BROWSERSTACK_DEBUG`
- `BROWSERSTACK_CONSOLE`
- `BROWSERSTACK_NETWORK_LOGS`
- `BROWSERSTACK_VIDEO`
- `BROWSERSTACK_TIMEZONE`

## Specyfing Chrome Command Line Arguments

To set [Chrome command line arguments](https://peter.sh/experiments/chromium-command-line-switches/), use the `BROWSERSTACK_CHROME_ARGS` environment variable. You can specify multiple arguments by joining them with the space symbol. This option works only if the [Browserstack Automate API is enabled](https://github.com/ondrejbartas/testcafe-browser-provider-browserstack/#browserstack-js-testing-and-browserstack-automate).

Examples:
```
export BROWSERSTACK_USE_AUTOMATE="1"
export BROWSERSTACK_CHROME_ARGS="--autoplay-policy=no-user-gesture-required"
testcafe browserstack:chrome test.js
```
```
export BROWSERSTACK_USE_AUTOMATE="1"
export BROWSERSTACK_CHROME_ARGS="--start-maximized --autoplay-policy=no-user-gesture-required"
testcafe browserstack:chrome test.js
```

## Author
Developer Express Inc. (https://devexpress.com)
14 changes: 6 additions & 8 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "testcafe-browser-provider-browserstack",
"version": "1.5.0",
"version": "1.7.0",
"description": "browserstack TestCafe browser provider plugin.",
"repository": "https://github.com/DevExpress/testcafe-browser-provider-browserstack",
"engines": {
Expand Down Expand Up @@ -30,12 +30,12 @@
"license": "MIT",
"dependencies": {
"babel-runtime": "^6.11.6",
"browserstack-local": "^1.3.0",
"browserstack-local": "^1.3.6",
"desired-capabilities": "^0.1.0",
"jimp": "^0.2.27",
"jimp": "^0.2.28",
"os-family": "^1.0.0",
"pinkie": "^2.0.4",
"request": "^2.79.0",
"request": "^2.88.0",
"request-promise": "^4.1.1"
},
"devDependencies": {
Expand All @@ -48,13 +48,11 @@
"chai": "^3.5.0",
"del": "^2.2.2",
"execa": "^0.9.0",
"gulp": "^3.9.1",
"gulp": "^4.0.0",
"gulp-babel": "^6.1.2",
"gulp-eslint": "^3.0.1",
"gulp-sequence": "^0.4.6",
"mocha": "^5.0.1",
"node-version": "^1.0.0",
"publish-please": "^2.1.4",
"publish-please": "^5.4.3",
"testcafe": "latest",
"tmp": "0.0.31"
}
Expand Down
2 changes: 1 addition & 1 deletion src/backends/automate.js
Expand Up @@ -63,7 +63,7 @@ const BROWSERSTACK_API_PATHS = {
function requestApi (path, params) {
return requestApiBase(path, params)
.then(response => {
if (response.status !== 0)
if (response.status)
throw new Error(`API error ${response.status}: ${response.value.message}`);

return response;
Expand Down
18 changes: 12 additions & 6 deletions src/index.js
Expand Up @@ -5,13 +5,13 @@ import BrowserstackConnector from './connector';
import JSTestingBackend from './backends/js-testing';
import AutomateBackend from './backends/automate';
import BrowserProxy from './browser-proxy';
import isEnvVarTrue from './utils/is-env-var-true';

const ANDROID_PROXY_RESPONSE_DELAY = 500;

function isAutomateEnabled () {
return process.env['BROWSERSTACK_USE_AUTOMATE'] && process.env['BROWSERSTACK_USE_AUTOMATE'] !== '0';
}

const isAutomateEnabled = () => isEnvVarTrue('BROWSERSTACK_USE_AUTOMATE');
const isLocalEnabled = () => !isEnvVarTrue('BROWSERSTACK_NO_LOCAL');

export default {
// Multiple browsers support
Expand Down Expand Up @@ -57,7 +57,7 @@ export default {
_getConnector () {
this.connectorPromise = this.connectorPromise
.then(async connector => {
if (!connector) {
if (!connector && isLocalEnabled()) {
connector = new BrowserstackConnector(process.env['BROWSERSTACK_ACCESS_KEY']);

await connector.create();
Expand Down Expand Up @@ -184,8 +184,14 @@ export default {
this._addEnvironmentPreferencesToCapabilities(capabilities);

capabilities.name = `TestCafe test run ${id}`;
capabilities.localIdentifier = connector.connectorInstance.localIdentifierFlag;
capabilities.local = true;

if (connector) {
capabilities.localIdentifier = connector.connectorInstance.localIdentifierFlag;
capabilities.local = true;
}

if (browserName.indexOf('chrome') !== -1 && process.env['BROWSERSTACK_CHROME_ARGS'] && process.env['BROWSERSTACK_CHROME_ARGS'].length > 0)
capabilities.chromeOptions = { args: [process.env['BROWSERSTACK_CHROME_ARGS']] };

await this.backend.openBrowser(id, pageUrl, capabilities);

Expand Down
3 changes: 3 additions & 0 deletions src/utils/is-env-var-true.js
@@ -0,0 +1,3 @@
export default function (envVar) {
return process.env[envVar] && process.env[envVar] !== '0';
}
6 changes: 3 additions & 3 deletions test/mocha/browser-names-test.js
Expand Up @@ -28,10 +28,10 @@ describe('Browser names', function () {
'ie@10.0:Windows 8',
'ie@11.0:Windows 8.1',
'edge@15.0:Windows 10',
'iPhone 6@8.3',
'iPhone 7@10.3',
'iPhone SE@11.2',
'iPad Pro@11.2',
'Google Nexus 5@5.0'
'iPhone XR@12.1',
'Google Nexus 6@6.0'
]);
});
});
Expand Down

0 comments on commit 292849c

Please sign in to comment.