Skip to content

Commit

Permalink
#1604 - slight refactoring and added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Dec 5, 2017
1 parent 01be031 commit 5b71d13
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/tests/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {

client.expect.element('#lst-ib').to.be.enabled;

client.expect.element('#hplogo').text.to.match(/Norge/).before(1000);
//client.expect.element('#hplogo').text.to.match(/Norge/).before(1000);

client.setValue('#lst-ib', 'Norway').pause(500);
client.expect.element('#lst-ib').to.have.value.equal('Norway');
Expand Down
28 changes: 18 additions & 10 deletions lib/api/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var Element = require('../page-object/element.js');

module.exports = function(Nightwatch) {

var WEBDRIVER_ELEMENT_ID = 'element-6066-11e4-a52e-4f735466cecf';

var MOUSE_BUTTON_LEFT = 'left',
MOUSE_BUTTON_MIDDLE = 'middle',
MOUSE_BUTTON_RIGHT = 'right',
Expand Down Expand Up @@ -1385,30 +1387,36 @@ module.exports = function(Nightwatch) {
return sendRequest(options, callback);
}

function validateElementEntry(result) {
if (!result.ELEMENT && result[WEBDRIVER_ELEMENT_ID]) {
result.ELEMENT = result[WEBDRIVER_ELEMENT_ID];
delete result[WEBDRIVER_ELEMENT_ID];
}

return result;
}

function postRequest(path, data, callback) {
var validateElementEntry = function (result) {
var ELEMENT_UID = 'element-6066-11e4-a52e-4f735466cecf';
if (!result.ELEMENT && result[ELEMENT_UID]) {
result.ELEMENT = result[ELEMENT_UID];
delete result[ELEMENT_UID];
}
return result;
};
if (arguments.length === 2 && typeof data === 'function') {
callback = data;
data = '';
}

var options = {
path : '/session/' + Nightwatch.sessionId + path,
method : 'POST',
data : data || ''
};

return sendRequest(options, function(result) {
if (/\/element$/.test(options.path) && result.value) {
result.value = validateElementEntry(result.value);
} else if (/\/elements$/.test(options.path) && result.value[0]) {
result.value[0] = validateElementEntry(result.value[0]);
} else if (/\/elements$/.test(options.path) && Array.isArray(result.value)) {
result.value = result.value.map(function(entry) {
return validateElementEntry(entry);
});
}

if (typeof callback === 'function') {
callback.call(this, result);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ module.exports = (function() {
delete result.value.stackTrace;
}

if (result.value.stacktrace) {
delete result.value.stacktrace;
}

if (needsFormattedErrorMessage(result)) {
errorMessage = formatErrorMessage(result.value);
delete result.value.localizedMessage;
Expand Down
5 changes: 5 additions & 0 deletions test/lib/mocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
"postdata" : "{\"using\":\"css selector\",\"value\":\"#weblogin\"}",
"response" : "{\"sessionId\":\"1352110219202\",\"status\":0,\"value\":[{\"ELEMENT\":\"0\"}],\"class\":\"org.openqa.selenium.remote.Response\",\"hCode\":604376696}"
},
{
"url" : "/wd/hub/session/1352110219202/elements",
"postdata" : "{\"using\":\"css selector\",\"value\":\"#webdriver\"}",
"response" : "{\"sessionId\":\"1352110219202\",\"status\":0,\"value\":[{\"element-6066-11e4-a52e-4f735466cecf\":\"5cc459b8-36a8-3042-8b4a-258883ea642b\"}]}"
},
{
"url" : "/wd/hub/session/1352110219202/elements",
"postdata" : "{\"using\":\"css selector\",\"value\":\"#weblogin-multiple\"}",
Expand Down
13 changes: 13 additions & 0 deletions test/src/api/commands/testWaitForElementPresent.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ module.exports = {
Nightwatch.start();
},

'client.waitForElementPresent() with webdriver response success': function (done) {
var api = Nightwatch.api();

api.waitForElementPresent('#webdriver', 100, function callback(result, instance) {
assert.equal(instance.expectedValue, 'found');
assert.equal(result.status, 0);
assert.equal(result.value[0].ELEMENT, '5cc459b8-36a8-3042-8b4a-258883ea642b');
done();
});

Nightwatch.start();
},

'client.waitForElementPresent() failure with custom message': function (done) {
var api = Nightwatch.api();
api.globals.waitForConditionPollInterval = 10;
Expand Down
19 changes: 19 additions & 0 deletions test/src/api/element-selectors/testProtocolElementSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ module.exports = MochaTest.add('test protocol element selectors', {
Nightwatch.start();
},

'protocol.element(using, {selector, locateStrategy}) webdriver response' : function(done) {
nocks.elementFound('#nock-webdriver', 'css selector', {
'element-6066-11e4-a52e-4f735466cecf': '5cc459b8-36a8-3042-8b4a-258883ea642b'
});

Nightwatch.api()
.element('css selector', {selector: '#nock-webdriver', locateStrategy: 'css selector'}, function callback(result) {
assert.equal(result.value.ELEMENT, '5cc459b8-36a8-3042-8b4a-258883ea642b', 'Found element, same locateStrategy');
})
.element('css selector', {selector: '#nock-webdriver', locateStrategy: null}, function callback(result) {
assert.equal(result.value.ELEMENT, '5cc459b8-36a8-3042-8b4a-258883ea642b', 'Found element, null locateStrategy');
})
.perform(function() {
done();
});

Nightwatch.start();
},

'protocol.element(using, {locateStrategy})' : function(done) {
utils.catchQueueError(function (err) {
var msg = 'No selector property for';
Expand Down

0 comments on commit 5b71d13

Please sign in to comment.