Repository: nightwatchjs/nightwatch
Pull request #3116 (2022-03-24 14:22:51)

Features/register basic auth
### Changes
- added command `registerBasicAuth`


Patch:
diff --git a/lib/api/_loaders/command.js b/lib/api/_loaders/command.js
index 22a22d99..8aa6773b 100644
--- a/lib/api/_loaders/command.js
+++ b/lib/api/_loaders/command.js
@@ -72,14 +72,7 @@ class CommandLoader extends BaseCommandLoader {
 
     class CommandInstance extends CommandClass {
       get api() {
-        const isES6Async = this.isES6AsyncCommand;
-
-        return {
-          ...nightwatchInstance.api,
-          get isES6Async() {
-            return isES6Async;
-          }
-        };
+        return nightwatchInstance.api;
       }
 
       get isES6AsyncCommand() {
diff --git a/lib/api/client-commands/registerBasicAuth.js b/lib/api/client-commands/registerBasicAuth.js
new file mode 100644
index 00000000..4b9d5ec5
--- /dev/null
+++ b/lib/api/client-commands/registerBasicAuth.js
@@ -0,0 +1,50 @@
+const ClientCommand = require('./_base-command.js');
+const {Logger} = require('../../utils');
+
+/**
+ * Automate the input of basic auth credentials whenever they arise.
+ *
+ * @example
+ *  this.demoTest = function (browser) {
+ *    browser
+ *      .registerBasicAuth('test-username', 'test-password')
+ *      .navigateTo('http://browserspy.dk/password-ok.php');
+ *  };
+ *
+ * @method registerBasicAuth
+ * @syntax .registerBasicAuth(username, password, [callback])
+ * @param {string} username
+ * @param {string} password
+ * @param {function} [callback] Optional callback function to be called when the command finishes.
+ * @api protocol.userprompts
+ */
+class RegisterBasicAuth extends ClientCommand {
+
+  performAction(callback) {
+
+    if (!this.api.isChrome()  && !this.api.isEdge()) {
+      const error =  new Error('RegisterBasicAuth is not supported while using this driver');
+      Logger.error(error);
+
+      return callback(error);
+    }
+
+    const {username, password} = this;
+    
+    this.transportActions
+      .registerAuth(username, password, callback)
+      .catch(err => {
+        return err;
+      })
+      .then(result => callback(result));
+  }
+
+  command(username, password, callback) {
+    this.username = username;
+    this.password = password;
+
+    return super.command(callback);
+  }
+}
+
+module.exports = RegisterBasicAuth;
diff --git a/lib/transport/selenium-webdriver/method-mappings.js b/lib/transport/selenium-webdriver/method-mappings.js
index e6fe0831..1d42e81f 100644
--- a/lib/transport/selenium-webdriver/method-mappings.js
+++ b/lib/transport/selenium-webdriver/method-mappings.js
@@ -915,10 +915,24 @@ module.exports = class MethodMappings {
         async wait(conditionFn, timeMs, message, retryInterval) {
           await this.driver.wait(conditionFn(), timeMs, message, retryInterval);
 
+          return {
+            value: null
+          };
+        },
+
+        ///////////////////////////////////////////////////////////////////////////
+        // BiDi apis
+        ///////////////////////////////////////////////////////////////////////////
+
+        async registerAuth(username, password) {
+          const pageCdpConnection = await this.driver.createCDPConnection('page');
+          await this.driver.register(username, password, pageCdpConnection);
+
           return {
             value: null
           };
         }
+        
       }
     };
   }


Test patch:
diff --git a/test/src/api/commands/client/testRegisterBasicAuth.js b/test/src/api/commands/client/testRegisterBasicAuth.js
new file mode 100644
index 00000000..1224a704
--- /dev/null
+++ b/test/src/api/commands/client/testRegisterBasicAuth.js
@@ -0,0 +1,71 @@
+const assert = require('assert');
+const CommandGlobals = require('../../../../lib/globals/commands.js');
+const MockServer = require('../../../../lib/mockserver.js');
+const Nightwatch = require('../../../../lib/nightwatch.js');
+
+describe('.registerBasicAuth()', function () {
+  beforeEach(function (done) {
+    CommandGlobals.beforeEach.call(this, done);
+  });
+
+  afterEach(function (done) {
+    CommandGlobals.afterEach.call(this, done);
+  });
+
+  it('browser.registerBasicAuth()', function (done) {
+
+    MockServer.addMock({
+      url: '/session',
+      response: {
+        value: {
+          sessionId: '13521-10219-202',
+          capabilities: {
+            browserName: 'chrome',
+            browserVersion: '92.0'
+          }
+        }
+      },
+      method: 'POST',
+      statusCode: 201
+    }, true);
+
+    Nightwatch.initW3CClient({
+      desiredCapabilities: {
+        browserName: 'chrome',
+        'goog:chromeOptions': {}
+      }
+    }).then(client=>{
+
+      let expectedUsername;
+      let expectedPassword;
+
+      client.transport.driver.createCDPConnection =  function() {
+        return  Promise.resolve();
+      };
+      client.transport.driver.register =  (username, password) =>{
+        expectedUsername = username;
+        expectedPassword = password;
+      };
+      client.api.registerBasicAuth('nightwatch', 'BarnOwl', function (){
+        assert.strictEqual(expectedUsername, 'nightwatch');
+        assert.strictEqual(expectedPassword, 'BarnOwl');
+      });
+      client.start(done);
+    });
+  });
+
+  it('browser.registerBasicAuth - driver not supported', function(done){
+    Nightwatch.initW3CClient({
+      desiredCapabilities: {
+        browserName: 'firefox'
+      }
+    }).then(client => {
+      client.api.registerBasicAuth('admin', 'admin', function(result){
+        assert.strictEqual(result.status, -1);
+        assert.strictEqual(result.error, 'RegisterBasicAuth is not supported while using this driver');
+      });
+      client.start(done);
+    });
+  });
+
+});