Custom browser actions in Protractor

后端 未结 4 728
耶瑟儿~
耶瑟儿~ 2020-11-29 09:16

The problem:

In one of our tests we have a \"long click\"/\"click and hold\" functionality that we solve by using:

browser.actions()         


        
4条回答
  •  清歌不尽
    2020-11-29 10:09

    Here is what I did (based on the perfect @Louis's answer).

    Put the following into onPrepare() in the protractor config:

    // extending action sequences
    protractor.ActionSequence.prototype.sleep = function (delay) {
        var driver = this.driver_;
        this.schedule_("sleep", function () { driver.sleep(delay); });
        return this;
    };
    
    protractor.ActionSequence.prototype.perform = function () {
        var actions = this.actions_.slice();
        var driver = this.driver_;
        return driver.controlFlow().execute(function() {
            actions.forEach(function(action) {
                var command = action.command;
                if (typeof command === "function")
                    driver.flow_.execute(command);
                else
                    driver.schedule(command, action.description);
            });
        }, 'ActionSequence.perform');
    };
    
    protractor.ActionSequence.prototype.clickAndHold = function (elm) {
        return this.mouseDown(elm).sleep(3000).mouseUp(elm);
    };
    

    Now you'll have sleep() and clickAndHold() browser actions available. Example usage:

    browser.actions().clickAndHold(element).perform();
    

提交回复
热议问题