量角器无需等待即可进行下一个测试 [英] Protractor moves on to next test without waiting
问题描述
我正在使用量角器,并且在浏览器堆栈上运行测试时,我收到以下错误消息
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 it
s 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()
方面,在承诺解决方面还存在一些挑战,因为尚不清楚加载页面是否可以与ControlFlow同步,这一点尚不清楚.因此,browser.driver.get()
并不总是强迫量角器等待.
在此处和
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屋!