量角器期望元素最终出现

Mic*_*mza 4 jasmine angularjs protractor e2e-testing

有没有办法期望元素最终出现在页面上?例如一种方法

browser.wait(protractor.ExpectedConditions.presenceOf(element(by.partialLinkText('Continue'))), 1000, 'Unable to find continue link');
Run Code Online (Sandbox Code Playgroud)

因预期错误而不是超时而失败?本质上是一种在下面的行中使用 aisEventuallyPresent()代替的方法isPresent()

expect(element(by.partialLinkText('Continue')).isPresent()).toBe(true);
Run Code Online (Sandbox Code Playgroud)

作为参考,我正在使用browser.ignoreSynchronization = true即使它是一个 Angular 应用程序,并使用 Jasmine(至少现在是这样)。

Mic*_*mza 5

利用以下事实

  • browser.wait返回一个 Promise,一旦条件函数返回 true,该 Promise 就会被解决;如果超时则被拒绝。

  • 如果expect传递了一个 Promise,它只会在 Promise 得到解决时才运行期望

您可以创建一个函数来包装对browser.wait

function eventual(expectedCondition) {
  return browser.wait(expectedCondition, 2000).then(function() {
    return true;
  }, function() {
    return false;
  });
}
Run Code Online (Sandbox Code Playgroud)

然后创造一个期望

expect(eventual(protractor.ExpectedConditions.presenceOf(element(by.partialLinkText('Continue'))))).toBe(true);
Run Code Online (Sandbox Code Playgroud)

或者,要使其在任何浏览器实例上工作,您可以对 Protractor 原型进行猴子修补

protractor.Protractor.prototype.eventual = function(expectedCondition) {
  return this.wait(expectedCondition, 2000).then(function() {
    return true;
  }, function() {
    return false;
  });
}
Run Code Online (Sandbox Code Playgroud)

并可以用作

expect(browser.eventual(protractor.ExpectedConditions.presenceOf(element(by.partialLinkText('Continue'))))).toBe(true);
Run Code Online (Sandbox Code Playgroud)

为了避免超时,您必须确保传递给的超时browser.wait小于 Jasmine 异步测试超时,该超时在jasmineNodeOpts: {defaultTimeoutInterval: timeout_in_millis}量角器配置文件中指定