Repository: nightwatchjs/nightwatch
Pull request #2774 (2021-07-05 13:07:12)

status positive in case no elements are found
### Changes
- status=0 in case no elements are found  but the command executed successfully. 

gravityvi: In order to pass this test case: https://github.com/nightwatchjs/nightwatch/blob/6ae30d1340b915f3278461d7bd3969b129f1f864/test/src/element/testCommandsElementSelectors.js#L160. I guess this change is required as the status should be positive if the command executes successfully.

Patch:
diff --git a/lib/api/_loaders/_command-loader.js b/lib/api/_loaders/_command-loader.js
index 5b592ae6..3d40ab6a 100644
--- a/lib/api/_loaders/_command-loader.js
+++ b/lib/api/_loaders/_command-loader.js
@@ -1,7 +1,4 @@
-const path = require('path');
-const fs = require('fs');
 const Element = require('../../element');
-const Utils = require('../../utils');
 const BaseLoader = require('./_base-loader.js');
 
 class BaseCommandLoader extends BaseLoader {
diff --git a/lib/api/client-commands/getPageSource.js b/lib/api/client-commands/pageSource.js
similarity index 69%
rename from lib/api/client-commands/getPageSource.js
rename to lib/api/client-commands/pageSource.js
index aa55cffd..a241e8ab 100644
--- a/lib/api/client-commands/getPageSource.js
+++ b/lib/api/client-commands/pageSource.js
@@ -1,31 +1,31 @@
 const ClientCommand = require('./_base-command.js');
-
 /**
  * Returns the page source. Uses pageSource protocol command.
  *
  * @example
  *  this.demoTest = function (browser) {
- *    browser.getPageSource(function(pageSource) {
+ *    browser.pageSource(function(pageSource) {
  *      console.log(pageSource);
  *    });
  *  };
  *
  *
- * @method getPageSource
- * @syntax .getPageSource(callback)
+ * @method pageSource
+ * @syntax .pageSource(callback)
  * @param {function} callback Callback function which is called with the result value.
  * @see pageSource
  * @returns {string} The page source.
  * @api protocol.pageSource
  */
-class GetPageSource extends ClientCommand {
+
+class PageSource extends ClientCommand {
   get returnsFullResultObject() {
     return true;
   }
 
   performAction(callback) {
-    this.api.pageSource(callback);
+    this.api.source(callback);
   }
 }
 
-module.exports = GetPageSource;
+module.exports = PageSource;
diff --git a/lib/api/element-commands/_baseElementCommand.js b/lib/api/element-commands/_baseElementCommand.js
index 635f4c09..f7c2ccc3 100644
--- a/lib/api/element-commands/_baseElementCommand.js
+++ b/lib/api/element-commands/_baseElementCommand.js
@@ -128,7 +128,6 @@ class BaseElementCommand extends ElementCommand {
   handleElementError(err) {
     const shouldRegister = this.transport.shouldRegisterError(err);
     let originalErrorMessage = BaseElementCommand.getErrorMessage(err.response);
-
     err.message = `An error occurred while running .${this.commandName}() command on <${this.element.toString()}>: ${originalErrorMessage}`;
     err.detailedErr = JSON.stringify(err.response);
     err.stack = filterStackTrace(this.stackTrace);
diff --git a/lib/api/protocol/doubleClick.js b/lib/api/protocol/doubleClick.js
index 507261ef..f97be073 100644
--- a/lib/api/protocol/doubleClick.js
+++ b/lib/api/protocol/doubleClick.js
@@ -8,6 +8,7 @@ const ProtocolAction = require('./_base-action.js');
  * @api protocol.useractions
  */
 module.exports = class DoubleClick extends ProtocolAction {
+
   get w3c_deprecated() {
     return true;
   }
diff --git a/lib/api/protocol/elementIdEquals.js b/lib/api/protocol/elementIdEquals.js
index b6145fa0..73970bf6 100644
--- a/lib/api/protocol/elementIdEquals.js
+++ b/lib/api/protocol/elementIdEquals.js
@@ -23,6 +23,7 @@ const ProtocolAction = require('./_base-action.js');
  * @api protocol.elements
  */
 module.exports = class Session extends ProtocolAction {
+
   get w3c_deprecated() {
     return true;
   }
diff --git a/lib/api/protocol/mouseButtonClick.js b/lib/api/protocol/mouseButtonClick.js
index 5323ad30..8311df14 100644
--- a/lib/api/protocol/mouseButtonClick.js
+++ b/lib/api/protocol/mouseButtonClick.js
@@ -10,6 +10,7 @@ const ProtocolAction = require('./_base-action.js');
  * @api protocol.useractions
  */
 module.exports = class Command extends ProtocolAction {
+
   get w3c_deprecated() {
     return true;
   }
diff --git a/lib/transport/selenium-webdriver/actions.js b/lib/transport/selenium-webdriver/actions.js
index f8f1e8c3..4246f4d3 100644
--- a/lib/transport/selenium-webdriver/actions.js
+++ b/lib/transport/selenium-webdriver/actions.js
@@ -58,7 +58,7 @@ class TransportActions extends BaseActions {
 
           promise = this.transport.runProtocolAction(opts);
         }
-
+        
         result = await promise;
 
         if (Array.isArray(result) || !Utils.isObject(result)) {
diff --git a/lib/transport/selenium-webdriver/index.js b/lib/transport/selenium-webdriver/index.js
index 76026351..75fe4f4e 100644
--- a/lib/transport/selenium-webdriver/index.js
+++ b/lib/transport/selenium-webdriver/index.js
@@ -86,7 +86,6 @@ class SeleniumWebdriver extends WebdriverTransport {
   }
 
   set driver(value) {
-    console.log("setting driver to", value);
     this.__driver = value;
   }
 
@@ -105,11 +104,7 @@ class SeleniumWebdriver extends WebdriverTransport {
     if (browserName) {
       this.capabilities.setBrowserName(browserName);
     }
-
     this.elementKey = WebdriverTransport.WEB_ELEMENT_ID;
-    // Logging.setupVerboseLogging({
-    //   isSelenium
-    // });
   }
 
 
diff --git a/lib/transport/selenium-webdriver/method-mappings.js b/lib/transport/selenium-webdriver/method-mappings.js
index f01f19fd..64a5b94d 100644
--- a/lib/transport/selenium-webdriver/method-mappings.js
+++ b/lib/transport/selenium-webdriver/method-mappings.js
@@ -318,7 +318,7 @@ module.exports = class MethodMappings {
 
           if (Array.isArray(resultValue) && resultValue.length === 0) {
             return {
-              status: -1,
+              status: 0,
               value: [],
               error: `unable to locate element using ${locator.using}`
             };
@@ -521,14 +521,6 @@ module.exports = class MethodMappings {
           return this.methods.session.setElementValue.call(this, ...args);
         },
 
-        async pageSource() {
-          const value = await this.driver.getPageSource();
-
-          return {
-            value
-          };
-        },
-
         async setElementValue(webElement, value) {
           if (Array.isArray(value)) {
             value = value.join('');
@@ -587,6 +579,14 @@ module.exports = class MethodMappings {
         ///////////////////////////////////////////////////////////
         // Document Handling
         ///////////////////////////////////////////////////////////
+        async getPageSource() {
+          const value = await this.driver.getPageSource();
+
+          return {
+            value
+          };
+        },
+
 
         async executeScript(script, args) {
           const value = await this.driver.executeScript(script, args);
@@ -784,4 +784,4 @@ module.exports = class MethodMappings {
       }
     };
   }
-};
+};
\ No newline at end of file


Test patch:
diff --git a/lib/api/protocol/pageSource.js b/lib/api/protocol/source.js
similarity index 100%
rename from lib/api/protocol/pageSource.js
rename to lib/api/protocol/source.js
diff --git a/lib/testsuite/index.js b/lib/testsuite/index.js
index 0543ac20..dd51cc56 100644
--- a/lib/testsuite/index.js
+++ b/lib/testsuite/index.js
@@ -395,7 +395,7 @@ class TestSuite {
     }
     
     this.client.transport.testSuiteFinished(failures);
-    this.client.session.finished(failures ? 'FAILED' : '');
+    this.client.transport.sessionFinished(failures ? 'FAILED' : '');
     
     return this;
   }
diff --git a/test/src/api/commands/testGetPageSource.js b/test/src/api/commands/testPageSource.js
similarity index 82%
rename from test/src/api/commands/testGetPageSource.js
rename to test/src/api/commands/testPageSource.js
index 17fd8631..6700d2b7 100644
--- a/test/src/api/commands/testGetPageSource.js
+++ b/test/src/api/commands/testPageSource.js
@@ -2,7 +2,7 @@ const assert = require('assert');
 const MockServer  = require('../../../lib/mockserver.js');
 const CommandGlobals = require('../../../lib/globals/commands.js');
 
-describe('getPageSource', function() {
+describe('pageSource', function() {
   before(function(done) {
     CommandGlobals.beforeEach.call(this, done);
   });
@@ -10,7 +10,7 @@ describe('getPageSource', function() {
   after(function(done) {
     CommandGlobals.afterEach.call(this, done);
   });
-  it('client.getPageSource()', function(done) {
+  it('client.pageSource()', function(done) {
     MockServer.addMock({
       url: '/wd/hub/session/1352110219202/source',
       method: 'GET',
@@ -21,11 +21,11 @@ describe('getPageSource', function() {
       })
     });
 
-    this.client.api.getPageSource(function callback(result) {
+    this.client.api.source(function callback(result) {
       assert.strictEqual(result.value, '<html>Sample HTML</html>');
       assert.strictEqual(result.status, 0)
     });
 
     this.client.start(done);
   });
-});
+});
\ No newline at end of file
diff --git a/test/src/index/testCreateTransport.js b/test/src/index/testCreateTransport.js
index d7ab3f21..9af426dd 100644
--- a/test/src/index/testCreateTransport.js
+++ b/test/src/index/testCreateTransport.js
@@ -3,7 +3,6 @@ const nock = require('nock');
 const common = require('../../common.js');
 const NightwatchClient = common.require('index.js');
 const WebDriver = common.require('transport/webdriver');
-const JsonWire = common.require('transport/jsonwire');
 const Selenium = common.require('transport/selenium-webdriver/selenium.js');
 const Browserstack = common.require('transport/browserstack');
 
@@ -374,7 +373,6 @@ describe('Transport.create()', function () {
     assert.strictEqual(client.settings.webdriver.default_path_prefix, '/wd/hub');
   });
 
-  //TODO: Make the test pass after browserstack works with selenium-webdriver
   it('test create Transport for Browserstack', function(done) {
     assert.throws(function() {
       NightwatchClient.client({
@@ -433,7 +431,7 @@ describe('Transport.create()', function () {
     assert.strictEqual(client.settings.webdriver.ssl, true);
 
     const {transport} = client;
-    assert.strictEqual(transport instanceof WebDriver, false);
+    assert.ok(transport instanceof WebDriver);
     assert.strictEqual(transport.username, 'test-access-user');
     assert.strictEqual(transport.accessKey, 'test-access-key');
 
@@ -464,7 +462,6 @@ describe('Transport.create()', function () {
 
   });
 
-  //TODO: Make the test pass after browserstack works with selenium-webdriver
   it('test create Transport for Browserstack with failures', function(done) {
     const client = NightwatchClient.client({
       webdriver: {