Repository: nightwatchjs/nightwatch
Pull request #2709 (2021-05-21 09:40:55)

added isEnabled command
Thanks in advance for your contribution. Please follow the below steps in submitting a pull request, as it will help us with reviewing it quicker.

- [ ] Create a new branch from master (e.g. `features/my-new-feature` or `issue/123-my-bugfix`);
- [ ] If you're fixing a bug also create an issue if one doesn't exist yet;
- [ ] If it's a new feature explain why do you think it's necessary. Please check with the maintainers beforehand to make sure it is something that we will accept. Usually we only accept new features if we feel that they will benefit the entire community;
- [ ] Please avoid sending PRs which contain drastic or low level changes. If you are certain that the changes are needed, please discuss them beforehand and indicate what the impact will be;
- [ ] If your change is based on existing functionality please consider refactoring first. Pull requests that duplicate code will most likely be ignored;
- [ ] Do not include changes that are not related to the issue at hand;
- [ ] Follow the same coding style with regards to spaces, semicolons, variable naming etc.;
- [ ] Always add unit tests - PRs without tests are most of the times ignored.



Patch:
diff --git a/lib/api/assertions/_enabled.js b/lib/api/assertions/enabled.js
similarity index 93%
rename from lib/api/assertions/_enabled.js
rename to lib/api/assertions/enabled.js
index 2e87b606..a5881745 100644
--- a/lib/api/assertions/_enabled.js
+++ b/lib/api/assertions/enabled.js
@@ -34,7 +34,7 @@ exports.assertion = function(definition, msg) {
   };
 
   this.evaluate = function(value) {
-    return value === false;
+    return value === true;
   };
 
   this.actual = function(passed) {
@@ -42,7 +42,7 @@ exports.assertion = function(definition, msg) {
   };
 
   this.command = function(callback) {
-    this.api.getElementProperty(definition, 'disabled', callback);
+    this.api.isEnabled(definition, callback);
   };
   
 };
diff --git a/lib/api/element-commands/isEnabled.js b/lib/api/element-commands/isEnabled.js
new file mode 100644
index 00000000..e270d852
--- /dev/null
+++ b/lib/api/element-commands/isEnabled.js
@@ -0,0 +1,57 @@
+const BaseElementCommand = require('./_baseElementCommand.js');
+
+/**
+ * Determines if an element is enabled, as indicated by the 'disabled' attribute.
+ *
+ * @example
+ * module.exports = {
+ *   demoTest(browser) {
+ *     browser.isEnabled('#main select option.first', function(result) {
+ *       this.assert.equal(typeof result, "object");
+ *       this.assert.equal(result.status, 0);
+ *       this.assert.equal(result.value, true);
+ *     });
+ *
+ *     // with explicit locate strategy
+ *     browser.isEnabled('css selector', '#main select option.first');
+ *
+ *     // with selector object - see https://nightwatchjs.org/guide#element-properties
+ *     browser.isEnabled({
+ *       selector: '#main ul li a',
+ *       index: 1,
+ *       suppressNotFoundErrors: true
+ *     });
+ *
+ *     browser.isEnabled({
+ *       selector: '#main select option.first',
+ *       timeout: 2000 // overwrite the default timeout (in ms) to check if the element is present
+ *     });
+ *   },
+ *
+ *   demoTestAsync: async function(browser) {
+ *     const result = await browser.isEnabled('#main select option.first');
+ *     console.log('isVisible result', result);
+ *   }
+ * }
+ *
+ * @method isEnabled
+ * @syntax .isEnabled(selector, callback)
+ * @syntax .isEnabled(using, selector, callback)
+ * @param {string} [using] The locator strategy to use. See [W3C Webdriver - locator strategies](https://www.w3.org/TR/webdriver/#locator-strategies)
+ * @param {string|object} selector The selector (CSS/Xpath) used to locate the element. Can either be a string or an object which specifies [element properties](https://nightwatchjs.org/guide#element-properties).
+ * @param {function} callback Callback function which is called with the result value.
+ * @link /#is-element-enabled
+ * @api protocol.elementstate
+ */
+class IsEnabled extends BaseElementCommand {
+  get extraArgsCount() {
+    return 0;
+  }
+
+  get elementProtocolAction() {
+    return 'isElementEnabled';
+  }
+
+}
+
+module.exports = IsEnabled;
\ No newline at end of file


