javascript 量角器无法找到 Angular

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19391813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:24:53  来源:igfitidea点击:

Protractor fails to find Angular

javascriptangularjsseleniumjasmineprotractor

提问by MBielski

I can't seem to get Protractor to realize that Angular is loaded and running. When it opens Chrome my app loads fully in the browser, so I know that Angular is loaded and running correctly.

我似乎无法让 Protractor 意识到 Angular 已加载并正在运行。当它打开 Chrome 时,我的应用程序在浏览器中完全加载,所以我知道 Angular 已正确加载并运行。

The config file:

配置文件:

exports.config = {
seleniumServerJar: 'C:/Dev/PrismWeb/selenium/selenium-server-standalone-2.35.0.jar',

seleniumPort: null,

chromeDriver: 'C:/Dev/PrismWeb/selenium/chromedriver.exe',

seleniumArgs: [],

seleniumAddress: null,

allScriptsTimeout: 110000,

specs: ['c:/dev/prismweb/test/e2e/*.js'],

capabilities: {'browserName': 'chrome'},

baseUrl: 'http://localhost:8080',

rootElement: 'html',

jasmineNodeOpts: {
    onComplete: null,
    isVerbose: true,
    showColors: true,
    includeStackTrace: true,
    defaultTimeoutInterval: 30000
}
};

I've only got one test that I am trying to run and it fails because Protractor can't find Angular.

我只有一个我试图运行的测试,它失败了,因为量角器找不到 Angular。

The Test:

考试:

describe('homepage loads: ', function(){
var ptor;

    ptor = protractor.getInstance();

    beforeEach(function(){
        ptor.get('/');
    });

it('should load the prism homepage: ', function(){
    var usernameField = ptor.findElement(protractor.By.id("username"));
    //expect(usernameField).toBeDefined();
});
});

This is the error I get:

这是我得到的错误:

UnknownError: javascript error: angular is not defined (Session info: chrome=30.0.1599.69) (Driver info: chromedriver=2.2,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 19 milliseconds Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01' System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_21' Session ID: 1ef7dcd7c5fc9c4e9e1dede050002adf Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={chromedriverVersion=2.2}, rotatable=false, locationContextEnabled=true, version=30.0.1599.69, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]

UnknownError: javascript error: angular is not defined (Session info: chrome=30.0.1599.69) (Driver info: chromedriver=2.2,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) 命令持续时间或超时:19 毫秒 构建信息:版本:'2.35.0',修订版:'c916b9d',时间:'2013-08-12 15:42:01' 系统信息:os.name:'Windows 7',os.arch : 'amd64', os.version: '6.1', java.version: '1.7.0_21' 会话 ID: 1ef7dcd7c5fc9c4e9e1dede050002adf 驱动程序信息: org.openqa.selenium.chrome.ChromeDriver Capabilities [{platform=javascriptEnabled, acceptS] =true,browserName=chrome,chrome={chromedriverVersion=2.2},rotatable=false,locationContextEnabled=true,version=30.0.1599.69,cssSelectorsEnabled=true,databaseEnabled=true,handlesAlerts=true,browserConnectionEnabled=false,webStorageEnabled=true,nativeEvents=true,applicationCacheEnabled=false,takesScreenshot=true}]

I've tried moving the ng-app attribute to the body tag (and changing the config accordingly) but I got the same error. We also have an ng-controller attribute on the html tag, and I tried moving that to the body and leaving the ng-app attribute alone on the html tag, but the result was the same. Does anybody have any insight as to why this fails?

我已经尝试将 ng-app 属性移动到 body 标签(并相应地更改配置),但我遇到了同样的错误。我们在 html 标签上也有一个 ng-controller 属性,我尝试将它移到正文中并将 ng-app 属性单独留在 html 标签上,但结果是一样的。有没有人知道为什么会失败?

EDIT: Just a couple of notes that might help. I've updated the test above to include the manual bootstrapping efforts. The script tags for Angular and all of the modules are located at the bottom of the page, right before the closing BODY tag. The HTML tag still has the ng-app="myApp" attribute plus the ng-controller="baseController" attribute. If I try to bootstrap the app manually in the test, I get the following:

编辑:只是一些可能有帮助的注释。我已经更新了上面的测试以包括手动引导工作。Angular 和所有模块的脚本标签位于页面底部,就在结束 BODY 标签之前。HTML 标记仍然具有 ng-app="myApp" 属性和 ng-controller="baseController" 属性。如果我尝试在测试中手动引导应用程序,我会得到以下信息:

ReferenceError: angular is not defined

One other thing that worries me is that one of the modules we use absolutely needs "$" to be mapped to jQuery, so we map it like this:

另一件让我担心的事情是,我们使用的模块之一绝对需要将“$”映射到 jQuery,所以我们这样映射它:

<script type="text/javascript">
    var $jq=jQuery.noConflict();
    var $=jQuery.noConflict();
</script>

Where the ng-app is included:

包含 ng-app 的地方:

<!DOCTYPE html>
<html ng-app="prismApp" ng-controller="baseController">
<head>

采纳答案by preeve

I'm not too sure about doing a beforeEach()within a, it()but that aside, have you tried waiting a while? Obviously by this i mean either a ptor.waitForAngular()or a ptor.wait()?

我不太确定在 abeforeEach()内做一个,it()但除此之外,你有没有试过等一会儿?显然,我的意思是 aptor.waitForAngular()或 a ptor.wait()?

Try a ptor.sleep(10000)after your ptor.get()just to see if it is a timing thing.

试试 a ptor.sleep(10000)after you ptor.get()just 看看它是否是一个时间问题。

Edit: also take a look at the protractor apiand how wait()works:

编辑:还可以看看量角器 api以及它是如何wait()工作的:

ptor.wait(function () {
  // Return a condition. Code will continue to run once it is true      
}, 10000); // Only do this for 10 seconds

Edit: Try ptor.driver.get('my-page');instead of ptor.get('my-page');

编辑:尝试ptor.driver.get('my-page');而不是ptor.get('my-page');

Edit: Protractor now exposes browseras a global so you can just use browser.get('index.html#/foo')or browser.wait()etc.

编辑:量角器现在公开browser作为一个全球性的,所以你可以只使用browser.get('index.html#/foo')browser.wait()

回答by Mohammad Ibrahim Khan

Try including this in your test file:

尝试将其包含在您的测试文件中:

browser.ignoreSynchronization = true;

It switches off your test's sync with angular and makes it work.

它会关闭您的测试与 angular 的同步并使其工作。

回答by ApacheTyler

Are you using ng-app to bootstrap your Angular application? Or are you manually bootstrapping your application?

您是否使用 ng-app 来引导您的 Angular 应用程序?或者您是否手动引导您的应用程序?

I see that in your configuration file that rootElementis set to 'html'.

我在您的配置文件中看到rootElement设置为“html”。

By default protractor assumes that the root of the app will be on the body tag. If the <html>tag is not where angular bootstraps in your application, try changing the root element in the config file to where angular first bootstraps(wether it is manually or though using the ng-app directive). This really helps with timing issues, and speeds protractors execution up.

默认情况下,量角器假定应用程序的根将在 body 标签上。如果<html>标记不在应用程序中 angular 引导程序的位置,请尝试将配置文件中的根元素更改为 angular 首先引导程序的位置(无论是手动还是使用 ng-app 指令)。这确实有助于解决计时问题,并加快量角器的执行速度。