量角器:等待量角器与页面同步时出错:“ angularJS可测试性和角度可测试性均未定义

Thu*_*che 5 selenium angularjs protractor e2e-testing

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

配置文件

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

规格文件

    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');

    });

})
Run Code Online (Sandbox Code Playgroud)

错误等待量角器与页面同步时发生错误:“ angularJS可测试性和角度可测试性均未定义。这可能是因为这是一个非角度页面,或者是因为您的测试涉及客户端导航,这可能会干扰量角器的自举。”

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

Ole*_*sii 6

您收到此错误是因为默认情况下,量角器已加载等待角度页面。如果使用非角度,则应添加await browser.waitForAngularEnabled(false);onPrepare块:

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

Run Code Online (Sandbox Code Playgroud)

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

     * 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.
Run Code Online (Sandbox Code Playgroud)

因此,正如您所看到的,这都是关于$http$timeout任务的。开发人员经常以不正确的方式使用它。

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

both angularJS testability and angular testability are undefined
Run Code Online (Sandbox Code Playgroud)

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