techgeek1985
techgeek1985

Reputation: 13

How to tackle when the status of an webelement changes from initial value "xxx" to changed value "yyy" after the page refresh with selenium wait?

This is what i have tried with no luck,
Not sure where i am going wrong?

   if (("xxx").equals(messageStatus)) {      
   FluentWait<WebDriver> wait = new FluentWait<WebDriver> (driver)            
                .withTimeout(90, TimeUnit.SECONDS)                  
                .pollingEvery(500, TimeUnit.MILLISECONDS)            
                .ignoring(NoSuchElementException.class);              

  wait.until(new Function<WebDriver, WebElement>() {      
            public WebElement apply(WebDriver webDriver) {            
           String messageStatus =
  MessageLogPage.messageStatus.getText();            
                if (messageStatus.equals("yyy")){              
                    MessageLogPage.receivedPresentation.click();            
                }      
                    return null;          
                 }                
        });    

Any help/suggestions are much appreciated.Thanks in advance!

Upvotes: 1

Views: 432

Answers (1)

timbre timbre
timbre timbre

Reputation: 13995

I think the problem could be that if your page truly refreshes, the old reference to MessageLogPage.messageStatus becomes invalid. I.e. it's not only that messageStatus text changes, but the element instance itself does too.

So I would change the algorithm to get the element every time before getting its text. Also you should not click inside wait, do it after:

  wait.until(new ExpectedCondition<WebElement>() {      
           @Override public WebElement apply(WebDriver webDriver) {            
               try {
                   WebElement element = webDriver.findElement(By.<...>); // however you can locate MessageLogPage.messageStatus
                   if(element.getText().equals("yyy")) {
                       return element;
                   }      
                }
                catch(Exception e) { } // keep going (retun null)
                return null;                
        });    

wait.until will call this function multiple times until return value is not null or timeout has expired, in which case it will throw an exception. So next line after wait.until, can be the click:

 wait.until(...); // above code
 MessageLogPage.receivedPresentation.click(); // we are here only if wait.until did not throw the exception

Although it could be that MessageLogPage.receivedPresentation may also be invalid if page refreshed.

You can even further simplify it by returning Boolean, since you don't really care about this element after you found it:

  wait.until(new ExpectedCondition<Boolean>() {      
           @Override public Boolean apply(WebDriver webDriver) {            
               try {
                   WebElement element = webDriver.findElement(By.<...>); // however you can locate MessageLogPage.messageStatus
                   return element.getText().equals("yyy");
               }
               catch(Exception e) { } // keep going (return false)
               return false;          
        });

Upvotes: 1

Related Questions