3

I want to wait for the error message comes out. However, when I use browser.driver.manage().timeouts().implicitlyWait(), but I have to use browser.driver.sleep()

this.getErrorMessage = function () {
    var defer = protractor.promise.defer();
    browser.driver.sleep(2000); //This works
    browser.driver.manage().timeouts().implicitlyWait(2000); // This does not work
    browser.driver.findElement(By.xpath(_error_msg_xpath)).getText().then(function (errorMsg) {
        defer.fulfill(errorMsg);
    });
    return defer.promise;
};
isian8814
  • 191
  • 10

2 Answers2

0

Updated

Xpath is a slower location strategy. Use an explicit wait and maybe bump to 3 seconds.

var aElement
try {
  aElement = driver.wait(until.elementLocated(By.xpath(_error_msg_xpath)), 3000);
  aElement.getText().then(function (errorMsg) {
      defer.fulfill(errorMsg);
  });
catch (err) {
  message.innerHTML = "Error: " + err + ".";
}

Or something like that. Set the wait time (AKA 3000 above) to how long you want to wait for the error. If the element is found first you exit the until.

MikeJRamsey56
  • 2,779
  • 1
  • 20
  • 34
  • Browse [here](https://github.com/SeleniumHQ/selenium/blob/c10e8a955883f004452cdde18096d70738397788/javascript/webdriver/webdriver.js). – MikeJRamsey56 Apr 20 '16 at 20:27
  • The message is showing only when there is error. What I am trying to do is 1. Wait for specific time 2. If no error shows, then pass 3. If there is error, then display (Try to catch if errors happen, during valid scenarios) – isian8814 Apr 20 '16 at 20:56
0

From what I understand, you need a browser.wait() in this case:

this.getErrorMessage = function () {
    var EC = protractor.ExpectedConditions;
    var elm = element(by.xpath(_error_msg_xpath));

    browser.wait(EC.presenceOf(elm), 2000);
    return elm.getText();
};

This would wait for the presence of the element up to 2 seconds returning a promise with a text of the element in case the element is found and you would get a Timeout Error in case the element would not become present in 2 seconds.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195