Skip to content

Commit

Permalink
programatic-api: add methods to call global hooks, add a cleanup meth…
Browse files Browse the repository at this point in the history
…od (#3822)
  • Loading branch information
swrdfish authored Jul 26, 2023
1 parent d8a3d73 commit d3de801
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 5 deletions.
16 changes: 15 additions & 1 deletion lib/core/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class CommandQueue extends EventEmitter {
const {compatMode} = this;
const parentContext = this.currentNode.context;

if (!this.deferred) {
this.deferred = Utils.createPromise();
}

const node = new Node({
name: commandName,
parent: this.currentNode,
Expand Down Expand Up @@ -133,12 +137,22 @@ class CommandQueue extends EventEmitter {
}
}

waitForCompletion() {
if (this.deferred) {
return this.deferred.promise;
}

return Promise.resolve();
}

run() {
if (this.tree.started) {
return this;
}

this.deferred = Utils.createPromise();
if (!this.deferred) {
this.deferred = Utils.createPromise();
}
this.scheduleTraverse();

return this.deferred.promise;
Expand Down
16 changes: 16 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ module.exports.createClient = function({
return client.mergeCapabilities(value);
},

runGlobalBeforeHook() {
return cliRunner.globals.runGlobalHook('before');
},

runGlobalAfterHook() {
return cliRunner.globals.runGlobalHook('after');
},

launchBrowser() {
const {argv} = cliRunner;

Expand All @@ -141,6 +149,14 @@ module.exports.createClient = function({
.then(_ => {
return client.api;
});
},

async cleanup() {
await client.queue.waitForCompletion();
client.queue.empty();
client.queue.reset();
client.queue.removeAllListeners();
Logger.reset();
}
};

Expand Down
17 changes: 14 additions & 3 deletions lib/utils/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function timestamp(d = new Date(), format) {
return d.toISOString();
}

let time = [
const time = [
pad(d.getHours()),
pad(d.getMinutes()),
pad(d.getSeconds())
Expand Down Expand Up @@ -76,8 +76,8 @@ function logMessage(type, message, args, alwaysShow) {
let messageStr = '';
let logMethod = 'log';
let prefix;
let d = new Date();
let timeIso = d.toISOString();
const d = new Date();
const timeIso = d.toISOString();
let timestamp = logTimestamp(d);

switch (type) {
Expand Down Expand Up @@ -482,3 +482,14 @@ module.exports.collectTestSectionOutput = function() {

return testSectionOutput;
};

module.exports.reset = function() {
const instance = Logger.getInstance();

if (!instance) {
return;
}

instance.testSectionOutput = [];
instance.output = [];
};
157 changes: 156 additions & 1 deletion test/src/index/testProgrammaticApis.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const mockery = require('mockery');
const MockServer = require('../../lib/command-mocks.js');
const common = require('../../common.js');
const {Logger} = common.require('utils');

describe('test programmatic apis', function () {
// [ '-vv', '--port=62625' ]
Expand Down Expand Up @@ -75,7 +76,8 @@ describe('test programmatic apis', function () {
assert.ok(!!global.browser);
assert.ok(!!global.browser.page);

assert.deepStrictEqual(Object.keys(client), ['updateCapabilities', 'launchBrowser']);
assert.deepStrictEqual(Object.keys(client), ['updateCapabilities', 'runGlobalBeforeHook',
'runGlobalAfterHook', 'launchBrowser', 'cleanup']);
assert.strictEqual(typeof client.launchBrowser, 'function');
assert.strictEqual(typeof client.settings, 'object');

Expand Down Expand Up @@ -640,4 +642,157 @@ describe('test programmatic apis', function () {
CliRunner.createDefaultConfig = createDefaultConfig;
CliRunner.prototype.loadConfig = loadConfig;
});

it('test runGlobalBeforeHook() programmatic API', async function() {
const CliRunner = common.require('runner/cli/cli.js');
const Nightwatch = common.require('index.js');
MockServer.createFirefoxSession({});

let globalBeforeCalled = false;

const defaultConfig = {
test_settings: {
default: {
launchUrl: 'http://localhost'
}
},
selenium: {
port: 10195,
start_process: false
},
selenium_host: 'localhost',

globals: {
before() {
globalBeforeCalled = true;
}
}
};

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

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

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

const client = Nightwatch.createClient({
headless: true,
silent: false,
output: false,
enable_global_apis: true
});

await client.runGlobalBeforeHook();

assert.ok(globalBeforeCalled);

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

it('test runGlobalAfterHook() programmatic API', async function() {
const CliRunner = common.require('runner/cli/cli.js');
const Nightwatch = common.require('index.js');
MockServer.createFirefoxSession({});

let globalAfterCalled = false;

const defaultConfig = {
test_settings: {
default: {
launchUrl: 'http://localhost'
}
},
selenium: {
port: 10195,
start_process: false
},
selenium_host: 'localhost',

globals: {
after() {
globalAfterCalled = true;
}
}
};

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

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

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

const client = Nightwatch.createClient({
headless: true,
silent: false,
output: false,
enable_global_apis: true
});

await client.runGlobalAfterHook();

assert.ok(globalAfterCalled);

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

it('test cleanup() programmatic API', async function() {
const CliRunner = common.require('runner/cli/cli.js');
const Nightwatch = common.require('index.js');
MockServer.createFirefoxSession({});

const defaultConfig = {
test_settings: {
default: {
launchUrl: 'http://localhost'
}
},
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 client = Nightwatch.createClient({
headless: true,
silent: false,
output: false,
enable_global_apis: true
});

const session = await client.launchBrowser();

await client.cleanup();

const httpOutput = Logger.collectOutput();
const httpSectionOutput = Logger.collectTestSectionOutput();

assert.equal(httpOutput.length, 0);
assert.equal(httpSectionOutput.length, 0);
CliRunner.createDefaultConfig = createDefaultConfig;
CliRunner.prototype.loadConfig = loadConfig;
});
});

0 comments on commit d3de801

Please sign in to comment.