browser.sleep()vs browser.wait()量角器-茉莉-JavaScript

时间:2018-12-04 14:53:34

标签: javascript jasmine protractor

我正在使用Protractor-Jasmine-JavaScript。

在下面的代码中,单击两个按钮后,按预期执行了Expect.toBe语句。

element(by.xpath(button1_xpath)).click(); //click on button1
var elem=element(by.xpath(button2_xpath));
browser.sleep(3000)
elem.click(); //click on button2

element(by.xpath(receivedMsg_xpath)).getText().then(function(msg){        
  expect(msg).toBe(Expected_Msg);
});

在下面的代码中,expect.toBe语句在单击第二个按钮之前执行,因此失败。

  element(by.xpath(button1_xpath)).click();
  var elem=element(by.xpath(button2_xpath));
  var isClickable = exCon.elementToBeClickable(elem);
  browser.wait(isClickable,3000);
  elem.click();

  element(by.xpath(receivedMsg_xpath)).getText().then(function(msg){        
      expect(msg).toBe(Expected_Msg);
  });

我不想使用browser.sleep()。 bowser.wait()有什么办法可以工作?或任何其他替代解决方案?

1 个答案:

答案 0 :(得分:0)

尝试一下:

var elem;
element(by.xpath(button1_xpath)).click();
browser.wait(function(){
  elem = exCon.elementToBeClickable(element(by.xpath(button2_xpath)));
},3000).then(function(){
  elem.click();
});

element(by.xpath(receivedMsg_xpath)).getText().then(function(msg){        
  expect(msg).toBe(Expected_Msg);
});

请注意,elementToBeClickable()将返回可单击的元素。因此,您要等它返回元素后再单击它。

这个问题类似于在这里回答的问题:https://stackoverflow.com/a/38674105/10397500