Skip to content

Commit 58f55a9

Browse files
authored
Fix getting sessionId; Bump version (v1.7.1) (#68)
1 parent f224788 commit 58f55a9

7 files changed

Lines changed: 70 additions & 25 deletions

File tree

Gulpfile.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ function build () {
3434
.pipe(gulp.dest('lib'));
3535
}
3636

37-
function testMocha () {
37+
function ensureAuthCredentials () {
38+
const ERROR_MESSAGES = require('./lib/templates/error-messages');
39+
3840
if (!process.env.BROWSERSTACK_USERNAME || !process.env.BROWSERSTACK_ACCESS_KEY)
39-
throw new Error('Specify your credentials by using the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables to authenticate to BrowserStack.');
41+
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());
42+
}
4043

44+
function testMocha () {
45+
ensureAuthCredentials();
4146

4247
var mochaCmd = path.join(__dirname, 'node_modules/.bin/mocha');
4348

@@ -68,8 +73,7 @@ function testMochaAutomate () {
6873
}
6974

7075
function testTestcafe () {
71-
if (!process.env.BROWSERSTACK_USERNAME || !process.env.BROWSERSTACK_ACCESS_KEY)
72-
throw new Error('Specify your credentials by using the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables to authenticate to BrowserStack.');
76+
ensureAuthCredentials();
7377

7478
var testCafeCmd = path.join(__dirname, 'node_modules/.bin/testcafe');
7579

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "testcafe-browser-provider-browserstack",
3-
"version": "1.7.0",
3+
"version": "1.7.1",
44
"description": "browserstack TestCafe browser provider plugin.",
55
"repository": "https://github.com/DevExpress/testcafe-browser-provider-browserstack",
66
"engines": {
@@ -31,6 +31,7 @@
3131
"dependencies": {
3232
"babel-runtime": "^6.11.6",
3333
"browserstack-local": "^1.3.6",
34+
"dedent": "^0.7.0",
3435
"desired-capabilities": "^0.1.0",
3536
"jimp": "^0.2.28",
3637
"os-family": "^1.0.0",

src/api-request.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import Promise from 'pinkie';
22
import request from 'request-promise';
33
import delay from './utils/delay';
4+
import * as ERROR_MESSAGES from './templates/error-messages';
45

56

67
const BUILD_ID = process.env['BROWSERSTACK_BUILD_ID'];
78
const PROJECT_NAME = process.env['BROWSERSTACK_PROJECT_NAME'];
89

9-
const AUTH_FAILED_ERROR = 'Authentication failed. Please assign the correct username and access key ' +
10-
'to the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.';
11-
1210
const API_REQUEST_DELAY = 100;
1311

1412
let apiRequestPromise = Promise.resolve(null);
1513

1614

1715
export default function (apiPath, params) {
1816
if (!process.env['BROWSERSTACK_USERNAME'] || !process.env['BROWSERSTACK_ACCESS_KEY'])
19-
throw new Error(AUTH_FAILED_ERROR);
17+
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());
2018

2119
var url = apiPath.url;
2220

@@ -43,7 +41,7 @@ export default function (apiPath, params) {
4341
.then(() => request(url, opts))
4442
.catch(error => {
4543
if (error.statusCode === 401)
46-
throw new Error(AUTH_FAILED_ERROR);
44+
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());
4745

4846
throw error;
4947
});

src/backends/automate.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { inspect } from 'util';
12
import Promise from 'pinkie';
23
import jimp from 'jimp';
34
import BaseBackend from './base';
45
import requestApiBase from '../utils/request-api';
56
import createBrowserstackStatus from '../utils/create-browserstack-status';
7+
import * as ERROR_MESSAGES from '../templates/error-messages';
68

79

810
const API_POLLING_INTERVAL = 80000;
@@ -63,8 +65,12 @@ const BROWSERSTACK_API_PATHS = {
6365
function requestApi (path, params) {
6466
return requestApiBase(path, params)
6567
.then(response => {
66-
if (response.status)
67-
throw new Error(`API error ${response.status}: ${response.value.message}`);
68+
if (response.status) {
69+
throw new Error(ERROR_MESSAGES.REMOTE_API_REQUEST_FAILED({
70+
status: response.status,
71+
apiResponse: response.value && response.value.message || inspect(response)
72+
}));
73+
}
6874

6975
return response;
7076
});
@@ -87,6 +93,22 @@ export default class AutomateBackend extends BaseBackend {
8793
this.sessions = {};
8894
}
8995

96+
static _ensureSessionId (sessionInfo) {
97+
const sessionData = sessionInfo.value || {};
98+
const sessionCapabilities = sessionData.capabilities || {};
99+
100+
sessionInfo.sessionId = sessionInfo.sessionId ||
101+
sessionData.sessionId ||
102+
sessionData['webdriver.remote.sessionid'] ||
103+
sessionCapabilities['webdriver.remote.sessionid'];
104+
105+
if (!sessionInfo.sessionId) {
106+
throw new Error(ERROR_MESSAGES.SESSION_ID_NOT_FOUND({
107+
sessionInfoDump: inspect(sessionInfo)
108+
}));
109+
}
110+
}
111+
90112
async _requestSessionUrl (id) {
91113
var sessionInfo = await requestApiBase(BROWSERSTACK_API_PATHS.getStatus(this.sessions[id].sessionId));
92114

@@ -127,6 +149,8 @@ export default class AutomateBackend extends BaseBackend {
127149
executeImmediately: true
128150
});
129151

152+
AutomateBackend._ensureSessionId(this.sessions[id]);
153+
130154
this.sessions[id].sessionUrl = await this._requestSessionUrl(id);
131155

132156
var sessionId = this.sessions[id].sessionId;

src/backends/base.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
1+
import * as ERROR_MESSAGES from '../templates/error-messages';
2+
3+
14
export default class BaseBackend {
25
constructor (reportWarning) {
36
this.reportWarning = reportWarning;
47
}
58

69
async getBrowsersList () {
7-
throw new Error('Not implemented');
10+
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
811
}
912

1013
async openBrowser (/*id, pageUrl, capabilities*/) {
11-
throw new Error('Not implemented');
14+
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
1215
}
1316

1417
async closeBrowser (/*id*/) {
15-
throw new Error('Not implemented');
18+
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
1619
}
1720

1821
async takeScreenshot (/*id, path*/) {
19-
throw new Error('Not implemented');
22+
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
2023
}
2124

2225
async resizeWindow (/*id, width, height, currentWidth, currentHeight*/) {
23-
throw new Error('Not implemented');
26+
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
2427
}
2528

2629
async maximizeWindow (/*id*/) {
27-
throw new Error('Not implemented');
30+
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
2831
}
2932

3033
async reportJobResult (/*id, jobStatus, jobData, possibleStatuses*/) {
31-
throw new Error('Not implemented');
34+
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
3235
}
3336

3437
getSessionUrl (/*id*/) {
35-
throw new Error('Not implemented');
38+
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
3639
}
3740
}

src/templates/error-messages.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import dedent from 'dedent';
2+
3+
4+
export const BROWSERSTACK_AUTHENTICATION_FAILED = () => 'Authentication failed. Please assign the correct username and access key to the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.';
5+
export const API_METHOD_NOT_IMPLEMENTED = () => 'The API method is not implemented';
6+
7+
export const REMOTE_API_REQUEST_FAILED = ({ status, apiResponse }) => dedent `
8+
API error ${status}:
9+
10+
${apiResponse}
11+
`;
12+
13+
export const SESSION_ID_NOT_FOUND = ({ sessionInfoDump }) => dedent `
14+
Unable to find a session ID in the following session information:
15+
16+
${sessionInfoDump}
17+
`;

src/utils/request-api.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import Promise from 'pinkie';
22
import request from 'request-promise';
33
import delay from './delay';
4+
import * as ERROR_MESSAGES from '../templates/error-messages';
45

56

6-
const AUTH_FAILED_ERROR = 'Authentication failed. Please assign the correct username and access key ' +
7-
'to the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.';
8-
97
const API_REQUEST_DELAY = 100;
108

119
let apiRequestPromise = Promise.resolve(null);
1210

1311
export default function (apiPath, params = {}) {
1412
if (!process.env['BROWSERSTACK_USERNAME'] || !process.env['BROWSERSTACK_ACCESS_KEY'])
15-
throw new Error(AUTH_FAILED_ERROR);
13+
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());
1614

1715
var url = apiPath.url;
1816

@@ -42,7 +40,7 @@ export default function (apiPath, params = {}) {
4240
.then(() => request(url, opts))
4341
.catch(error => {
4442
if (error.statusCode === 401)
45-
throw new Error(AUTH_FAILED_ERROR);
43+
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());
4644

4745
throw error;
4846
});

0 commit comments

Comments
 (0)