【问题标题】:Protractor moves on to next test without waiting量角器无需等待即可继续下一个测试
【发布时间】:2017-10-14 01:07:12
【问题描述】:

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

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 ...

这里是导致错误的代码sn-p:

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 仍在运行(我猜是基于错误)。

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

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('/'),它会再次失败。

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

更新: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);
    }
};

【问题讨论】:

    标签: selenium-webdriver protractor browserstack


    【解决方案1】:

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

    在promise-resolving方面,围绕browser.get()browser.driver.get()还存在一些挑战,因为尚不清楚要加载的页面是否可以与ControlFlow同步。因此browser.driver.get() 并不总是迫使量角器等待。 阅读有关herehere 的更多信息

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

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

    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.
        });
        ...
    });
    

    【讨论】:

    • 感谢您指出这一点。不幸的是,我不能再检查这个了,因为我已经搬到了 Angular :)
    猜你喜欢
    • 2021-03-05
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    相关资源
    最近更新 更多