PageObjects + 非 angular 页面 - 如何加入它们?

PageObjects + non-angular page - how to join them?

我正在为我们的应用程序编写一些量角器测试。 在angular页面中有一个地方是非angular页面作为iframe。

问题:我无法在页面对象对象中映射来自非angular页面的字段并在我的测试规范中正确地使用它们。

备注:

这是我的规格>

'use strict';

var DamagePage = require('../../pages/case/CaseDamagePage.po.js');

describe('Damage selection - ', function() {

  var damagePage = new DamagePage();

  it('Switch to iFrame',function(){
      browser.driver.switchTo().frame('padIframe');
  });

  it('Open a zone', function() {
      browser.driver.findElement(by.id('30_0_79')).click();
      browser.sleep(2000);
  });

  it('Select a part', function () {
      browser.driver.findElement(by.id('32_0_201')).click();
      browser.sleep(3000);
  });

  it('Put I on it with 5 WU', function() {
      // Click I
      damagePage.leftMenuButtons.I.button.click();
  });

这是我的 PageObject(实际上是函数)>

'use strict';

var CaseInterface = require('./CaseInterface.js');

var DamagePage = function() {

  // LEFT-SITE MENU BUTTONS
  this.leftMenuButtons = {
      I: {
          button: browser.driver.findElement(by.id('operation-button-I'))
      },
      E: {
          button: element(by.id('operation-button-E')),
          mutation: element(by.id('operation-button-mutation-E'))
      }
  };
};

module.exports = DamagePage;

在规范中,在 'Put I on it with 5 WU' 步骤之前一切正常。在此配置中,量角器启动后立即出现错误:

C:\Users\xxx\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:108 var template = new Error(this.message); ^ NoSuchElementError: no such element: Unable to locate element: {"method":"id","selector":"operation-button-I"} (Session info: chrome=47.0.2526.106) (Driver info: chromedriver=2.19.346078 (6f1f0cde889532d48ce8242342d0b84f94b114a1),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 22 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

嗯,这要归功于页面对象中的 "browser.driver.findElement" 语法。当我将其更改为 "element" 时,量角器正确启动并 运行 直到 'Put I on it with 5 WU' 步骤。这里抛出:

Failed: Error while waiting for Protractor to sync with the page: "angular could not be found on the window"

I got error immediately after protractor start

这是因为您初始化页面对象的位置 - Protractor 在收集规范时很早就初始化了您的页面对象。您需要在 beforeEach():

中初始化页面对象
describe('Damage selection - ', function() {
    var damagePage;

    beforeEach(function () {
       damagePage = new DamagePage();
    });

    // ...
});

或者,直接在it()里面:

it('Put I on it with 5 WU', function() {
    var damagePage = new DamagePage();

    // Click I
    damagePage.leftMenuButtons.I.button.click();
});