【问题标题】:Protractor: Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined量角器:等待量角器与页面同步时出错:“angularJS 可测试性和角度可测试性均未定义
【发布时间】:2019-12-07 00:56:06
【问题描述】:

我正在尝试编写一些端到端测试,并希望使用异步和等待。

配置文件

exports.config = {
    framework: 'jasmine',
    seleniumAddress: 'http://localhost:4444/wd/hub',
    specs: ['spec.js'],
    SELENIUM_PROMISE_MANAGER: false,
    getPageTimeout: 10000,
    multiCapabilities: [
        {
            browserName: 'firefox'
        }, {
            browserName: 'chrome'
        }
    ]
}

规格文件

    describe('home-view', function(){

    beforeEach(async function(){

        await browser.get('http://localhost:49335/index.html#!/home');

    });

    it('sorted by firstname', async function(){

        await element(by.css("[ng-click=\"sortData('firstname')\"]")).click();
        var firstname = element.all(by.repeater('a in emps')).all(by.css('td'));     
        expect(await firstname.get(0).getText()).toEqual('abraham');

    });

})

错误 等待 Protractor 与页面同步时出错:“angularJS 可测试性和 Angular 可测试性都未定义。这可能是因为这是一个非 Angular 页面,也可能是因为您的测试涉及客户端导航,这可能会干扰 Protractor 的引导。 "

为什么会出现此错误?谢谢

【问题讨论】:

    标签: angularjs selenium protractor e2e-testing


    【解决方案1】:

    您收到此错误是因为 Protractor 默认会加载等待角度页面。如果您使用非角度工作,则应将 await browser.waitForAngularEnabled(false); 添加到 onPrepare 块:

     onPrepare: async () => {
     ...
     await browser.waitForAngularEnabled(false);
     ...  
    
    

    这种“等待”机制是如何工作的?我将从代码中复制描述:

         * If set to false, Protractor will not wait for Angular $http and $timeout
         * tasks to complete before interacting with the browser. This can cause
         * flaky tests, but should be used if, for instance, your app continuously
         * polls an API with $timeout.
    

    所以,正如您所见,这一切都与 $http 和 $timeout 任务有关。开发人员经常以不正确的方式使用它。

    总之,如果你看到这样的错误:

    both angularJS testability and angular testability are undefined
    

    您必须添加await browser.waitForAngularEnabled(false);。

    【讨论】:

    • 这是一个 angularjs 应用程序
    • 如果您需要,我会更深入地回答我的问题
    • @Oleksii,是来自protractor 的browser 变量吗?
    • @陈大超是
    【解决方案2】:

    让 getPageTimeOut 超过 20 秒。在 browser.get 方法之后使用像 browser.sleep(2000) 这样的显式等待。发生的错误可能是由于网页响应缓慢,并且还使用 dirctConnect 而不是 seleniumAddress。

    【讨论】:

      【解决方案3】:

      之前我只需要在我的 script.js 中添加这个 browser.driver.ignoreSynchronization = true;

      但是添加这个解决了我的问题。 browser.waitForAngularEnabled(false);

      所以最终的 script.js 是

      describe('My first non angular class', function() {
          it('My function', function() {
              browser.driver.ignoreSynchronization = true;
              browser.waitForAngularEnabled(false);
              browser.driver.manage().window().maximize();
              //browser.get('http://juliemr.github.io/protractor-demo/');
              browser.driver.get('https://stackoverflow.com/users/login');
              element(by.id('email')).sendKeys('6');
      
          })
      
      }) 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-11
        • 2019-10-03
        • 2014-05-16
        • 1970-01-01
        • 2016-04-26
        • 1970-01-01
        • 2018-12-07
        • 2016-08-16
        相关资源
        最近更新 更多