Skip to content

Commit

Permalink
Fixed #3998 – Cannot set custom session status for retried test when …
Browse files Browse the repository at this point in the history
…using Browserstack (#3999)
  • Loading branch information
garg3133 committed Jan 24, 2024
1 parent 06679a4 commit c5331d2
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 16 deletions.
5 changes: 5 additions & 0 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ class HttpRequest extends EventEmitter {
const url = new URL(options.url);
reqOptions.path = url.pathname;
reqOptions.host = url.hostname;

if (url.search && reqOptions.method === HttpUtil.Method.GET) {
// append search query parameters to path.
reqOptions.path += url.search;
}
}

this.auth = options.auth || null;
Expand Down
19 changes: 17 additions & 2 deletions lib/transport/selenium-webdriver/browserstack/browserstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Browserstack extends AppiumBaseServer {
async getBuildId() {
try {
const builds = await this.sendHttpRequest({
url: `${this.ApiUrl}/builds.json`,
url: `${this.ApiUrl}/builds.json?status=running`,
method: 'GET',
use_ssl: true,
auth: {
Expand All @@ -120,6 +120,22 @@ class Browserstack extends AppiumBaseServer {
}

async sendReasonToBrowserstack(isFailure = false, reason = '') {
const sessionDetails = await this.sendHttpRequest({
url: `${this.ApiUrl}/sessions/${this.sessionId}.json`,
method: 'GET',
use_ssl: true,
auth: {
user: this.username,
pass: this.accessKey
}
});

const status = sessionDetails?.automation_session?.status;
if (['passed', 'failed'].includes(status)) {
// status has already been set by user
return;
}

reason = stripAnsi(reason);
await this.sendHttpRequest({
url: `${this.ApiUrl}/sessions/${this.sessionId}.json`,
Expand All @@ -134,7 +150,6 @@ class Browserstack extends AppiumBaseServer {
pass: this.accessKey
}
});

}

async sessionFinished(reason, err) {
Expand Down
51 changes: 46 additions & 5 deletions test/src/core/testCreateSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@ describe('test Request With Credentials', function () {
});

nock('https://api.browserstack.com')
.get('/automate/builds.json')
.get('/automate/builds.json?status=running')
.times(2)
.reply(200, [
{
automation_build: {
Expand Down Expand Up @@ -620,6 +621,9 @@ describe('test Request With Credentials', function () {
}
});

const buildId = await client.transport.getBuildId();
assert.strictEqual(buildId, undefined);

assert.strictEqual(client.api.isAppiumClient(), false);
});

Expand Down Expand Up @@ -667,7 +671,8 @@ describe('test Request With Credentials', function () {
});

nock('https://api.browserstack.com')
.get('/app-automate/builds.json')
.get('/app-automate/builds.json?status=running')
.times(2)
.reply(200, [
{
automation_build: {
Expand Down Expand Up @@ -733,6 +738,9 @@ describe('test Request With Credentials', function () {
realMobile: true
}
});

const buildId = await client.transport.getBuildId();
assert.strictEqual(buildId, undefined);

assert.strictEqual(client.transport.uploadedAppUrl, 'bs://878bdf21505f0004ce');

Expand Down Expand Up @@ -796,8 +804,16 @@ describe('test Request With Credentials', function () {
});

nock('https://api.browserstack.com')
.get('/app-automate/builds.json')
.get('/app-automate/builds.json?status=running')
.times(2)
.reply(200, [
{
automation_build: {
name: 'Nightwatch Programmatic Api Demo',
status: 'running',
hashed_id: '123456789'
}
},
{
automation_build: {
name: 'WIN_CHROME_PROD_SANITY_LIVE_1831',
Expand Down Expand Up @@ -865,6 +881,9 @@ describe('test Request With Credentials', function () {
}
});

const buildId = await client.transport.getBuildId();
assert.strictEqual(buildId, '123456789');

assert.strictEqual(client.transport.uploadedAppUrl, undefined);

assert.strictEqual(client.settings.selenium.use_appium, undefined);
Expand Down Expand Up @@ -902,8 +921,16 @@ describe('test Request With Credentials', function () {
});

nock('https://api.browserstack.com')
.get('/automate/builds.json')
.get('/automate/builds.json?status=running')
.times(2)
.reply(200, [
{
automation_build: {
name: 'nightwatch-test-build',
status: 'running',
hashed_id: '123-456-789'
}
},
{
automation_build: {
name: 'WIN_CHROME_PROD_SANITY_LIVE_1831',
Expand Down Expand Up @@ -968,6 +995,9 @@ describe('test Request With Credentials', function () {
}
}
});

const buildId = await client.transport.getBuildId();
assert.strictEqual(buildId, '123-456-789');
});

it('Test create session with browserstack with random browser and update buildName', async function () {
Expand Down Expand Up @@ -1001,8 +1031,16 @@ describe('test Request With Credentials', function () {
});

nock('https://api.browserstack.com')
.get('/automate/builds.json')
.get('/automate/builds.json?status=running')
.times(2)
.reply(200, [
{
automation_build: {
name: 'Nightwatch Programmatic Api Demo',
status: 'running',
hashed_id: '123456789'
}
},
{
automation_build: {
name: 'WIN_CHROME_PROD_SANITY_LIVE_1831',
Expand Down Expand Up @@ -1070,5 +1108,8 @@ describe('test Request With Credentials', function () {
}
}
});

const buildId = await client.transport.getBuildId();
assert.strictEqual(buildId, '123456789');
});
});
23 changes: 19 additions & 4 deletions test/src/index/transport/testBrowserstackTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ xdescribe('BrowserstackTransport', function () {
});

nock('https://api.browserstack.com')
.get('/automate/builds.json')
.get('/automate/builds.json?status=running')
.reply(200, [
{
automation_build: {
Expand Down Expand Up @@ -129,7 +129,7 @@ xdescribe('BrowserstackTransport', function () {
});

nock('https://api.browserstack.com')
.get('/automate/builds.json')
.get('/automate/builds.json?status=running')
.reply(200, [
{
automation_build: {
Expand Down Expand Up @@ -160,6 +160,11 @@ xdescribe('BrowserstackTransport', function () {
assert.strictEqual(transport.accessKey, 'test-access-key');
assert.strictEqual(client.settings.webdriver.start_process, false);

nock('https://api.browserstack.com')
.get('/automate/sessions/1234567.json')
.reply(200, {
automation_session: {status: 'done'}
});
nock('https://api.browserstack.com')
.put('/automate/sessions/1234567.json', {
status: 'passed',
Expand Down Expand Up @@ -201,7 +206,7 @@ xdescribe('BrowserstackTransport', function () {
});

nock('https://api.browserstack.com')
.get('/app-automate/builds.json')
.get('/app-automate/builds.json?status=running')
.reply(200, [
{
automation_build: {
Expand Down Expand Up @@ -232,6 +237,11 @@ xdescribe('BrowserstackTransport', function () {
assert.strictEqual(transport.accessKey, 'test-access-key');
assert.strictEqual(client.settings.webdriver.start_process, false);

nock('https://api.browserstack.com')
.get('/app-automate/sessions/1234567.json')
.reply(200, {
automation_session: {status: 'done'}
});
nock('https://api.browserstack.com')
.put('/app-automate/sessions/1234567.json', {
status: 'passed',
Expand Down Expand Up @@ -275,7 +285,7 @@ xdescribe('BrowserstackTransport', function () {
});

nock('https://api.browserstack.com')
.get('/automate/builds.json')
.get('/automate/builds.json?status=running')
.reply(200, [
{
automation_build: {
Expand All @@ -298,6 +308,11 @@ xdescribe('BrowserstackTransport', function () {
return new Promise((resolve, reject) => {
setTimeout(async function() {
try {
nock('https://api.browserstack.com')
.get('/automate/sessions/1234567.json')
.reply(200, {
automation_session: {status: 'done'}
});
nock('https://api.browserstack.com')
.put('/automate/sessions/1234567.json', {
status: 'failed',
Expand Down
36 changes: 31 additions & 5 deletions test/src/runner/testRunBrowserstackTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const {settings} = common;
const {runTests} = common.require('index.js');

describe('testRunBrowserstackTransport', function() {
let nockCalled = 0;
let statusSetNockCalled = 0;
let sessionNockCalled = 0;

before(function(done) {
try {
Expand All @@ -31,7 +32,7 @@ describe('testRunBrowserstackTransport', function() {
});

nock('https://api.browserstack.com')
.get('/automate/builds.json')
.get('/automate/builds.json?status=running')
.times(2)
.reply(200, []);

Expand All @@ -57,14 +58,38 @@ describe('testRunBrowserstackTransport', function() {
value: []
});

nock('https://api.browserstack.com')
.get('/automate/sessions/1352110219202.json')
.reply(200, function() {
sessionNockCalled++;

return {
automation_session: {
status: 'failed'
}
};
});

nock('https://api.browserstack.com')
.get('/automate/sessions/1352110219202.json')
.reply(200, function() {
sessionNockCalled++;

return {
automation_session: {
status: 'done'
}
};
});

nock('https://api.browserstack.com')
.put('/automate/sessions/1352110219202.json', {
status: 'failed',
reason: /^NightwatchAssertError: Testing if element <#weblogin> is present in 10ms/
})
.times(2)
.times(1)
.reply(200, function() {
nockCalled++;
statusSetNockCalled++;

return {};
});
Expand Down Expand Up @@ -103,7 +128,8 @@ describe('testRunBrowserstackTransport', function() {
waitForConditionTimeout: 11,
retryAssertionTimeout: 10,
reporter(results) {
assert.strictEqual(nockCalled, 2);
assert.strictEqual(sessionNockCalled, 2);
assert.strictEqual(statusSetNockCalled, 1);
assert.strictEqual(Object.keys(results.modules).length, 1);
}
}
Expand Down

0 comments on commit c5331d2

Please sign in to comment.