【问题标题】:No injector found for element argument to getTestability没有为 getTestability 的元素参数找到注入器
【发布时间】:2018-11-03 00:02:33
【问题描述】:

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

     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 教程,向您展示如何定位自己的代码,而不是其他人的代码?

提前谢谢你

【问题讨论】:

标签: angularjs jasmine protractor


【解决方案1】:

就像公认的答案所暗示的那样,问题在于让量角器知道您的应用程序的根是什么,这就是定义 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 中:

&lt;div id="my-app" ng-app=myAppManager"&gt;

你可以使用

rootElement: '#my-app'

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

rootElement: '[ng-app]'

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

【讨论】:

  • 在我的例子中,ng-app 在 html 中, 我需要什么提?根元素:'html'?在启动 selenium 服务器之前我需要在配置中提到哪里(我使用“directConnect”)?
  • 如果我的页面没有“ng-app”怎么办,我应该要求开发人员在页面中添加它还是有任何解决方法?请提出建议。
【解决方案2】:

答案是您的 html 页面顶部必须有 &lt;html ng-app&gt;&lt;html ng-app = ""&gt; 才能使用 Protractor。

例如,将应用程序作为 &lt;div ng-app = ""&gt; 的一部分将不起作用

【讨论】:

  • 如果你在 protractor.config.js 中设置 rootElement: "[ng-app]" 就可以了
  • 但是请注意,将 ng-app 放在 标签上会起作用。
  • 虽然有&lt;html ng-app&gt; or &lt;html ng-app = ""&gt;,但我们仍然收到错误:等待量角器与页面同步时出错:“[ng:test] 没有找到用于 getTestability 的元素参数的注入器,而用量角器测试什么我们可以吗?
【解决方案3】:

我的角度引导代码如下:

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']
}

【讨论】:

    【解决方案4】:

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

    这与根html标签有关。

    在运行量角器时,我得到了错误..
    “等待 Protractor 与页面同步时出错:” [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>

    希望对你有帮助。

    谢谢。

    【讨论】:

    • 我遇到了完全相同的问题,但您的解决方案没有解决它。我正在手动引导角度。您对此有何其他见解?
    • 代码sn-p可以共享吗?所以,我对你的问题有了更好的了解。
    • 它真的没什么特别的。我有一个带有 id 的 div 并在其上引导我的应用程序:angular.bootstrap(document.getElementById("myapp"), ["myapp.module" ]);
    【解决方案5】:

    您可能有一个不相关的角喷射器错误导致量角器输出此输出。尝试清除终端,再次运行量角器,然后查看显示在详细量角器输出上方的第一个错误。如果是角度注入器错误,您只需在 index.html 中添加脚本标记即可修复它。

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

    可以通过在 index.html 中添加以下内容来修复:
    &lt;script src="app/components/sliders/slider.component.js"&gt;&lt;/script&gt;

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

    【讨论】:

      猜你喜欢
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      相关资源
      最近更新 更多