How to make parts of Protractor wait or become synchronous?

我是研究僧i 提交于 2019-12-08 06:43:10

问题


I have code like this in a protractor test, it goes to a home page determines we need to login and then calls this function. Note I'm running this in a NODE.js environment:

function Login() {
    browser.findElement(by.id('ID')).then(function (ele) {
        ele.sendKeys('SomeUserName');
        browser.findElement(by.id('password')).then(function (ele) {
            ele.sendKeys('SomePassword');
            browser.findElement(by.partialButtonText('Sign in')).then(function (ele) {
                ele.click();
                browser.getCurrentUrl().then(function (url) {
                    expect(url).toBe("http://NextURLAfterClick");
                    debugger;
                });
            });
        });
    });
}

But I can't get the click to fire prior to validating the browser.getCurrentUrl(), so what's happening is I'm getting the url of the login page, I want to get the url after the click to login.

I suspect it's my misunderstanding of how the Asynchronous nature of this works.


回答1:


How to make parts of Protractor wait or become synchronous?

You can wait for the url to change with the help of browser.wait():

var urlChanged = function(url) {
  return function () {
    return browser.getCurrentUrl().then(function(actualUrl) {
      return url === actualUrl;
    });
  };
};

element(by.id('ID')).sendKeys('SomeUserName');
element(by.id('password')).sendKeys('SomePassword');
element(by.partialButtonText('Sign in')).click();

browser.wait(urlChanged("http://NextURLAfterClick")), 5000);

where urlChanged, in selenium terms, is a "Custom Expected Condition".



来源:https://stackoverflow.com/questions/30491815/how-to-make-parts-of-protractor-wait-or-become-synchronous

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