量角器 E2E 和 Angular

Protractor E2E and Angular

我无法弄清楚如何在我们的单页应用程序中定位元素。

我正在测试大部分经过身份验证的页面,因此使用量角器模拟数据:ngMockE2E$httpBackend

假设有 4 个 pages/views:首页 | 关于 | 设置 | 退出

设置页面结构大致为:

<div ng-view class="ng-scope">
  <div class="row ng-scope" ng-controller="SettingsCtrl as ctrl">
    <h1>Settings Main Page</h1>
    <div ng-if="show.more">
      <a href="#/more/info">More Info</a>
      [...]
    </div>
  </div>
</div>

如果您单击 更多信息 link,视图将更改为该页面,其中唯一的主要区别在于 ng-controller

<div ng-view class="ng-scope">
  <div class="row ng-scope" ng-controller="SettingsCtrl">
    <h2>Settings More Info Page</h2>
    [...]
  </div>
</div>

在量角器中,我不知道如何访问 更多信息页面 中的元素 - 但我可以在 主要设置 [=46] 中找到这些元素=] 页面就好了。

例如:

var main_title = element(by.tagName('h1')).isPresent();
expect(main_title).toBe(true); // Will return true

var moreInfo_title = element(by.tagName('h2')).isPresent();
expect(moreInfo_title).toBe(true); // Will return false

为什么我不能访问视图中的元素?

您可能需要等待 h2 元素的 presence:

// click "More info"

var EC = protractor.ExpectedConditions;
var moreInfo_title = element(by.tagName('h2'));  

browser.wait(EC.presenceOf(moreInfo_title), 5000);

expect(moreInfo_title.isPresent()).toBe(true);

您应该尝试单击 link,然后检查是否存在更多信息标题。

var main_title = element(by.tagName('h1')).isPresent();
expect(main_title).toBe(true);

var link = element(by.linkText('More Info'));
link.click().then(function() {
   var moreInfo_title = element(by.tagName('h2')).isPresent();
   expect(moreInfo_title).toBe(true);
});