【问题标题】:Protractor- Changing ControlFlow to async-await量角器 - 将 ControlFlow 更改为 async-await
【发布时间】:2019-06-17 15:21:00
【问题描述】:

以下是我们的规范文件,它可以在没有 async 关键字的情况下正常工作。

但是,当我们尝试将现有量角器框架从控制流更改为 async-await 时,它会在 describe 和 it block 中显示上述错误。即使我们尝试使用'async () =>'。真的很郁闷

【问题讨论】:

  • 我认为你不应该将async 关键字放在describe 函数中,只在每个it 块中使用它。并尝试切换到 ES6 箭头函数语法,如下所示:describe('Projects CRUD', () => {

标签: javascript asynchronous jasmine protractor


【解决方案1】:

更详细的解释是describe 块仅用于分组。 async 函数是“返回承诺链的语法糖”,describe 块通常不支持(如等待解决)返回承诺。
此外,最好将所有 require 声明放在 describe 块之外。我会重新组织该代码,如下所示,然后重试:

const projectsPage = require('../pages/Projects_Page.js');
const commonFunctions = require('../utils/CommonFunctions.js');

describe('Projects CRUD', () => {
    it('Rename Project - Duplicate name check', async () => {
        //your code here  
    });
});

【讨论】:

  • 如果我使用 async 关键字来阻止它,那么它会显示“无效的箭头函数参数”错误。
  • 然后尝试切换回正常的函数语法,但只在 it 块中保持异步
  • 您可能还想在调用it 块内的每个函数之前添加关键字await
【解决方案2】:

像Jasmine、Mocha这样的测试框架包含了describe、before、beforeEach等关键字。测试框架具有这些块的执行顺序的内置定义。 对于 describe 和 It block 的函数定义是

export const describe: {
    /**
    * Registers a new test suite.
    * @param name The suite name.
    * @param fn The suite function, or {@code undefined} to define a pending test suite.
    */
    (name: string, fn: Function): void;


 export const it: {
/**
 * Add a test to the current suite.
 * @param name The test name.
 * @param fn The test function, or {@code undefined} to define a pending test case.
 */
(name: string, fn: Function): void;

这意味着描述,它块有两个参数。名称和功能。在 describe 的情况下,您不需要使用 async/await,因为此函数在内部使用“return”。因此它将等待整个事情在描述块内完成。换句话说,如果您只是在 describe 块中编写 async,那么您将不得不在不需要的 describe 函数下编写 await。

在 It 块的情况下,您将不得不像 async function() {} 那样编写,因为您必须等待在 it 块下编写的步骤。请参考以下示例。

describe('angularjs homepage', function() {
  it('should greet the named user', async function() {
    await browser.get('http://www.angularjs.org');

    await element(by.model('yourName')).sendKeys('Julie');

    var greeting = element(by.binding('yourName'));

    expect(await greeting.getText()).toEqual('Hello Julie!');
  });

注意:如果函数没有返回 Promise,则不需要使用 await。这是没用的。在上面的代码 sn-p 中,元素之前没有等待。这意味着元素不返回承诺,它只返回ElementFinder的对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 2018-01-05
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多