Protractor Button Click and open page in new tab

后端 未结 5 803
我在风中等你
我在风中等你 2021-02-08 02:21

I am fairly new to Protractor. I am trying to automate a scenario where I click on a button and its opens up a page in new tab and then we need to populate form in new page and

5条回答
  •  粉色の甜心
    2021-02-08 03:02

    Here is implementation without using browser.sleep() method. Function waitForNewWindow() is created using async and underscorejs. Where async.until() method is used for calling getAllWindowHandles() synchronously.

    element(by.id("newPlan")).click()
        .then(function () {
            return waitForNewWindow();
        })
        .then(function (newWindowHandle) {
            browser.switchTo().window(newWindowHandle).then(function () {
                expect(browser.getCurrentUrl()).toMatch(/\/url/);
            });
        });
    
     /**
     * Wait for new window is opened
     *
     * @param {Object} [params]
     * @param {number} [params.runs] - number of tries
     * @param {number} [params.interval] - interval for launching getAllWindowHandles()
     *
     * @returns {webdriver.promise.Promise}
     */
    function waitForNewWindow(params) {
        var currentHandles = [];
        var deferred = protractor.promise.defer();
        var finish;
        var newHandle;
        var numberOfRuns = 0;
    
        params = params ? params : {};
        params.runs = params.runs || 10;
        params.interval = params.interval || 1000;
    
        browser.driver.getAllWindowHandles()
            .then(function (handles) {
                currentHandles = handles;
            })
            .then(function () {
                async.until(
                    // function that tests condition
                    function () {
                        return newHandle || finish;
                    },
                    // function that is executed until test condition is true
                    function (callback) {
                        browser.driver.getAllWindowHandles()
                            .then(function (newHandles) {
                                numberOfRuns++;
    
                                if (numberOfRuns > params.runs) {
                                    finish = true;
    
                                    return callback(null, newHandle);
                                }
    
                                if (currentHandles.length < newHandles.length) {
                                    newHandle = _.difference(newHandles, currentHandles);
    
                                    return callback(null, newHandle);
                                }
    
                                setTimeout(function () {
                                    callback(null, newHandle);
                                }, params.interval);
                            });
                    },
                    // callback when test condition is true
                    function (error, result) {
                        if (!result) {
                            return deferred.reject('New browser window hasn\'t been opened');
                        }
    
                        if (result.length > 1) {
                            return deferred.reject('More than one new browser window were opened');
                        }
    
                        deferred.fulfill(result.toString());
                    }
                );
            });
    
        return deferred.promise;
    };
    

提交回复
热议问题