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

Add option to not load Nightwatch APIs while launching browser. #4358

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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;
});
});
Loading