如何使用量角器向流量控制队列添加承诺?

时间:2021-08-21 01:21:57

In my test I am calling and outside library to seed data into our backend before running some ui tests using protractor.

在我的测试中,我在调用和外部库之前将数据播种到我们的后端,然后使用量角器运行一些ui测试。

'use strict'

var dataBuilder = require('data_builder.js');

describe('test', function () {
  var testData = {
    name: 'foo',
    title: 'bar',
    ...
  };

  beforeEach(function () {
    //create test data on the backend
    dataBuilder.create(testData).then(function (id) {
      testData.id = id.id;
    });
  });



  it('test something', function () {
    ...
  });

As such the promise returned by the dataBuilder isn't resolved before the it() actually finishes. How can I add the promise returned by the dataBuilder into webDriver's flow control?

因此,在it()实际完成之前,dataBuilder返回的promise未得到解决。如何将dataBuilder返回的promise添加到webDriver的流控制中?

4 个解决方案

#1


15  

Protractor exposes WebDriverJS promises on the protractor object so you could use either the flow.await method or create a new promise and use flow.execute.

量角器在量角器对象上公开WebDriverJS承诺,因此您可以使用flow.await方法或创建新的promise并使用flow.execute。

The former could be achieved something like:

前者可以实现如下:

flow = protractor.promise.controlFlow()

flow.await(dataBuilder.create(testData)).then( function(id) {
    testData.id = id.id;
})

And you can see an example of the latter in this blog post.

你可以在这篇博客文章中看到后者的一个例子。

This could be done in the it function itself or if this is common to all your tests consider placing it in the onPrepare function of your protractor config.

这可以在it函数本身中完成,或者如果这对所有测试都很常见,可以考虑将它放在量角器配置的onPrepare函数中。

#2


12  

In my protractor tests I add something into webDriver's flow control using the following pattern. If one creates and returns a promise from these "various statements", the promise will be correctly inserted into the control flow.

在我的量角器测试中,我使用以下模式在webDriver的流控制中添加了一些东西。如果从这些“各种语句”创建并返回一个promise,则promise将被正确地插入到控制流中。

browser.controlFlow().execute(function() {
    // various statements
});

#3


2  

In this specific case, you could use the done callback in the beforeEach like this:

在这种特定情况下,您可以在beforeEach中使用完成回调,如下所示:

beforeEach(function (done) {
   dataBuilder
      .create(testData)
      .then(function (id) {
         testData.id = id.id;
      })
      .finally(done);
});

Accepting a done callback parameter indicates that the setup is asynchronous.

接受完成回调参数表示设置是异步的。

#4


0  

I never remember the syntax, so I use a hack of sort that is easier to remember since it relies on the way promise behaves (so basically you can use this in every promise based system)

我从来没有记住语法,所以我使用了一种更容易记住的排序,因为它依赖于promise行为的方式(所以基本上你可以在每个基于promise的系统中使用它)

browser.sleep(1).then(()=> {
  return someAsyncPromiseAction();
})

Another thing you could do is simply rely on the action before it. So if you, for example, just clicked a button it will look like this

你可以做的另一件事就是依靠之前的行动。因此,例如,如果您只是单击一个按钮,它将如下所示

$(' ... ').click().then( () => { return someAsync(); })

While this doesn't add a promise to the flow control, you still get the same result.

虽然这不会为流量控制添加承诺,但您仍然可以获得相同的结果。

#1


15  

Protractor exposes WebDriverJS promises on the protractor object so you could use either the flow.await method or create a new promise and use flow.execute.

量角器在量角器对象上公开WebDriverJS承诺,因此您可以使用flow.await方法或创建新的promise并使用flow.execute。

The former could be achieved something like:

前者可以实现如下:

flow = protractor.promise.controlFlow()

flow.await(dataBuilder.create(testData)).then( function(id) {
    testData.id = id.id;
})

And you can see an example of the latter in this blog post.

你可以在这篇博客文章中看到后者的一个例子。

This could be done in the it function itself or if this is common to all your tests consider placing it in the onPrepare function of your protractor config.

这可以在it函数本身中完成,或者如果这对所有测试都很常见,可以考虑将它放在量角器配置的onPrepare函数中。

#2


12  

In my protractor tests I add something into webDriver's flow control using the following pattern. If one creates and returns a promise from these "various statements", the promise will be correctly inserted into the control flow.

在我的量角器测试中,我使用以下模式在webDriver的流控制中添加了一些东西。如果从这些“各种语句”创建并返回一个promise,则promise将被正确地插入到控制流中。

browser.controlFlow().execute(function() {
    // various statements
});

#3


2  

In this specific case, you could use the done callback in the beforeEach like this:

在这种特定情况下,您可以在beforeEach中使用完成回调,如下所示:

beforeEach(function (done) {
   dataBuilder
      .create(testData)
      .then(function (id) {
         testData.id = id.id;
      })
      .finally(done);
});

Accepting a done callback parameter indicates that the setup is asynchronous.

接受完成回调参数表示设置是异步的。

#4


0  

I never remember the syntax, so I use a hack of sort that is easier to remember since it relies on the way promise behaves (so basically you can use this in every promise based system)

我从来没有记住语法,所以我使用了一种更容易记住的排序,因为它依赖于promise行为的方式(所以基本上你可以在每个基于promise的系统中使用它)

browser.sleep(1).then(()=> {
  return someAsyncPromiseAction();
})

Another thing you could do is simply rely on the action before it. So if you, for example, just clicked a button it will look like this

你可以做的另一件事就是依靠之前的行动。因此,例如,如果您只是单击一个按钮,它将如下所示

$(' ... ').click().then( () => { return someAsync(); })

While this doesn't add a promise to the flow control, you still get the same result.

虽然这不会为流量控制添加承诺,但您仍然可以获得相同的结果。