Skip to content

Commit

Permalink
Add option to not load Nightwatch APIs while launching browser. (#4358)
Browse files Browse the repository at this point in the history
* Add option to not load Nightwatch APIs while launching browser.

* Add test.
  • Loading branch information
garg3133 authored Jan 16, 2025
1 parent 32b40a6 commit e4e259d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,10 +680,12 @@ class NightwatchClient extends EventEmitter {
// Initialize the APIs
//////////////////////////////////////////////////////////////////////////////////////////

async initialize() {
async initialize(loadNightwatchApis = true) {
this.loadKeyCodes();

return this.loadNightwatchApis();
if (loadNightwatchApis) {
return this.loadNightwatchApis();
}
}

setCurrentTest() {
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ module.exports.createClient = function({
return cliRunner.globals.runGlobalHook('after', [client.settings]);
},

launchBrowser() {
launchBrowser({loadNightwatchApis = true} = {}) {
const {argv} = cliRunner;

return client.initialize()
return client.initialize(loadNightwatchApis)
.then(() => {
return client.createSession({argv});
})
Expand Down
81 changes: 81 additions & 0 deletions test/src/index/testProgrammaticApis.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,4 +882,85 @@ describe('test programmatic apis', function () {
CliRunner.createDefaultConfig = createDefaultConfig;
CliRunner.prototype.loadConfig = loadConfig;
});

it('test multiple calls to launchBrowser() on same client', async function() {
const CliRunner = common.require('runner/cli/cli.js');
const Nightwatch = common.require('index.js');
MockServer.createChromeSession({sessionId: '12345678'});
MockServer.createChromeSession({sessionId: '87654321'});

const defaultConfig = {
test_settings: {
default: {
launchUrl: 'http://localhost'
},

chrome: {
desiredCapabilities: {
browserName: 'chrome'
}
}
},
selenium: {
port: 10195,
start_process: false
},
selenium_host: 'localhost'
};

const createDefaultConfig = CliRunner.createDefaultConfig;
const loadConfig = CliRunner.prototype.loadConfig;

CliRunner.createDefaultConfig = function(destFileName) {
return defaultConfig;
};

CliRunner.prototype.loadConfig = function () {
return defaultConfig;
};

const clientChrome = Nightwatch.createClient({
browserName: 'chrome',
headless: true
});

const session = await clientChrome.launchBrowser();

assert.strictEqual(session.sessionId, '12345678');
assert.strictEqual(session.options.webdriver.port, 10195);
assert.deepStrictEqual(session.capabilities, {
acceptInsecureCerts: false,
browserName: 'chrome',
browserVersion: '90'
});

await session.end();
assert.strictEqual(session.sessionId, null);

let launchBrowserError;
try {
await clientChrome.launchBrowser();
} catch (err) {
launchBrowserError = err;
}
assert.notStrictEqual(launchBrowserError, undefined);
assert.strictEqual(launchBrowserError.message.includes('Error while loading the API commands'), true);

const session2 = await clientChrome.launchBrowser({loadNightwatchApis: false});

assert.strictEqual(session2.sessionId, '87654321');
assert.strictEqual(session2.options.webdriver.port, 10195);
assert.deepStrictEqual(session2.capabilities, {
acceptInsecureCerts: false,
browserName: 'chrome',
browserVersion: '90'
});

await session2.quit();
// TODO: calling `.quit()` does not clear the sessionId
assert.notStrictEqual(session2.sessionId, null);

CliRunner.createDefaultConfig = createDefaultConfig;
CliRunner.prototype.loadConfig = loadConfig;
});
});

0 comments on commit e4e259d

Please sign in to comment.