0

I am using Protractor-Jasmine-JavaScript.

In below code expect.toBe statement is executed as expected after clicking on both buttons.

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);
});

In below code expect.toBe statement is executed before clicking on second button and hence failing.

  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);
  });

I do not want to use browser.sleep(). Is there any way bowser.wait() can work? Or any other alternative solution?

user812142
  • 163
  • 2
  • 17
  • Is elem correct and you are testing for the correct element? It might be that you are testing one element to become clickable but then hitting another before that one is clickable – DanteTheSmith Dec 04 '18 at 15:02
  • @DanteTheSmith yes the element is correct. It is working fine with browser.sleep(3000). Bu what if the internet is slow and button loads after 3 seconds. I do not want more sleep time. – user812142 Dec 06 '18 at 10:04

1 Answers1

0

Try this:

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);
});

Note that elementToBeClickable() returns the element if it is clickable. So you want to wait for it to return the element before you click on it.

This question is similar to the one answered here: https://stackoverflow.com/a/38674105/10397500

Marie
  • 149
  • 9
  • I updated the code to put the click inside `.then()`. Try that – Marie Dec 05 '18 at 14:44
  • It is getting failed with this error: Failed: Wait timed out after 3007ms. I tried changing the wait time to 10000ms but it still fails with time out error. (element takes only 1 or 2 seconds to load) – user812142 Dec 06 '18 at 09:46
  • My mistake. The `browser.wait()` function only exits when it either returns `true` or times out. Try to add something in the `browser.wait()` function like "if elem is a WebElement, return true, else return false" – Marie Dec 06 '18 at 22:18
  • I couldn't find any solution to this – user812142 Dec 09 '18 at 04:36
  • I tried this browser.wait(function(){ elem = exCon.elementToBeClickable(element(by.xpath(approveOR.approveBtn2))); if(typeof elem === 'object') { return true; } else{ return false; } },10000).then(function(){ elem.click(); }); The issue here is exCon.elementToBeClickable() returns type 'function' always – user812142 Dec 13 '18 at 13:41
  • elementToBeClickable will return an IWebElement when the element is clickable. https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_Support_UI_ExpectedConditions_ElementToBeClickable.htm – Marie Dec 20 '18 at 17:31