量角器3.1.1 + Angular2:无法找到元素

时间:2016-02-23 21:11:10

标签: javascript angular protractor

我正在使用Protractor 3.1.1和Angular2运行一个简单的HelloWorld示例,但这件事告诉我无法找到元素的可测试性。我在网上搜索了有关错误的一些信息,但没有运气,这似乎是一种新的例外,并没有那么多人面临过。

这是我使用的组件:

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>' +
    '<h2>{{myName}}</h2>' +
    '<input id="someId" [(ngModel)]="myName"/>'
})
export class AppComponent {
    myName = 'Joe';
}

这是Protractor配置文件:

exports.config = {
  framework: 'jasmine',
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: [
    'e2e/*.js'
  ],
  baseUrl: 'http://localhost:3000'
};

这是我正在运行的量角器场景:

describe('angularjs homepage', function() {
    it('should say my name', function() {
        browser.get('/index.html');
        var greeting = element(by.id('someId'));
        expect(greeting.getAttribute('value')).toEqual('Joe');
    });
});

网页正常加载模板HTML,但是Protractor认为生成的网页不是Angular网页,现在,为什么呢?显然,如果我检查生成的网页,它只是处理过的Angular代码的结果HTML,我做错了吗?

这是完整的错误:

Error: Failed: Error while waiting for Protractor to sync with the page: "Could not find testability for element."

如果我按照Protractor Tutorial所说的那样运行一个简单的测试,使用这个演示页面:http://juliemr.github.io/protractor-demo/,它按预期工作,所以我的Angular2代码有一些,Protractor没有使用它,但我已经运行了出于想法,任何人都知道发生了什么?

更新23-02-2016

经过一些研究后我发现要将Protractor与Angular2一起使用,配置文件中必须有一个额外的配置行:

useAllAngular2AppRoots: true

这样conf.js现在看起来像:

exports.config = {
  framework: 'jasmine',
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: [
    'e2e/*.js'
  ],
  baseUrl: 'http://localhost:3000',
  useAllAngular2AppRoots: true
};

或明确包含应用根:

rootElement: 'my-app'

在此更新之后,查找元素by.id()的所有调用都可以正常工作,但如果您假装使用任何定位器by.model()by.binding(),则它将失败并显示消息{ {1}}。不知道为什么。

2 个答案:

答案 0 :(得分:2)

显然该提交中存在错误。 Check this!

但是现在你可以运行你的测试了 browser.executeScript('window.name = "NG_ENABLE_DEBUG_INFO!"');

答案 1 :(得分:0)

使用量角器和Angular,在您的rootElement: 'my-app-root'中添加protractor.conf.js,其中my-app-root是元素名称(CSS选择器)。 here中提供了配置文档。