Repository: nightwatchjs/nightwatch
Pull request #3087 (2022-03-03 13:00:55)

Fixes/3079 unable to clear in set value command
### Changes
- updated `setElemnetValue` to clear element value when editable
- updated tests of `setValue`


Patch:
diff --git a/lib/transport/selenium-webdriver/method-mappings.js b/lib/transport/selenium-webdriver/method-mappings.js
index 89a33fef..e6fe0831 100644
--- a/lib/transport/selenium-webdriver/method-mappings.js
+++ b/lib/transport/selenium-webdriver/method-mappings.js
@@ -572,7 +572,13 @@ module.exports = class MethodMappings {
           const element = this.getWebElement(webElementOrId);
 
           // clear Element value
-          await element.clear();
+          try {
+            await element.clear();
+          } catch (err) {
+            if (err.name !== 'InvalidElementStateError') {
+              throw err;
+            }
+          }
           await element.sendKeys(value);
 
           return null;


Test patch:
diff --git a/test/lib/globals/commands-w3c.js b/test/lib/globals/commands-w3c.js
index 4ebf4bee..b3092725 100644
--- a/test/lib/globals/commands-w3c.js
+++ b/test/lib/globals/commands-w3c.js
@@ -16,8 +16,7 @@ module.exports = {
           port: 10195,
           host: 'localhost'
         },
-        // output: process.env.VERBOSE === '1' || false,
-        output: false,
+        output: process.env.VERBOSE === '1' || false,
         silent: false
       })
         .then(client => {
diff --git a/test/src/api/commands/element/testSetValue.js b/test/src/api/commands/element/testSetValue.js
index abe8d1e6..a640dc98 100644
--- a/test/src/api/commands/element/testSetValue.js
+++ b/test/src/api/commands/element/testSetValue.js
@@ -1,25 +1,35 @@
 const assert = require('assert');
 const MockServer  = require('../../../../lib/mockserver.js');
-const CommandGlobals = require('../../../../lib/globals/commands.js');
+const CommandGlobals = require('../../../../lib/globals/commands-w3c.js');
 
 describe('setValue', function() {
 
-  before(function(done) {
+  beforeEach(function(done) {
     CommandGlobals.beforeEach.call(this, done);
   });
 
-  after(function(done) {
+  afterEach(function(done) {
     CommandGlobals.afterEach.call(this, done);
   });
 
   it('client.setValue()', function(done) {
+    
+     
     MockServer.addMock({
-      url: '/wd/hub/session/1352110219202/element/0/value',
+      url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/clear',
       method: 'POST',
-      postdata: {value: ['p', 'a', 's', 's', 'w', 'o', 'r', 'd']},
+      statusCode: 200,
       response: {
-        sessionId: '1352110219202',
-        status: 0
+        value: null
+      }
+    });
+
+    MockServer.addMock({
+      url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/value',
+      method: 'POST',
+      postdata: {text: 'password', value: ['p', 'a', 's', 's', 'w', 'o', 'r', 'd']},
+      response: {
+        value: null
       }
     });
 
@@ -45,4 +55,37 @@ describe('setValue', function() {
 
     this.client.start(done);
   });
+
+  it('client.setValue - non editable element', function(done) {
+    
+    MockServer.addMock({
+      url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/clear',
+      method: 'POST',
+      statusCode: 400,
+      response: {
+        value: {
+          error: 'invalid element state',
+          message: 'Unable to clear element that cannot be edited',
+          stacktrace: ''
+        }
+      }
+    });
+
+    MockServer.addMock({
+      url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/value',
+      method: 'POST',
+      postdata: {text: 'password', value: ['p', 'a', 's', 's', 'w', 'o', 'r', 'd']},
+      response: {
+        sessionId: '1352110219202',
+        status: 0
+      }
+    });
+
+    this.client.api.setValue('#weblogin', 'password', function callback(result) {
+      assert.strictEqual(result.status, 0);
+    });
+
+    this.client.start(done);
+    
+  });
 });