简体   繁体   English

如何使量角器的各个部分等待或同步?

[英]How to make parts of Protractor wait or become synchronous?

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: 注意,我在NODE.js环境中运行它:

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 . 但是我无法在验证browser.getCurrentUrl()之前触发点击,所以发生的事情是我获取了登录页面的网址, 我想在点击登录后获取该网址

I suspect it's my misunderstanding of how the Asynchronous nature of this works. 我怀疑这是我对这种异步性工作原理的误解。

How to make parts of Protractor wait or become synchronous? 如何使量角器的各个部分等待或同步?

You can wait for the url to change with the help of browser.wait() : 您可以在browser.wait()的帮助下等待url的更改:

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". 其中urlChanged (以selenium为单位)是“自定义预期条件”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM