Skip to content

Commit

Permalink
Added support for element commands (e.g. getText) to be used with ES6…
Browse files Browse the repository at this point in the history
… await
  • Loading branch information
beatfactor committed May 23, 2019
1 parent 18c3e04 commit 0f01683
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 44 deletions.
4 changes: 2 additions & 2 deletions lib/api-loader/element-command.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const BaseCommandLoader = require('./_base-loader.js');
const CommandLoader = require('./command.js');
const ElementCommand = require('../element/command.js');

class ElementCommandLoader extends BaseCommandLoader {
class ElementCommandLoader extends CommandLoader {
static createInstance(CommandModule, opts) {
let ClassName = CommandModule || ElementCommand;

Expand Down
2 changes: 1 addition & 1 deletion lib/api/assertions/elementNotPresent.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exports.assertion = function(selector, msg) {
};

this.value = function(result) {
return result.value.length === 0 ? 'not present' : 'present';
return result.value && result.value.length > 0 ? 'present' : 'not present';
};

this.command = function(callback) {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/assertions/elementPresent.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports.assertion = function(selector, msg) {
};

this.value = function(result) {
return result.value.length === 0 ? 'not present' : 'present';
return result.value && result.value.length > 0 ? 'present' : 'not present';
};

this.command = function(callback) {
Expand Down
39 changes: 34 additions & 5 deletions lib/api/element-commands/click.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
const BaseElementCommand = require('./_baseElementCommand.js');

/**
* Simulates a click event on the given DOM element. Uses `elementIdClick` protocol action internally.
* Simulates a click event on the given DOM element.
*
* The element is scrolled into view if it is not already pointer-interactable. See the WebDriver specification for <a href="https://www.w3.org/TR/webdriver/#element-interactability" target="_blank">element interactability</a>
* The element is scrolled into view if it is not already pointer-interactable. See the WebDriver specification for <a href="https://www.w3.org/TR/webdriver/#element-interactability" target="_blank">element interactability</a>. Starting with v1.1 `click()` will also wait for the element to be present (until the specified timeout).
*
* Uses `elementIdClick` protocol action internally.
*
* @example
* this.demoTest = function (browser) {
* browser.click("#main ul li a.first");
* };
* module.exports = {
* demoTest(browser) {
* browser.click('#main ul li a.first');
*
* browser.click('#main ul li a.first', function(result) {
* console.log('Click result', result);
* });
*
* // with explicit locate strategy
* browser.click('css selector', '#main ul li a.first');
*
* // with selector object - see https://nightwatchjs.org/guide#element-properties
* browser.click({
* selector: '#main ul li a',
* index: 1
* });
*
* browser.click({
* selector: '#main ul li a.first',
* timeout: 2000 // overwrite the default timeout (in ms) to check if the element is present
* });
* },
*
* demoTestAsync: async function(browser) {
* const result = await browser.click('#main ul li a.first');
* console.log('Click result', result);
* }
* }
*
*
* @method click
* @syntax .click(selector, [callback])
* @syntax .click(using, selector, [callback])
* @param {string} [using] The locator strategy to use. See [W3C Webdriver - locator strategies](https://www.w3.org/TR/webdriver/#locator-strategies)
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see elementIdClick
Expand Down
46 changes: 38 additions & 8 deletions lib/api/element-commands/getText.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
const BaseElementCommand = require('./_baseElementCommand.js');

/**
* Returns the visible text for the element. Uses `elementIdText` protocol command.
* Returns the visible text for the element. Starting with v1.1 `getText()` will also wait for the element to be present (until the specified timeout).
*
* Uses `elementIdText` protocol command.
*
* @example
* this.demoTest = function (browser) {
* browser.getText("#main ul li a.first", function(result) {
* this.assert.equal(typeof result, "object");
* this.assert.equal(result.status, 0);
* this.assert.equal(result.value, "nightwatchjs.org");
* });
* };
* module.exports = {
* demoTest(browser) {
* browser.getText('#main ul li a.first', function(result) {
* this.assert.equal(typeof result, 'object);
* this.assert.strictEqual(result.status, 0); // only when using Selenium / JSONWire
* this.assert.equal(result.value, 'nightwatchjs.org');
* });
*
* // with explicit locate strategy
* browser.getText('css selector', '#main ul li a.first', function(result) {
* console.log('getText result', result.value);
* });
*
* // with selector object - see https://nightwatchjs.org/guide#element-properties
* browser.getText({
* selector: '#main ul li a',
* index: 1
* }, function(result) {
* console.log('getText result', result.value);
* });
*
* browser.getText({
* selector: '#main ul li a.first',
* timeout: 2000 // overwrite the default timeout (in ms) to check if the element is present
* }, function(result) {
* console.log('getText result', result.value);
* });
* },
*
* demoTestAsync: async function(browser) {
* const result = await browser.getText('#main ul li a.first');
* console.log('getText result', result);
* }
* }
*
* @method getText
* @syntax .getText(selector, callback)
* @syntax .getText(using, selector, callback)
* @param {string} [using] The locator strategy to use. See [W3C Webdriver - locator strategies](https://www.w3.org/TR/webdriver/#locator-strategies)
* @param {string} selector The CSS/Xpath selector used to locate the element.
* @param {function} callback Callback function which is called with the result value.
* @see elementIdText
Expand Down
2 changes: 1 addition & 1 deletion lib/element/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class ElementCommand extends EventEmitter {
if (Utils.isString(this.args[0]) && (expectedArgs === this.args.length || ElementCommand.isSelectorObject(this.args[1]))) {
const strategy = this.args.shift();
LocateStrategy.validate(strategy, this.commandName);
this.setStrategy();
this.setStrategy(strategy);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/sampletests/es6await/webdriver/getText/getTextES6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const assert = require('assert');

module.exports = {
'getText() test with ES6 async/await': async (client) => {
client.url('http://localhost');

const textResult = await client.getText('#element-selector');
assert.ok(!!textResult);
assert.ok('value' in textResult);
assert.strictEqual(textResult.value, 'sample text value');
}
};
5 changes: 0 additions & 5 deletions test/src/api/commands/testIsLogAvailable.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
const assert = require('assert');
const MockServer = require('../../../lib/mockserver.js');
const CommandGlobals = require('../../../lib/globals/commands.js');
const common = require('../../../common.js');
const Logger = common.require('util/logger.js');

describe('isLogAvailable', function() {
//Logger.enable();
//Logger.setOutputEnabled(true);

before(function(done) {
CommandGlobals.beforeEach.call(this, done);
});
Expand Down
2 changes: 0 additions & 2 deletions test/src/api/commands/testSetValue.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const assert = require('assert');
const MockServer = require('../../../lib/mockserver.js');
const common = require('../../../common.js');
const CommandGlobals = require('../../../lib/globals/commands.js');
const Logger = common.require('util/logger.js');

describe('setValue', function() {

Expand Down
5 changes: 0 additions & 5 deletions test/src/element/testProtocolElementSelectors.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
const path = require('path');
const assert = require('assert');
const common = require('../../common.js');
const nocks = require('../../lib/nockselements.js');
const Nightwatch = require('../../lib/nightwatch.js');
const Logger = common.require('util/logger.js');

describe('test protocol element selectors', function() {
before(function(done) {
nocks.enable();
//Logger.enable();
//Logger.setOutputEnabled(false);

Nightwatch.startMockServer(done);
});

Expand Down
2 changes: 1 addition & 1 deletion test/src/index/testNightwatchIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('test NightwatchIndex', function () {
browserName: 'chrome'
},
silent: false,
output: true
output: false
});

client.startSession().catch(err => {
Expand Down
6 changes: 3 additions & 3 deletions test/src/runner/cli/testParallelExecution.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('test Parallel Execution', function() {
assert.ok(runner.test_settings.test_workers);

return runner.runTests().then(_ => {
assert.strictEqual(allArgs.length, 30);
assert.strictEqual(allArgs.length, 31);
assert.strictEqual(runner.concurrency.globalExitCode, 0);
});
});
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('test Parallel Execution', function() {
});

return runner.runTests().then(_ => {
assert.strictEqual(allArgs.length, 30);
assert.strictEqual(allArgs.length, 31);
});
});

Expand Down Expand Up @@ -163,7 +163,7 @@ describe('test Parallel Execution', function() {
});

return runner.runTests().then(_ => {
assert.strictEqual(allArgs.length, 30);
assert.strictEqual(allArgs.length, 31);
});
});

Expand Down
8 changes: 2 additions & 6 deletions test/src/runner/testRunWithCustomCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ const common = require('../../common.js');
const CommandGlobals = require('../../lib/globals/commands.js');
const MockServer = require('../../lib/mockserver.js');
const NightwatchClient = common.require('index.js');
const Logger = common.require('util/logger.js');

describe('testRunWithCustomCommands', function() {

//Logger.enable();
//Logger.setOutputEnabled(true);

before(function(done) {
this.server = MockServer.init();

Expand Down Expand Up @@ -88,7 +84,7 @@ describe('testRunWithCustomCommands', function() {
version2: true,
start_process: true
},
output: true,
output: false,
silent: false,
custom_commands_path: [path.join(__dirname, '../../extra/commands/es6async')],
persist_globals: true,
Expand Down Expand Up @@ -132,7 +128,7 @@ describe('testRunWithCustomCommands', function() {
version2: true,
start_process: true
},
output: true,
output: false,
silent: false,
custom_commands_path: [path.join(__dirname, '../../extra/commands')],
persist_globals: true,
Expand Down
2 changes: 1 addition & 1 deletion test/src/runner/testRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('testRunner', function() {
reporter: function () {
}
},
output: true,
output: false,
screenshots: {
enabled: true,
on_failure: true,
Expand Down
84 changes: 81 additions & 3 deletions test/src/runner/testRunnerEs6Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('testRunner ES6 Async', function() {
});

it('test Runner with ES6 async/await tests basic sample', function() {
let testsPath = path.join(__dirname, '../../sampletests/es6await');
let testsPath = path.join(__dirname, '../../sampletests/es6await/selenium');
MockServer.addMock({
url: '/wd/hub/session/1352110219202/cookie',
method: 'GET',
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('testRunner ES6 Async', function() {
});

it('test Runner with ES6 async/await tests getLog example', function() {
const testsPath = path.join(__dirname, '../../sampletests/es6await/getlog');
const testsPath = path.join(__dirname, '../../sampletests/es6await/selenium/getlog');
const globals = {
waitForConditionPollInterval: 50,

Expand All @@ -106,8 +106,86 @@ describe('testRunner ES6 Async', function() {
});
});

it('test Runner with ES6 async/await tests getText example', function() {
MockServer.addMock({
url: '/session',
statusCode: 201,
response: JSON.stringify({
value: {
sessionId: '13521-10219-202',
capabilities: {
acceptInsecureCerts: false,
browserName: 'firefox',
browserVersion: '65.0.1'
}
}
})
}, true);

MockServer.addMock({
url: '/session/13521-10219-202/url',
statusCode: 200,
response: JSON.stringify({
value: null
})
}, true);

MockServer.addMock({
url: '/session/13521-10219-202/elements',
postdata: {
using: 'css selector',
value: '#element-selector'
},

method: 'POST',
response: JSON.stringify({
sessionId: '1352110219202',
status: 0,
value: [{
'element-6066-11e4-a52e-4f735466cecf': '5cc459b8-36a8-3042-8b4a-258883ea642b'
}]
})
}, true);

MockServer.addMock({
url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/text',
method: 'GET',
response: JSON.stringify({
value: 'sample text value'
})
}, true);

const testsPath = path.join(__dirname, '../../sampletests/es6await/webdriver/getText');
const globals = {
waitForConditionPollInterval: 50,

reporter(results) {
if (results.lastError) {
throw results.lastError;
}
}
};

return NightwatchClient.runTests(testsPath, {
selenium: {
port: 10195,
version2: false,
start_process: false
},
webdriver: {
start_process: true
},
output: false,
skip_testcases_on_fail: false,
silent: false,
persist_globals: true,
globals: globals,
output_folder: false
});
});

it('test Runner with ES6 async commands', function() {
const testsPath = path.join(__dirname, '../../sampletests/es6await/getlog');
const testsPath = path.join(__dirname, '../../sampletests/es6await/selenium/getlog');
const globals = {
waitForConditionPollInterval: 50,

Expand Down

0 comments on commit 0f01683

Please sign in to comment.