RamanaMuttana
RamanaMuttana

Reputation: 491

Is there a way to check if page was reloaded with WebDriver (java)?

I need to verify an element after every update.Ex: Lowrtemperature:7.54 after some time the value changes to Lowrtemperature:3.78 ,5.87,9.32 ...etchere the value is changing after some time(time varies) each time. but i need the driver to get the updated value afterthe change .how to get the element using selenium webdriver

WebElement PrecipitationLabel_14_L= (new WebDriverWait(driver, 30)).until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='webFor']/div/div[1]/div[1]/div/div[2]")));
String Precipationclass= PrecipitationLabel_14_L.getAttribute("class");
return Precipationclass;

Upvotes: 0

Views: 353

Answers (1)

Florent B.
Florent B.

Reputation: 42518

The purpose of your test should be to ensure that the text is updated. Whether the page is reloaded or not should be irrelevant here.

Here's an example waiting for the text to be updated:

WebDriverWait wait = new WebDriverWait(driver, 30);

String temperatureBefore = wait.until(textToBeDifferent(By.cssSelector(...), ""));
String temperatureAfter = wait.until(textToBeDifferent(By.cssSelector(...), temperatureBefore));

And the custom waiter:

public static ExpectedCondition<String> textToBeDifferent(final By locator, final String text) {
  return new ExpectedCondition<String>() {
    @Override
    public String apply(WebDriver driver) {
      try {
        String elemText = driver.findElement(locator).getText().trim();
        return elemText.equals(text) ? null : elemText;
      } catch (NoSuchElementException  e) {
        return null;
      } catch (StaleElementReferenceException e) {
        return null;
      }
    }

    @Override
    public String toString() {
      return String.format("text ('%s') to not be found by %s", text, locator);
    }
  };
}

Upvotes: 1

Related Questions