简体   繁体   English

量角器E2E和Angular

[英]Protractor E2E and Angular

I'm having trouble figuring out how to target elements in our single-page application. 我在弄清楚如何在单页应用程序中定位元素时遇到了麻烦。

I'm testing mostly authenticated pages, so the data is being mocked for protractor using: ngMockE2E and $httpBackend 我正在测试大多数经过身份验证的页面,因此正在使用ngMockE2E$httpBackend对量角器$httpBackend

Let's say there are 4 pages/views: Home | 比方说,有4页/访问量: 首页 | About | 关于 | Settings | 设置 | Sign out 登出

The Settings page structure is roughly: 设置页面的结构大致如下:

<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>

If you click the more info link, the view will change to that page where the only major difference is in the ng-controller . 如果单击更多信息链接,则视图将切换到该页面,唯一的不同之处在于ng-controller

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

In protractor I cannot figure out how to access the elements within the more info page - but I can locate the elements on the main settings page just fine. 在量角器中,我无法弄清楚如何访问更多信息页面中的元素-但我可以在主设置页面上找到这些元素。

For example: 例如:

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

Why can't I access elements within a view? 为什么无法访问视图中的元素?

You might need to wait for the presence of the h2 element : 您可能需要等待存在的的h2元素

// 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);

You should try clicking the link then check to see if the more info title exists. 您应该尝试单击链接,然后检查是否存在更多信息标题。

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);
});

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

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