From e5a81a69852beb77d2408dec8575ad83c53d8fae Mon Sep 17 00:00:00 2001 From: Andrei Rusu Date: Tue, 5 Dec 2017 13:13:42 +0100 Subject: [PATCH] #1604 - Implemented a fix to support latest selenium server versions --- examples/tests/google.js | 2 +- lib/api/protocol.js | 27 ++++++++++++++++++- lib/http/request.js | 4 +++ test/lib/mocks.json | 10 +++++++ test/src/api/commands/testClearValue.js | 6 ++--- .../api/commands/testWaitForElementPresent.js | 13 +++++++++ 6 files changed, 57 insertions(+), 5 deletions(-) diff --git a/examples/tests/google.js b/examples/tests/google.js index 39de4e6896..98cd6f4146 100644 --- a/examples/tests/google.js +++ b/examples/tests/google.js @@ -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'); diff --git a/lib/api/protocol.js b/lib/api/protocol.js index 7006940971..e4a11de941 100644 --- a/lib/api/protocol.js +++ b/lib/api/protocol.js @@ -3,6 +3,8 @@ var elementsByRecursion = require('./element-commands/_elementsByRecursion.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', @@ -1267,17 +1269,40 @@ 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) { if (arguments.length === 2 && typeof data === 'function') { callback = data; data = ''; } + var options = { path : '/session/' + Nightwatch.sessionId + path, method : 'POST', data : data || '' }; - return sendRequest(options, callback); + + return sendRequest(options, function(result) { + if (/\/element$/.test(options.path) && result.value) { + result.value = validateElementEntry(result.value); + } 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); + } + }); } function sendRequest(options, callback) { diff --git a/lib/http/request.js b/lib/http/request.js index 62f576074d..0fa49a546b 100644 --- a/lib/http/request.js +++ b/lib/http/request.js @@ -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; diff --git a/test/lib/mocks.json b/test/lib/mocks.json index 6811c42dc3..7cc963644e 100644 --- a/test/lib/mocks.json +++ b/test/lib/mocks.json @@ -41,6 +41,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/element", + "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/element", "postdata" : "{\"using\":\"css selector\",\"value\":\"#signupSection\"}", @@ -61,6 +66,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\"}", diff --git a/test/src/api/commands/testClearValue.js b/test/src/api/commands/testClearValue.js index 3f75ae29a5..b29d3227b7 100644 --- a/test/src/api/commands/testClearValue.js +++ b/test/src/api/commands/testClearValue.js @@ -8,17 +8,17 @@ module.exports = MochaTest.add('clearValue', { var client = Nightwatch.api(); MockServer.addMock({ - 'url': '/wd/hub/session/1352110219202/element/0/clear', + 'url': '/wd/hub/session/1352110219202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/clear', 'response': JSON.stringify({ sessionId: '1352110219202', status: 0 }) }); - client.clearValue('#weblogin', function callback(result) { + client.clearValue('#webdriver', function callback(result) { assert.equal(result.status, 0); }) - .clearValue('css selector', '#weblogin', function callback(result) { + .clearValue('css selector', '#webdriver', function callback(result) { assert.equal(result.status, 0); done(); }); diff --git a/test/src/api/commands/testWaitForElementPresent.js b/test/src/api/commands/testWaitForElementPresent.js index 33c9a90382..109db20a4e 100644 --- a/test/src/api/commands/testWaitForElementPresent.js +++ b/test/src/api/commands/testWaitForElementPresent.js @@ -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;