没有为 getTestability 的元素参数找到注入器

No injector found for element argument to getTestability

我正在尝试 运行 this tutorial 反对我自己的 AngularJS 代码。我无法通过第一个测试。任何从我的页面中提取任何信息的尝试都会出现此错误:

     Error: Error while waiting for Protractor to sync with the page: "[ng:test] no injector found for element argument to getTestability\nhttp://errors.angularjs.org/1.3.8/ng/test"
   Stacktrace:
     Error: Error while waiting for Protractor to sync with the page: "[ng:test] no injector found for element argument to getTestability\nhttp://errors.angularjs.org/1.3.8/ng/test"
    at Error (<anonymous>)
==== async task ====

Protractor.waitForAngular()
==== async task ====
Asynchronous test function: it()

是否有人有 Protractor 教程向您展示如何针对您自己的代码而不是其他人的代码?

提前致谢

答案是 html 页面顶部必须有 <html ng-app><html ng-app = ""> 才能使用 Protractor。

例如,将应用程序作为 <div ng-app = ""> 的一部分是行不通的

我在使用 jasmine 和量角器进行 E2E 测试时遇到了同样的问题。

这与根 html 标签有关。

在运行量角器时,我遇到了错误..
"Error while waiting for Protractor to sync with the page: " [ng: test] 没有找到喷油器 对于 getTestability 的元素参数\ nhttp: //errors.angularjs.org/1.3.13/ng/test"

为了解决这个问题,我在根 html 标签中做了一个小改动 通过放置 lang="en" 属性。 这解决了我的问题。

决赛HTML

<!DOCTYPE html> <html lang="en" ng-app="test"> ..... </html>

希望对您有所帮助。

谢谢。

就像接受的答案所暗示的那样,问题在于让量角器知道您的应用程序的根是什么,这是定义 ng-app 的地方。默认情况下,这应该是标签,但您的代码似乎在其他地方。尽管如此,您实际上可以在配置文件中更改该行为。

来自量角器reference config file

  // CSS Selector for the element housing the angular app - this defaults to
  // body, but is necessary if ng-app is on a descendant of <body>.
  rootElement: 'body',

例如,如果您的 ng-app 在这样的 div 中:

<div id="my-app" ng-app=myAppManager">

你可以使用

rootElement: '#my-app'

因为它是一个 CSS select 或者,你也可以使用方括号语法来 select 按属性,像这样:

rootElement: '[ng-app]'

这将 select 包含属性 ng-app 的元素,无论它是哪一个。

您可能有一个不相关的 angular 注入器错误导致量角器出现此输出。尝试清除您的终端,再次 运行 量角器,并查看显示在详细量角器输出上方的第一个错误。如果它是 angular 注入器错误,您可以通过简单地向 index.html.

添加脚本标签来修复它

终端输出:
Error: [$injector:modulerr] Failed to instantiate module app.components.slider

可以通过将以下内容添加到 index.html 来修复:
<script src="app/components/sliders/slider.component.js"></script>

这是有效的,因为模块 app.components.slider 是在文件 app/components/sliders/slider.component.js 中定义的。它可能在 merge/rebase 期间从您的 index.html 文件中意外删除,或者一开始就没有添加。

我的 angular 引导代码如下:

var appName = "testApp";
var appContainer = document.getElementById('app-container');
angular.bootstrap(appContainer, [appName]);

我得到了和你一样的错误,根据我从@jgiralt 那里得到的建议,这是我的 conf.js 量角器修复了这个错误:

// conf.js
exports.config = {
  framework: 'jasmine',
  seleniumAddress: 'http://localhost:4444/wd/hub',
  rootElement: '#app-container',
  specs: ['spec.js']
}