Repository: nightwatchjs/nightwatch
Pull request #2739 (2021-06-14 13:28:01)

update: method-mapping for elements, and frames
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.


gravityvi: @vaibhavsingh97, can we also include tests for the commands refactored as well?

Patch:
diff --git a/lib/transport/selenium-webdriver/method-mappings.js b/lib/transport/selenium-webdriver/method-mappings.js
index 845fbc79..b4acce6b 100644
--- a/lib/transport/selenium-webdriver/method-mappings.js
+++ b/lib/transport/selenium-webdriver/method-mappings.js
@@ -254,21 +254,26 @@ module.exports = class MethodMappings {
         ///////////////////////////////////////////////////////////
         // Frames
         ///////////////////////////////////////////////////////////
-        switchToFrame(frameId) {
+        async switchToFrame(frameId) {
           if (frameId === undefined) {
             frameId = null;
           }
 
-          return TransportActions.post({
-            path: '/frame',
-            data: {
-              id: frameId
-            }
+          await this.driver.manage().switchToFrame({
+            id: frameId
           });
+
+          return {
+            value: null
+          };
         },
 
-        switchToParentFrame() {
-          return TransportActions.post('/frame/parent');
+        async switchToParentFrame() {
+          await this.driver.manage().switchToParentFrame();
+
+          return {
+            value: null
+          };
         },
 
         ///////////////////////////////////////////////////////////
@@ -305,60 +310,114 @@ module.exports = class MethodMappings {
           return `/element/${id}/equals/${otherId}`;
         },
 
-        locateSingleElementByElementId({id, using, value}) {
-          return TransportActions.post({
-            path: `/element/${id}/element`,
-            data: {
-              using,
-              value
-            }
-          });
+        async locateSingleElementByElementId({id, value}) {
+          const locator = SeleniumWebdriver.createLocator(value);
+          const element = await this.getWebElement(id);
+
+          const webElement = await element.findElement(locator);
+
+          const elementId = await webElement.getId();
+
+          return {
+            value: webElement,
+            elementId
+          };
         },
 
-        locateMultipleElementsByElementId({id, using, value}) {
-          return TransportActions.post({
-            path: `/element/${id}/elements`,
-            data: {
-              using,
-              value
-            }
-          });
+        async locateMultipleElementsByElementId({id, using, value}) {
+          const locator = SeleniumWebdriver.createLocator(value);
+          const element = await this.getWebElement(id);
+          const resultValue = await element.findElements(locator);
+
+          const resultProcessed = await Promise.all(resultValue.map(async webElement => {
+            const elementId = await webElement.getId();
+
+            return {[Transport.WEB_ELEMENT_ID]: elementId};
+          }));
+
+          return {
+            value: resultProcessed
+          };
         },
 
         getActiveElement() {
           return TransportActions.post('/element/active');
         },
 
-        getElementAttribute(id, attributeName) {
-          return `/element/${id}/attribute/${attributeName}`;
+        async getElementAttribute(webElement, attributeName) {
+          const element = this.getWebElement(webElement);
+          const elementValue = await element.getAttribute(attributeName);
+
+          return {
+            value: elementValue
+          };
         },
 
-        getElementCSSValue(id, cssPropertyName) {
-          return `/element/${id}/css/${cssPropertyName}`;
+        async getElementCSSValue(id, cssPropertyName) {
+          const element = this.getWebElement(id);
+          const elementCssValue = await element.getCssValue(cssPropertyName);
+
+          return {
+            value: elementCssValue
+          };
         },
 
-        getElementProperty(id, propertyName) {
-          return `/element/${id}/property/${propertyName}`;
+        async getElementProperty(webElement, propertyName) {
+          const element = this.getWebElement(webElement);
+          const elementValue = await element.getAttribute(propertyName);
+
+          return {
+            value: elementValue
+          };
         },
 
-        getElementTagName(id) {
-          return `/element/${id}/name`;
+        async getElementTagName(id) {
+          const element = this.getWebElement(id);
+          const elementTagName = await element.getTagName();
+
+          return {
+            value: elementTagName
+          };
         },
 
-        getElementSize(id) {
-          return `/element/${id}/size`;
+        async getElementSize(id) {
+          const element = this.getWebElement(id);
+          const elementSize = await element.getRect();
+
+          return {
+            value: elementSize
+          };
         },
 
-        getElementText(id) {
-          return `/element/${id}/text`;
+        async getElementText(id) {
+          const element = this.getWebElement(id);
+          const elementText = await element.getText();
+
+          return {
+            value: elementText
+          };
         },
 
-        getElementValue(id) {
-          return `/element/${id}/attribute/value`;
+        // the value param is compulsory
+        async getElementValue(webElement, value) {
+          const element = this.getWebElement(webElement);
+          const elementValue = await element.getAttribute(value);
+
+          return {
+            value: elementValue
+          };
         },
 
-        getElementLocation(id) {
-          return `/element/${id}/location`;
+        async getElementLocation(id) {
+          const element = this.getWebElement(id);
+          const {x, y} = await element.getRect();
+
+          return {
+            value: {
+              x,
+              y
+            }
+          };
         },
 
         isElementLocationInView(id) {
@@ -383,39 +442,109 @@ module.exports = class MethodMappings {
           }
         },
 
-        isElementEnabled(id) {
-          return `/element/${id}/enabled`;
+        async isElementEnabled(webElement) {
+          try {
+            const element = this.getWebElement(webElement);
+            const value = await element.isEnabled();
+
+            return {
+              value
+            };
+          } catch (error) {
+            Logger.error(error);
+
+            return {
+              error,
+              status: -1
+            };
+          }
         },
 
-        isElementSelected(id) {
-          return `/element/${id}/selected`;
+        async isElementSelected(webElement) {
+          try {
+            const element = this.getWebElement(webElement);
+            const value = await element.isSelected();
+
+            return {
+              value
+            };
+          } catch (error) {
+            Logger.error(error);
+
+            return {
+              error,
+              status: -1
+            };
+          }
         },
 
-        clearElementValue(id) {
-          return TransportActions.post(`/element/${id}/clear`);
+        async clearElementValue(webElement) {
+          const element = this.getWebElement(webElement);
+          await element.clear();
+
+          return {
+            value: null
+          };
         },
 
-        setElementValue(id, value) {
+        async setElementValue(webElement, value) {
           if (Array.isArray(value)) {
             value = value.join('');
           } else {
             value = String(value);
           }
 
-          return TransportActions.post({
-            path: `/element/${id}/value`,
-            data: {
-              value: value.split('')
-            }
-          });
+          try {
+            const element = this.getWebElement(webElement);
+            await element.sendKeys(value);
+
+            return {
+              value: null
+            };
+          } catch (error) {
+            Logger.error(error);
+
+            return {
+              error,
+              status: -1
+            };
+          }
         },
 
-        clickElement(id) {
-          return TransportActions.post(`/element/${id}/click`);
+        async clickElement(webElement) {
+          try {
+            const element = this.getWebElement(webElement);
+            await element.click();
+
+            return {
+              value: null
+            };
+          } catch (error) {
+            Logger.error(error);
+
+            return {
+              error,
+              status: -1
+            };
+          }
         },
 
-        elementSubmit(id) {
-          return TransportActions.post(`/element/${id}/submit`);
+        async elementSubmit(webElement) {
+          try {
+            const element = this.getWebElement(webElement);
+            await element.submit();
+
+            return {
+              value: null
+            };
+          } catch (error) {
+            Logger.error(error);
+
+            return {
+              error,
+              status: -1
+            };
+          }
         },
 
         sendKeys(keys) {
@@ -455,7 +584,6 @@ module.exports = class MethodMappings {
         ///////////////////////////////////////////////////////////
 
         async setCookieString(cookie) {
-          
           await this.driver.manage().addCookie(cookie);
 
           return {
@@ -480,14 +608,11 @@ module.exports = class MethodMappings {
         },
 
         async getCookieString() {
-
           const value = await this.driver.manage().getCookies();
 
           return {
             value
           };
-
-
         },
 
         ///////////////////////////////////////////////////////////


Test patch:
diff --git a/test/src/api/commands/testMaximizeWindow.js b/test/src/api/commands/testMaximizeWindow.js
index 27fd6607..f3e8dd1c 100644
--- a/test/src/api/commands/testMaximizeWindow.js
+++ b/test/src/api/commands/testMaximizeWindow.js
@@ -1,5 +1,5 @@
 const assert = require('assert');
-const MockServer  = require('../../../lib/mockserver.js');
+const Mocks = require('../../../lib/command-mocks');
 const CommandGlobals = require('../../../lib/globals/commands.js');
 
 describe('maximizeWindow', function() {
@@ -12,16 +12,9 @@ describe('maximizeWindow', function() {
   });
 
   it('client.maximizeWindow()', function(done) {
-    MockServer.addMock({
-      'url': '/wd/hub/session/1352110219202/window/current/maximize',
-      'response': JSON.stringify({
-        sessionId: '1352110219202',
-        status: 0
-      })
-    }, true);
-
+    Mocks.maximizeWindow();
     this.client.api.maximizeWindow(function callback(result) {
-      assert.strictEqual(result.status, 0);
+      assert.strictEqual(result.value, null);
     });
 
     this.client.start(done);
diff --git a/test/src/api/commands/testWindowCommands.js b/test/src/api/commands/testWindowCommands.js
index 31b0f232..9e2e3c50 100644
--- a/test/src/api/commands/testWindowCommands.js
+++ b/test/src/api/commands/testWindowCommands.js
@@ -13,7 +13,7 @@ describe('window', function () {
 
   it('client.closeWindow()', function (done) {
     this.client.api.closeWindow(function(res) {
-      assert.strictEqual(res.status, 0);
+      assert.strictEqual(res.value, null);
     });
 
     this.client.start(done);
@@ -21,7 +21,7 @@ describe('window', function () {
 
   it('client.switchWindow()', function (done) {
     this.client.api.switchWindow('0', function(res) {
-      assert.strictEqual(res.status, 0);
+      assert.strictEqual(res.value, null);
     });
 
     this.client.start(done);
@@ -43,46 +43,34 @@ describe('window', function () {
       })
     });
 
-    this.client.api.resizeWindow(100, 100, function(res) {
-      assert.strictEqual(res.status, 0);
-    });
-
-    this.client.start(done);
-  });
-
-  it('client.setWindowRect()', function (done) {
     MockServer.addMock({
-      url: '/wd/hub/session/1352110219202/window/rect',
-      method: 'POST',
+      url: '/wd/hub/session/1352110219202/window/current/size',
+      method: 'GET',
       response: JSON.stringify({
         sessionId: '1352110219202',
-        status: 0
+        status: 0,
+        value: {
+          height: 100,
+          width: 100
+        }
       })
     });
 
-    this.client.api.setWindowRect({width: 100, height: 100}, function(res) {
-      assert.strictEqual(res.status, 0);
-    });
-
-    this.client.start(done);
-  });
-
-  it('client.getWindowRect()', function (done) {
     MockServer.addMock({
-      url: '/wd/hub/session/1352110219202/window/rect',
+      url: '/wd/hub/session/1352110219202/window/current/position',
       method: 'GET',
       response: JSON.stringify({
+        sessionId: '1352110219202',
+        status: 0,
         value: {
-          width: 1000,
-          height: 1000,
           x: 100,
           y: 100
         }
       })
     });
 
-    this.client.api.getWindowRect(function(value) {
-      assert.deepStrictEqual(value, {width: 1000, height: 1000, x: 100, y: 100});
+    this.client.api.resizeWindow(100, 100, function(res) {
+      assert.strictEqual(res.value, null);
     });
 
     this.client.start(done);
@@ -98,8 +86,21 @@ describe('window', function () {
       })
     });
 
+    MockServer.addMock({
+      url: '/wd/hub/session/1352110219202/window/current/size',
+      method: 'GET',
+      response: JSON.stringify({
+        sessionId: '1352110219202',
+        status: 0,
+        value: {
+          height: 100,
+          width: 100
+        }
+      })
+    });
+
     this.client.api.setWindowSize(100, 100, function(res) {
-      assert.strictEqual(res.status, 0);
+      assert.strictEqual(res.value, null);
     });
 
     this.client.start(done);
diff --git a/test/src/api/commands/testWindowCommandsW3C.js b/test/src/api/commands/testWindowCommandsW3C.js
index 107f884c..def9d6d2 100644
--- a/test/src/api/commands/testWindowCommandsW3C.js
+++ b/test/src/api/commands/testWindowCommandsW3C.js
@@ -29,7 +29,7 @@ describe('window w3c commands', function () {
 
   it('client.getWindowRect()', function (done) {
     MockServer.addMock({
-      url: '/wd/hub/session/1352110219202/window/rect',
+      url: '/session/13521-10219-202/window/rect',
       method: 'GET',
       response: JSON.stringify({
         value: {
@@ -47,4 +47,8 @@ describe('window w3c commands', function () {
 
     this.client.start(done);
   });
+
+  
+
 });
+