Protractor onPrepare() native example from GitHub

时间:2018-02-01 18:11:16

标签: javascript github protractor

Example below is taken from Protractor GitHub. Since I'm new to protractor I'd like to understand everything thoroughly.

onPrepare: function() {
browser.driver.get(env.baseUrl + '/ng1/login.html');

browser.driver.findElement(by.id('username')).sendKeys('Jane');
browser.driver.findElement(by.id('password')).sendKeys('1234');
browser.driver.findElement(by.id('clickme')).click();

// Login takes some time, so wait until it's done.
// For the test app's login, we know it's done when it redirects to
// index.html.
return browser.driver.wait(function() {
  return browser.driver.getCurrentUrl().then(function(url) {
    return /index/.test(url);
  });
}, 10000);
}

So I don't entirely understand what last 3 return statements do? Especially this line

return /index/.test(url);

Any ideas?

1 个答案:

答案 0 :(得分:3)

Protractor API

中解释了其中两个功能

getCurrentUrl是自解释的,它会检索当前的网址。

wait也是相当自我解释的,它等待一个promise,条件对象或一个函数来评估条件

最后一个.test是一个javascript regular expression method,用于执行搜索匹配"

所以整个块只是说等待当前URL传递.test的条件(条件是URL包含"索引")