量角器:量角器中的忽略同步和异步/等待有什么区别



我是Protractor的新手。我正在做测试以熟悉它。在这里,我遇到了一个无法区分ignoreSynchronization和async/await方法的问题。我有3个街区来测试它们。第一个是明确的量角器自己的异步功能。

it('without await and ignoreSynchronization', async function() {
await browser.waitForAngularEnabled(false);
await browser.driver.get('https://www.baidu.com');
console.log('1');
element(by.css('#kw')).sendKeys('protractor').then(() => {
console.log('2');
});
console.log('3');
console.log('4');
element(by.css('#su')).click().then(() => {
console.log('5');
})
console.log('6');
browser.driver.sleep(2000);
});

很明显,打印流程为1,3,4,6,2,5第二个是添加等待的第一个块

it('with await', async function() {
await browser.waitForAngularEnabled(false);
await browser.driver.get('https://www.baidu.com');
await console.log('1');
await element(by.css('#kw')).sendKeys('protractor').then(() => {
console.log('2');
});
await console.log('3');
await console.log('4');
await element(by.css('#su')).click().then(() => {
console.log('5');
})
await console.log('6');
browser.driver.sleep(2000);
});

该区块的打印流程为1,2,3,4,5,6。对于最后一个块,它是一个带有ignoreSynchronization方法的清晰版本

it('with ignoreSynchronization is True', async function() {
await browser.waitForAngularEnabled(false);
await browser.driver.get('https://www.baidu.com');
browser.ignoreSynchronization = true;
console.log('1');
element(by.css('#kw')).sendKeys('protractor').then(() => {
console.log('2');
});
console.log('3');
console.log('4');
element(by.css('#su')).click().then(() => {
console.log('5');
})
console.log('6');
browser.driver.sleep(2000);
});

这个块的结果和第一个块的结果一样吗?我不知道为什么。也许,我没有使用ignoreSynchronization作为一种正确的方式。但有人能解释一下这两种方法之间的区别吗?非常感谢

从量角器6.0.0开始,量角器中不存在ignoreSynchronization,而应该使用browser.waitForAngularEnabledhttps://github.com/angular/protractor/issues/4187

所以当你做browser.ignoreSynchronization = true时,它没有任何效果,实际上它不会做任何

你看到不同结果的原因是你没有处理承诺,它们以随机顺序解决。有两种处理方法:async/await.then()语法,但最后一种很长,可读性差,而且很复杂,这使得调试过程成为的噩梦

我认为第一个答案涵盖了其他

ignoreSynchronizationasync/ await彼此非常不同。

忽略同步:

此函数已被弃用,并被waitForAngularEnabled()函数所取代。

为什么需要它?

Protractor被广泛用于测试Angular网站。因此,当执行开始时,量角器会在测试中的应用程序中搜索角度组件。因此,如果我们正在测试角度应用程序,可以初始化

browser.waitForAngularEnabled(true)

也就是

browser.ignoreSynchronization = false

但如果有人想测试一个非角度网站,必须在执行过程中禁用搜索角度组件。因此使用以下设置

browser.waitForAngularEnabled(false)

也就是

browser.ignoreSynchronization = true

异步/等待

他们习惯于处理承诺。由于JavaScript是一种同步语言,在执行过程中调用的回调函数是异步的,promise用于处理这些函数

现在我将解释第二和第三程序的输出:

await console.log('1'); // 1 will be printed
await element(by.css('#kw')).sendKeys('protractor').then(() => {
console.log('2'); // as `await` keyword is used, execution will wait till promise is resolved and then 2 is printed
});
await console.log('3'); // print 3
await console.log('4'); // print 4
await element(by.css('#su')).click().then(() => {
console.log('5'); // again execution will wait till promise is resolved and 5 is printed
})
await console.log('6'); // print 6

因此op是1,2,3,4,5,6

用于第三代码

console.log('1'); // print 1
element(by.css('#kw')).sendKeys('protractor').then(() => {
console.log('2'); // this block will be handled by browser for execution and executed once stack is emppty
});
console.log('3'); // print 3
console.log('4'); // print 4
element(by.css('#su')).click().then(() => {
console.log('5'); // this block will be handled by browser for execution and executed once stack is empty, after printing 2
})
console.log('6'); // print 6. Now stack is empty and after printing 6, 2 will be printed

因此op是1,3,4,6,2,5

如果你想了解更多关于异步编程的信息,请查看JSConfEU 的Philip Roberts的视频

希望这将解决您的疑问:-(

最新更新