量角器:量角器中的ignoreSynchronization和async/await有什么区别 [英] Protractor: what's the difference between ignoreSynchronization and async/await in Protractor

查看:48
本文介绍了量角器:量角器中的ignoreSynchronization和async/await有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是量角器的新手.我正在做测试以熟悉它.在这里,我遇到了一个问题,我无法区分ignoreSynchronization和async/await方法.我有3个方块来测试它们.首先很清楚,它具有量角器自己的异步功能.

I am new in Protractor. And I am doing tests to be familiar with it. Here, I met a problem what I cannot distinguish between ignoreSynchronization and async/await method. I had 3 blocks to test them. The first is clear with protractor's own async features.

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 is clear that the print flow is 1,3,4,6,2,5 The second is the first block adding await

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方法的清晰版本

The print flow of this block is 1,2,3,4,5,6. For the last block, it is a clear version with ignoreSynchronization method

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用作正确的方法.但是谁能解释这两种方法之间的区别?非常感谢

The result of this block is the same as the first one? I don't know why. Perhaps, I did not use ignoreSynchronization as a correct way. But can anyone explain what's difference between these two methods? Many thanks

推荐答案

ignoreSynchronizationasync/ await彼此非常不同.

该功能已被弃用,并由waitForAngularEnabled()功能取代.

This function is already deprecated and replaced by waitForAngularEnabled() function.

为什么需要它?

量角器被广泛用于测试Angular网站.因此,当执行开始时,量角器将在被测试的应用程序中搜索角度分量. 因此,如果我们要测试角度应用程序,则可以初始化

Protractor is widely used to test Angular websites. So when execution starts, protractor search for angular components in the application under test. So if we are testing angular application, one can initialize

browser.waitForAngularEnabled(true)

这也意味着

browser.ignoreSynchronization = false

但是,如果有人想测试非角度网站,则必须在执行期间禁用搜索角度组件.因此,使用以下设置

But if anyone wants to test non-angular website, one must disable searching for angular components during execution. Hence below settings are used

browser.waitForAngularEnabled(false)

这也意味着

browser.ignoreSynchronization = true

它们用于处理承诺的内容.由于JavaScript是异步语言,因此在执行过程中会调用许多回调函数,并使用promise来处理这些函数

They are used to handle promised. As JavaScript is asynchronous language, many callback functions are called during execution and promise is used to handle these functions

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

Now I will explain the outputs on 2nd and 3rd programs:

  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

第3个代码

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撰写

If you want to learn more about asynchronous programming, do check out this video by Philip Roberts at JSConfEU

希望这可以解决您的查询:-)

Hope this will solve your query :-)

这篇关于量角器:量角器中的ignoreSynchronization和async/await有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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