【问题标题】:Make request.post execute in sequence让request.post按顺序执行
【发布时间】:2017-07-20 03:44:30
【问题描述】:

我在Protractor-Jasmine 中为我的Angular2-TypeScript 应用程序编写了一个E2E 测试,如下所示,

it("Perform Some Action", function() {

    element(by.css('[ng-reflect-placeholder="Email"]')).sendKeys(test_email);
    element(by.css('[ng-reflect-placeholder="Password"]')).sendKeys("pass");
    element(by.css('[ng-reflect-placeholder="Confirm Password"]')).sendKeys("conf pass");
    element(by.buttonText("CONTINUE")).click();

    request.post(
      'myAPIEndPoint',
      { json: { emailaddress: test_email,user: "user1",code: "1234" } },
      function(error, response, body) {
        if (!error && response.statusCode == 200) {
          console.log(body)
          var info = JSON.parse(body);
          //Do something
        }
      }
    );

现在,我面临的问题是,request.post 在它上面的语句之前被调用并导致我的测试失败,因为来自 api post 调用的响应只有在上面的语句将被执行时才有价值通话前。

确保post-call 仅在上面的语句执行后才执行的正确方法是什么?

【问题讨论】:

    标签: angular typescript jasmine protractor e2e-testing


    【解决方案1】:

    出现问题是因为大多数量角器指令都是异步完成的。这同样适用于sendKeys()click()

    你需要做两件事:

    1. click() promise 被解析时执行请求(在.then() 方法中)
    2. 通知您的 Jasmine 异步工作已完成,它可以继续执行其他套件(使用 it('description', function(done){}) 结构

    您的 TC 如下所示:

    it("Perform Some Action", function(done) {
        element(by.css('[ng-reflect-placeholder="Email"]')).sendKeys(test_email);
        element(by.css('[ng-reflect-placeholder="Password"]')).sendKeys("pass");
        element(by.css('[ng-reflect-placeholder="Confirm Password"]')).sendKeys("conf pass");
        element(by.buttonText("CONTINUE")).click().then(()=> {
            request.post(
              'myAPIEndPoint',
              { json: { emailaddress: test_email,user: "user1",code: "1234" } },
              function(error, response, body) {
                if (!error && response.statusCode == 200) {
                  console.log(body)
                  var info = JSON.parse(body);
                  //Do something
                  done();
                }
              }
            );
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2021-03-03
      • 2011-02-07
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多