量角器无需等待即可继续下一个测试 [英] Protractor moves on to next test without waiting

查看:19
本文介绍了量角器无需等待即可继续下一个测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用量角器,当我在 browserstack 上运行测试时,我收到以下错误

I'm using protractor and when I run my tests on browserstack I receive the following error

StaleElementReferenceError: stale element reference: element is not attached to the page document

或取决于我在 beforeAll

Error: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator By.cssSelector ...

这是导致错误的代码片段:

Here is the code snippet causing the error:

describe('...', () => {
   it('...', () => {
       expect(element.all(by.css(...)).count()).toBe(9);
       expect(element.all(by.css('.items').get(0).isDisplayed()).toBeTruthy();
   });
}

describe('', () => {
    beforeAll((/* done */) => {
         element(by.css('.go-home').click(); // .then(done); 
         //browser.driver.get('/');//.then(done);
    });
    ...
});

由于某种原因, beforeAll 继续并更改了 url,而之前的 it 仍在运行(我猜是基于错误).

For some reason the beforeAll continues and changes the url, while the previous it is still running (I guess based on the error).

现在,我设法破解了这个方法.我已将 done 添加到 it 中,如下所示

Now, I managed to hack this such that is works. I've added the done to the it as follows

describe('...', () => {
   it('...', (done) => {
       expect(element.all(by.css(...).count()).toBe(9);

       element.all(by.css(...)).get(0).isDisplayed().then((state) => {
          expect(state).toBeTruthy();
          done();
    });
   });
}

describe('', () => {
    beforeAll(() => {
         element(by.css('.go-home').click(); // works 
         //browser.driver.get('/');          // still fails
    });
    ...
});

现在可以了.但是,如果我使用 browser.driver.get('/') 它会再次失败.

Now it works. However if I use browser.driver.get('/') it fails again.

通常我不必将 done 添加到我的 it 中,所以我的问题是:这里出了什么问题?任何帮助将不胜感激

Normally I don't have to add done to my its so my question is: What is going wrong here ? Any help would be appreciated

更新:protractor.config.js:

UPDATE: protractor.config.js:

exports.config = {
    chromeDriver: '../node_modules/protra...medriver_2.25',
    seleniumServerJar: '../node_...-server-standalone-2.53.1.jar',
    exclude: [],

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

    multiCapabilities: [
        {
            build: 'test',
            project: 'ABC',
            browserName: 'firefox',
            //browserName: 'chrome',
            os: 'Windows',
            os_version: '10',
            directConnect: true
        }],
    debug: true,
    maxSessions: 1,
    framework: 'jasmine2',

    onPrepare: function () {
        browser.driver.manage().window().setSize(1024, 768);

        // Register helpers
        require('../test/framework/jasmine2');

        var disableNgAnimate = function () {
            angular
                .module('disableNgAnimate', [])
                .run(['$animate', function ($animate) {
                    $animate.enabled(false);
                }]);
            };

        var disableCssAnimate = function () {
            angular
                .module('disableCssAnimate', [])
                .run(function () {
                    var style = document.createElement('style');
                    style.type = 'text/css';
                    style.innerHTML = '* {' +
                        '-webkit-transition: none !important;' +
                        '-moz-transition: none !important' +
                        '-o-transition: none !important' +
                        '-ms-transition: none !important' +
                        'transition: none !important' +
                        '}';
                    document.getElementsByTagName('head')[0].appendChild(style);
            });
        };

        browser.addMockModule('disableNgAnimate', disableNgAnimate);
        browser.addMockModule('disableCssAnimate', disableCssAnimate);
    }
};

推荐答案

按照我对 then() 功能的理解,它启动了一个异步任务.因此,只要输入 then()then() 函数之外的任何代码行都会继续.阅读更多关于这里这里这里.

The way I understand the then()-functionality, it starts an async task. Therefore any line of code outside the then()-function will be continued as soon as then() is entered. Read more about here, here and here.

browser.get()browser.driver.get() 在 promise-resolving 方面还存在一些挑战,因为目前尚不清楚页面是否加载可以与 ControlFlow 同步.因此 browser.driver.get() 并不总是强制 Protractor 等待.阅读更多关于这里这里

Further exist several challenges around browser.get() and browser.driver.get() in terms of promise-resolving, as it's not clear, whether the page to load can be synchronized with the ControlFlow. Therefore a browser.driver.get() isn't always forcing Protractor to wait. Read more about here and here

您的测试现在以某种方式结合了这两个问题.

Your test now combines these two issues in a way.

我建议在您的解决方案中尝试 browser.waitForAngular(); 以触发量角器实际等待直到所有承诺都得到解决:

I suggest to try browser.waitForAngular(); in your solution to trigger protractor to actually wait until all promises are resolved:

describe('...', () => {
   it('...', () => {
       expect(element.all(by.css(...).count()).toBe(9);

       element.all(by.css(...)).get(0).isDisplayed().then((state) => {
          expect(state).toBeTruthy();
       });
       //instead of using done to explicitly announce the "function finished"
       //use browser.waitForAngular() to let Protractor wait for any promises to be resolved.
       browser.waitForAngular(); 
   });
}

describe('', () => {
    beforeAll(() => {
         // .click() returns a promise, so Protractor waits for it to be resolved
         //element(by.css('.go-home').click();  
         browser.driver.get('/');   // doesn't return a promise
         browser.waitForAngular();  // triggers Protractor to wait for $http and promises resolved.
    });
    ...
});

这篇关于量角器无需等待即可继续下一个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