Skip to content

Commit

Permalink
Fix getting sessionId; Bump version (v1.7.1) (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyBelym authored Jan 24, 2019
1 parent f224788 commit 58f55a9
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 25 deletions.
12 changes: 8 additions & 4 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ function build () {
.pipe(gulp.dest('lib'));
}

function testMocha () {
function ensureAuthCredentials () {
const ERROR_MESSAGES = require('./lib/templates/error-messages');

if (!process.env.BROWSERSTACK_USERNAME || !process.env.BROWSERSTACK_ACCESS_KEY)
throw new Error('Specify your credentials by using the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables to authenticate to BrowserStack.');
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());
}

function testMocha () {
ensureAuthCredentials();

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

Expand Down Expand Up @@ -68,8 +73,7 @@ function testMochaAutomate () {
}

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

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

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "testcafe-browser-provider-browserstack",
"version": "1.7.0",
"version": "1.7.1",
"description": "browserstack TestCafe browser provider plugin.",
"repository": "https://github.com/DevExpress/testcafe-browser-provider-browserstack",
"engines": {
Expand Down Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"babel-runtime": "^6.11.6",
"browserstack-local": "^1.3.6",
"dedent": "^0.7.0",
"desired-capabilities": "^0.1.0",
"jimp": "^0.2.28",
"os-family": "^1.0.0",
Expand Down
8 changes: 3 additions & 5 deletions src/api-request.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import Promise from 'pinkie';
import request from 'request-promise';
import delay from './utils/delay';
import * as ERROR_MESSAGES from './templates/error-messages';


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

const AUTH_FAILED_ERROR = 'Authentication failed. Please assign the correct username and access key ' +
'to the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.';

const API_REQUEST_DELAY = 100;

let apiRequestPromise = Promise.resolve(null);


export default function (apiPath, params) {
if (!process.env['BROWSERSTACK_USERNAME'] || !process.env['BROWSERSTACK_ACCESS_KEY'])
throw new Error(AUTH_FAILED_ERROR);
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());

var url = apiPath.url;

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

throw error;
});
Expand Down
28 changes: 26 additions & 2 deletions src/backends/automate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { inspect } from 'util';
import Promise from 'pinkie';
import jimp from 'jimp';
import BaseBackend from './base';
import requestApiBase from '../utils/request-api';
import createBrowserstackStatus from '../utils/create-browserstack-status';
import * as ERROR_MESSAGES from '../templates/error-messages';


const API_POLLING_INTERVAL = 80000;
Expand Down Expand Up @@ -63,8 +65,12 @@ const BROWSERSTACK_API_PATHS = {
function requestApi (path, params) {
return requestApiBase(path, params)
.then(response => {
if (response.status)
throw new Error(`API error ${response.status}: ${response.value.message}`);
if (response.status) {
throw new Error(ERROR_MESSAGES.REMOTE_API_REQUEST_FAILED({
status: response.status,
apiResponse: response.value && response.value.message || inspect(response)
}));
}

return response;
});
Expand All @@ -87,6 +93,22 @@ export default class AutomateBackend extends BaseBackend {
this.sessions = {};
}

static _ensureSessionId (sessionInfo) {
const sessionData = sessionInfo.value || {};
const sessionCapabilities = sessionData.capabilities || {};

sessionInfo.sessionId = sessionInfo.sessionId ||
sessionData.sessionId ||
sessionData['webdriver.remote.sessionid'] ||
sessionCapabilities['webdriver.remote.sessionid'];

if (!sessionInfo.sessionId) {
throw new Error(ERROR_MESSAGES.SESSION_ID_NOT_FOUND({
sessionInfoDump: inspect(sessionInfo)
}));
}
}

async _requestSessionUrl (id) {
var sessionInfo = await requestApiBase(BROWSERSTACK_API_PATHS.getStatus(this.sessions[id].sessionId));

Expand Down Expand Up @@ -127,6 +149,8 @@ export default class AutomateBackend extends BaseBackend {
executeImmediately: true
});

AutomateBackend._ensureSessionId(this.sessions[id]);

this.sessions[id].sessionUrl = await this._requestSessionUrl(id);

var sessionId = this.sessions[id].sessionId;
Expand Down
19 changes: 11 additions & 8 deletions src/backends/base.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
import * as ERROR_MESSAGES from '../templates/error-messages';


export default class BaseBackend {
constructor (reportWarning) {
this.reportWarning = reportWarning;
}

async getBrowsersList () {
throw new Error('Not implemented');
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
}

async openBrowser (/*id, pageUrl, capabilities*/) {
throw new Error('Not implemented');
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
}

async closeBrowser (/*id*/) {
throw new Error('Not implemented');
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
}

async takeScreenshot (/*id, path*/) {
throw new Error('Not implemented');
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
}

async resizeWindow (/*id, width, height, currentWidth, currentHeight*/) {
throw new Error('Not implemented');
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
}

async maximizeWindow (/*id*/) {
throw new Error('Not implemented');
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
}

async reportJobResult (/*id, jobStatus, jobData, possibleStatuses*/) {
throw new Error('Not implemented');
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
}

getSessionUrl (/*id*/) {
throw new Error('Not implemented');
throw new Error(ERROR_MESSAGES.API_METHOD_NOT_IMPLEMENTED());
}
}
17 changes: 17 additions & 0 deletions src/templates/error-messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import dedent from 'dedent';


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.';
export const API_METHOD_NOT_IMPLEMENTED = () => 'The API method is not implemented';

export const REMOTE_API_REQUEST_FAILED = ({ status, apiResponse }) => dedent `
API error ${status}:
${apiResponse}
`;

export const SESSION_ID_NOT_FOUND = ({ sessionInfoDump }) => dedent `
Unable to find a session ID in the following session information:
${sessionInfoDump}
`;
8 changes: 3 additions & 5 deletions src/utils/request-api.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import Promise from 'pinkie';
import request from 'request-promise';
import delay from './delay';
import * as ERROR_MESSAGES from '../templates/error-messages';


const AUTH_FAILED_ERROR = 'Authentication failed. Please assign the correct username and access key ' +
'to the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.';

const API_REQUEST_DELAY = 100;

let apiRequestPromise = Promise.resolve(null);

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

var url = apiPath.url;

Expand Down Expand Up @@ -42,7 +40,7 @@ export default function (apiPath, params = {}) {
.then(() => request(url, opts))
.catch(error => {
if (error.statusCode === 401)
throw new Error(AUTH_FAILED_ERROR);
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());

throw error;
});
Expand Down

0 comments on commit 58f55a9

Please sign in to comment.