Test patch:
diff --git a/test/src/api/assertions/testEnabled.js b/test/src/api/assertions/testEnabled.js
index 7759357e..7bb303d2 100644
--- a/test/src/api/assertions/testEnabled.js
+++ b/test/src/api/assertions/testEnabled.js
@@ -1,9 +1,9 @@
 const assert = require('assert');
 const Globals = require('../../../lib/globals.js');
 
-xdescribe('assert.enabled', function () {
+describe('assert.enabled', function () {
   const assertionName = 'enabled';
-  const api = 'getElementProperty';
+  const api = 'isEnabled';
 
   function assertionTest(opts) {
     return Globals.assertion(assertionName, api, opts);
@@ -11,10 +11,10 @@ xdescribe('assert.enabled', function () {
 
   it('enabled assertion passed', function () {
     return assertionTest({
-      args: ['.test_element'],
+      args: ['.test_element', 'Test message'],
       commandResult: {
         status: 0,
-        value: false
+        value: true
       },
       assertArgs: true,
       assertMessage: true,
@@ -32,7 +32,7 @@ xdescribe('assert.enabled', function () {
       args: ['.test_element'],
       commandResult: {
         status: 0,
-        value: true
+        value: false
       },
       negate: true,
 
@@ -40,7 +40,7 @@ xdescribe('assert.enabled', function () {
         assert.strictEqual(typeof err, 'undefined');
         assert.strictEqual(queueOpts.negate, true);
         assert.strictEqual(instance.hasFailure(), false);
-        assert.strictEqual(instance.getValue(), true);
+        assert.strictEqual(instance.getValue(), false);
         assert.strictEqual(instance.getActual(), 'not enabled');
         assert.strictEqual(
           instance.message,
@@ -61,7 +61,7 @@ xdescribe('assert.enabled', function () {
       args: ['.test_element'],
       commandResult: {
         status: 0,
-        value: false
+        value: true
       },
       negate: true,
       assertError: true,
@@ -69,7 +69,7 @@ xdescribe('assert.enabled', function () {
       assertion({reporter, instance, queueOpts, err}) {
         assert.strictEqual(queueOpts.negate, true);
         assert.strictEqual(instance.hasFailure(), false);
-        assert.strictEqual(instance.getValue(), false);
+        assert.strictEqual(instance.getValue(), true);
         assert.strictEqual(instance.getActual(), 'enabled');
         assert.strictEqual(
           err.message,
@@ -84,7 +84,7 @@ xdescribe('assert.enabled', function () {
       args: [{selector: '.test_element'}],
       commandResult: {
         status: 0,
-        value: false
+        value: true
       },
       assertion({instance, failure, message, err}) {
         assert.strictEqual(err, undefined);
@@ -105,7 +105,7 @@ xdescribe('assert.enabled', function () {
       args: [{selector: '.test_element', timeout: 10, retryInterval: 15}],
       commandResult: {
         status: 0,
-        value: false
+        value: true
       },
       assertion({instance, failure, message, err}) {
         assert.strictEqual(err, undefined);
@@ -128,7 +128,7 @@ xdescribe('assert.enabled', function () {
       args: ['.test_element'],
       commandResult: {
         status: 0,
-        value: true
+        value: false
       },
       assertError: true,
       assertResult: true,
diff --git a/test/src/api/commands/testIsEnabled.js b/test/src/api/commands/testIsEnabled.js
new file mode 100644
index 00000000..8a4dd185
--- /dev/null
+++ b/test/src/api/commands/testIsEnabled.js
@@ -0,0 +1,40 @@
+const assert = require('assert');
+const MockServer  = require('../../../lib/mockserver.js');
+const CommandGlobals = require('../../../lib/globals/commands.js');
+
+describe('isEnabled', function() {
+  before(function(done) {
+    CommandGlobals.beforeEach.call(this, done);
+  });
+
+  afterEach(function() {
+    MockServer.removeMock({
+      url: '/wd/hub/session/1352110219202/element/0/enabled',
+      method: 'GET'
+    });
+  });
+
+  after(function(done) {
+    CommandGlobals.afterEach.call(this, done);
+  });
+
+  it('client.isEnabled()', function(done) {
+    MockServer.addMock({
+      url: '/wd/hub/session/1352110219202/element/0/enabled',
+      method: 'GET',
+      response: JSON.stringify({
+        sessionId: '1352110219202',
+        status: 0,
+        value: true
+      })
+    });
+
+    this.client.api.isEnabled('css selector', '#weblogin', function callback(result) {
+      assert.strictEqual(result.value, true);
+    }).isEnabled('#weblogin', function callback(result) {
+      assert.strictEqual(result.value, true);
+    });
+
+    this.client.start(done);
+  });
+});