Custom browser actions in Protractor

后端 未结 4 730
耶瑟儿~
耶瑟儿~ 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:10

    I think it is possible to extend the browser.actions() function but that is currently above my skill level so I'll lay out the route that I would take to solve this issue. I would recommend setting up a "HelperFunctions.js" Page Object that will contain all of these Global Helper Functions. In that file you can list your browser functions and reference it in multiple tests with all of the code in one location.

    This is the code for the "HelperFunctions.js" file that I would recommend setting up:

    var HelperFunctions = function() {
        this.longClick = function(targetElement) {
            browser.actions().mouseDown(targetElement).perform();
            browser.sleep(5000);
            browser.actions().mouseUp(targetElement).perform();
        };
    };
    
    module.exports = new HelperFunctions();
    

    Then in your Test you can reference the Helper file like this:

    var HelperFunctions = require('../File_Path_To/HelperFunctions.js');
    
    describe('Example Test', function() {
        beforeEach(function() {
            this.helperFunctions = HelperFunctions;
    
            browser.get('http://www.example.com/');
        });
    
        it('Should test something.', function() {
            var Element = element(by.className('targetedClassName'));
            this.helperFunctions.longClick(Element);
        });
    });
    

    In my Test Suite I have a few Helper files setup and they are referenced through out all of my Tests.

提交回复
热议问题