在Protractor中,很少有代码行被间歇性执行

时间:2018-05-14 10:05:42

标签: typescript promise protractor

我从我的测试中调用一个函数,该函数调用API服务并返回一个值。我每次都可以从我的测试中打印变量的值。但是之后的代码行没有被执行。 这是间歇性发生的。

主要功能:

it('Test1', function () {
  element(by.xpath("xpath of the element1")).click();
  response = homepo.getValuefromAPI();
  response.then(function(value){
    console.log("**********value inside Main*****************");
    console.log((value)); 
    element(by.xpath("xpath of the element2")).sendKeys(value);
    browser.sleep(3000);
    element(by.xpath("xpath of button']")).click();
    expect(value).toBe("expected value");
  });
});

返回承诺的函数:

getValuefromAPI(){
var deffered = protractor.promise.defer();
var options = {
  method: 'POST',
  url: 'url',
  headers:
    {'Content-Type': 'application/json'},
  body:
    {json body},
};
request(options, function (error, response, body) {
  let responseData = JSON.stringify(body);
  console.log("**********responseData inside function*****************");
  console.log(responseData);
  console.log("*******************Id*****************");
  console.log(body.content[0].data.section.class.repository[0].period.points[0].id);
  deffered.fulfill(body.content[0].data.section.class.repository[0].period.points[0].id);
});
return deffered.promise;
}

输出:

**********responseData inside function*****************
ABCDFEGHIJKLMNOPQRSTUVWXYZ
*******************Id*****************
ABCDFEGHIJKLMNOPQRSTUVWXYZ
**********value inside Main*****************
ABCDFEGHIJKLMNOPQRSTUVWXYZ
x Test1
  - Expected 'ABCDFEGHIJKLMNOPQRSTUVWXYZ' to be 'expected value'.

' sendKeys'行动和点击'之后的动作(在主测试中)大多数时间被跳过,控件直接跳到“期望”状态。声明。有时这些语句会在没有任何问题的情况下执行。

我处理承诺的方式有问题吗?

更新:现在我也尝试在其中执行api调用'它'阻止而不是返回值。但是,仍然没有执行这些行。

it('Test1', function () {
var options = {
  method: 'POST',
  url: 'url',
  headers:
    { 
    'Content-Type': 'application/json'
    },
  body:
    { 
    json body
    },
};
request(options, function (error, response, body) {
    let responseData = JSON.stringify(body);
    console.log("**********responseData inside function*****************");
    console.log(responseData);
    console.log("*******************Id*****************");
    console.log(body.content[0].data.section.class.repository[0].period.points[0].id); 
    element(by.xpath("xpath of the element1")).click();
    console.log("**********value inside Main*****************");
    console.log((value)); 
    browser.sleep(2000);   //This line is not getitng executed
    element(by.xpath("xpath of the element2")).sendKeys(value);   //This line is not getitng executed
    browser.sleep(2000);   //This line is not getitng executed
    element(by.xpath("xpath of button']")).click();   //This line is not getitng executed
    browser.sleep(2000);   //This line is not getitng executed
    expect(value).toBe("expected value");
});
});

4 个答案:

答案 0 :(得分:0)

it('Test1', function () {
  element(by.xpath("xpath of the element1")).click();
  response = homepo.getValuefromAPI();
//it will only execute when the value is set
    if(!!response){
          response.then(function(value){
            element(by.xpath("xpath of the element2")).sendKeys(value);
            browser.sleep(3000);
            element(by.xpath("xpath of button']")).click();
            expect(value).toBe("expected value");
           });
    }
});

答案 1 :(得分:0)

您存储在"值"中的返回承诺的值不会改变。所以,即使在以下行动之后

element(by.xpath("xpath of the element2")).sendKeys(value);
browser.sleep(3000);
element(by.xpath("xpath of button']")).click();

它会保持不变。因此,放置expect(value).toBe("expected value");的位置并不重要 尝试将expect(value).toBe("expected value");放在承诺的开头:

response.then(function(value){
    expect(value).toBe("expected value");
    console.log("**********value inside Main*****************");
    console.log((value)); 
    element(by.xpath("xpath of the element2")).sendKeys(value);
    browser.sleep(3000);
    element(by.xpath("xpath of button']")).click();
  });

答案 2 :(得分:0)

好的,试试吧。不要使用API​​函数的返回,请尝试使用回调,如下所示:

getValuefromAPI(callback){
var deffered = protractor.promise.defer();
var options = {
  method: 'POST',
  url: 'url',
  headers:
    {'Content-Type': 'application/json'},
  body:
    {json body},
};
request(options, function (error, response, body) {
  let responseData = JSON.stringify(body);
  console.log("**********responseData inside function*****************");
  console.log(responseData);
  console.log("*******************Id*****************");
  console.log(body.content[0].data.section.class.repository[0].period.points[0].id);
  deffered.fulfill(body.content[0].data.section.class.repository[0].period.points[0].id);
});
callback (deffered.promise);
}

你的通话功能看起来像这样:

  it('Test1', function () {
          element(by.xpath("xpath of the element1")).click();
          homepo.getValuefromAPI(function(response){
            response.then(function(value){
                 console.log("**********value inside Main*****************");
                 console.log((value)); 
                 element(by.xpath("xpath of the element2")).sendKeys(value);
                 browser.sleep(3000);
                 element(by.xpath("xpath of button']")).click();
                expect(value).toBe("expected value");
               });
          });   
    });

答案 3 :(得分:0)

通过添加如下所述的wait和return语句来修复该问题:

getValuefromAPI(){
  var deffered = protractor.promise.defer();
  return browser.wait(function() {
    var options = {
    method: 'POST',
    url: 'url',
    headers:
      {'Content-Type': 'application/json'},
    body:
      {json body},
  };
  request(options, function (error, response, body) {
    let responseData = JSON.stringify(body);
    console.log("**********responseData inside function*****************");
    console.log(responseData);
    console.log("*******************Id*****************");
    var id = body.content[0].data.section.class.repository[0].period.points[0].id;
    console.log(id);  
    deffered.fulfill(id);
  });
  return deffered.promise;
  }, 10000).then(function(){
    return id;
});