Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use any browser with BrowserStack and Appium. #3634

Merged
merged 6 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lib/transport/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ module.exports = class TransportFactory {
browserName = Browser.EDGE;
} else if (BrowsersLowerCase[browserName && browserName.toLowerCase()]) {
browserName = BrowsersLowerCase[browserName.toLowerCase()];
} else {
const didYouMean = require('didyoumean');
const browsersList = Object.values(Browser);
const resultMeant = didYouMean(browserName, browsersList);

throw new Error(`Unknown browser: "${browserName}"${resultMeant ? ('; did you mean "' + resultMeant + '"?') : ''}`);
}

return browserName;
Expand Down Expand Up @@ -154,8 +148,13 @@ module.exports = class TransportFactory {
Driver = require('./selenium-webdriver/safari.js');
break;

default:
throw new Error(`Unrecognized browser: ${browserName}.`);
default: {
const didYouMean = require('didyoumean');
const browsersList = Object.values(Browser);
const resultMeant = didYouMean(browserName, browsersList);

throw new Error(`Unknown browser: "${browserName}"${resultMeant ? ('; did you mean "' + resultMeant + '"?') : ''}`);
}
}

return new Driver(nightwatchInstance);
Expand Down
10 changes: 8 additions & 2 deletions lib/transport/selenium-webdriver/selenium.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const {Capabilities} = require('selenium-webdriver');

const DefaultSeleniumDriver = require('./');
const SeleniumServiceBuilder = require('./service-builders/selenium.js');


module.exports = class SeleniumServer extends DefaultSeleniumDriver {
/**
* Used when running in parallel with start_process=true
Expand Down Expand Up @@ -66,6 +69,11 @@ module.exports = class SeleniumServer extends DefaultSeleniumDriver {
}

setBuilderOptions({builder, options}) {
if (!(options instanceof Capabilities)) {
const browserName = this.initialCapabilities.getBrowserName();
throw new Error(`Unknown browser: '${browserName}'`);
}

const serverUrl = this.getServerUrl();

builder
Expand All @@ -74,8 +82,6 @@ module.exports = class SeleniumServer extends DefaultSeleniumDriver {

return super.setBuilderOptions({builder, options});
}


};


66 changes: 40 additions & 26 deletions test/src/core/testCreateSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ describe('test Request With Credentials', function () {
});

it('Test blank browserName', async function () {
assert.throws(function() {
try {
const client = Nightwatch.createClient({
selenium_port: 10195,
silent: false,
Expand All @@ -386,7 +386,13 @@ describe('test Request With Credentials', function () {
alwaysMatch: {}
}
});
}, /Error: Unknown browser:/);

await client.createSession();

assert.fail('Error expected.');
} catch (err) {
assert.strictEqual(err.message.includes('[Error] Unknown browser: \'undefined\''), true);
}
});

it('Test create session with browsername null - Appium support', async function () {
Expand Down Expand Up @@ -433,18 +439,32 @@ describe('test Request With Credentials', function () {
});
});

it('Test create session with use_appium property - Appium support', async function () {
it('Test create session with use_appium property and random browser - Appium support', async function () {
nock('http://somewhere:9999')
.post('/wd/hub/session')
.reply(201, {
value: {
.reply(201, function (uri, requestBody) {
assert.deepEqual(requestBody, {
capabilities: {
platformName: 'android',
platformVersion: '12.0',
name: 'sample test goes here'
},
sessionId: '1352110219202'
}
firstMatch: [{}],
alwaysMatch: {
browserName: 'acmeBrowser',
platformName: 'android',
'appium:platformVersion': '12.0'
}
}
});

return {
value: {
capabilities: {
browserName: 'acmeBrowser',
platformName: 'android',
platformVersion: '12.0',
name: 'sample test goes here'
},
sessionId: '1352110219202'
}
};
});

const client = Nightwatch.createClient({
Expand All @@ -458,15 +478,16 @@ describe('test Request With Credentials', function () {
port: 9999
},
desiredCapabilities: {
browserName: '',
browserName: 'acmeBrowser',
platformName: 'android',
platformVersion: '12.0'
'appium:platformVersion': '12.0'
}
});

const result = await client.createSession();
assert.deepStrictEqual(result, {
capabilities: {
browserName: 'acmeBrowser',
platformName: 'android',
platformVersion: '12.0',
name: 'sample test goes here'
Expand Down Expand Up @@ -925,25 +946,23 @@ describe('test Request With Credentials', function () {
});
});

it('Test create session with browserstack and update buildName', async function () {
it('Test create session with browserstack with random browser and update buildName', async function () {
nock('https://hub.browserstack.com')
.post('/wd/hub/session')
.reply(201, function (uri, requestBody) {

assert.deepEqual(requestBody, {
capabilities: {
firstMatch: [{}],
alwaysMatch: {
browserName: 'chrome',
browserName: 'acmeBrowser',
'bstack:options': {
local: 'false',
userName: 'test_user',
accessKey: 'test_key',
os: 'OS X',
osVersion: 'Monterey',
buildName: 'Nightwatch Programmatic Api Demo'
},
'goog:chromeOptions': {w3c: false}
}
}
}
});
Expand Down Expand Up @@ -995,12 +1014,8 @@ describe('test Request With Credentials', function () {
os: 'OS X',
osVersion: 'Monterey'
},
browserName: 'chrome',
chromeOptions: {
w3c: false
}
browserName: 'acmeBrowser'
},

parallel: false
});

Expand All @@ -1015,16 +1030,15 @@ describe('test Request With Credentials', function () {
capabilities: {
firstMatch: [{}],
alwaysMatch: {
browserName: 'chrome',
browserName: 'acmeBrowser',
'bstack:options': {
local: 'false',
userName: 'test_user',
accessKey: 'test_key',
os: 'OS X',
osVersion: 'Monterey',
buildName: 'Nightwatch Programmatic Api Demo'
},
'goog:chromeOptions': {w3c: false}
}
}
}
});
Expand Down