Override click command webdriverio

爷,独闯天下 提交于 2020-01-15 10:11:49

问题


I would like to override the click command in webdriverio

Before every click i would check if the locator exist. I would like something like this:

browser.addCommand("click", function () {
      browser.waitUntil()      
      browser.click()
}, true);

What is the best way to implement this?

--Update

i implement no something like this:

  afterCommand: function (commandName) {
    if (['click'].includes(commandName)) {
      browser.waitUntilPageIsLoaded()
    }
  }

After each click command i wait until the page is loaded. Works good for my situation.


回答1:


Rather than trying to override core (and documented) functionality, I would recommend creating a 'waitThenClick' function, similar to what you already have.




回答2:


I would suggest to create a class to hold all your actions. By default it can call the browser functions, you can override the actions you want some special operation.

class Action {

    constructor() {
        //assign browser function unless overridden
        Object.keys(browser)
            .filter(key => !this[key])
            .forEach(key => this[key] = browser[key]);
    }

    /*Override functions*/
    click(sel) {
        browser.waitUntil();
        browser.click(sel);
    }
}

In your test you can use 'Action' to do operations.

  describe('Suite', function() {
    it('Case', function() {
        Action.getText('#div');
        Action.click('#button');
    });
});

This way you can have a clearer code for maintenance.




回答3:


I would not recommend to override that function. Try to build a command on top of it. I think wait for an element before getting it should be mandatory when creating test steps.

browser.addCommand(`waitAndClick`, function () {
        return browser
          .waitForVisible(arguments[0])
          .then(() => {
            return browser.click.apply(this, arguments);
          });
      };); 

You could also implement this for most actions: 'click', 'getValue', 'setValue', 'getCssProperty', 'getAttribute', etc




回答4:


This is what we did. A function that tries to click on an element until it succeeds.

Disclaimer: this works well for API versions up to 4.0. Have 2 projects concurrent, one on older API and one on 4+. Don't need it on 4+.

module.exports = function ClickWithRetry (selector, tries, callback) {
    this.click(selector, function (err) {
        if (err != null && tries >= 0) {//We had som kind of error like selector was not yet visible.. try again
            this.pause(500);
            this.ClickWithRetry(selector, tries - 1, callback);
        } else {
            callback(err);
        }
    });
};


来源:https://stackoverflow.com/questions/41854598/override-click-command-webdriverio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!