Home > Back-end >  Selenium Automation - How to click on particular button periodically until getting another element&#
Selenium Automation - How to click on particular button periodically until getting another element&#

Time:02-09

In my webpage, I have a refresh button and one Text Field, when i landed to that page, Initially that Text Field value would be processing (In backend there is a functionality which is processing, Inorder to inform the user, textfield value is processing in UI), and after functionality is done, that Text Field would be completed

Now coming to the question, We will get to know the updated value of Text Field only when we click on the refresh button,

Is there a way to make a WebElement to be waited until that Text field has value as completed? We also need to click on that particular refresh button periodically to check that text field value became completed or not

I've found a method called textToBePresentInElement in ExpectedConditions, But using this, we cannot refresh button periodically, Any other solution selenium webdriver is providing?

CodePudding user response:

You need to write a custom method to wait for sometime then to perform click operation. You can see below sample code,

Code:

Create a re-usable method and write down the logic inside the method for click operation and element value check on given explicit interval times.

public static void clickUntilStatusIsChanged(By element1, By element2, String expectedStatus, int timeOutSeconds) {
    WebDriverWait wait = new WebDriverWait(driver, 5);

    /* The purpose of this loop is to wait for maximum of 50 seconds */
    for (int i = 0; i < timeOutSeconds / 10; i  ) {
        if (wait.until(ExpectedConditions.textToBePresentInElementLocated(element2, expectedStatus))) {
            break;
        } else {
            /* Waits for 10 seconds and performs click operation */
            waitForTime(timeOutSeconds / 5);
            driver.findElement(element1).click();
        }
    }
}

public static void waitForTime(int interval) {
    int waitTillSeconds = interval;
    long waitTillTime = Instant.now().getEpochSecond()   waitTillSeconds;
    while (Instant.now().getEpochSecond() < waitTillTime) {
        // Do nothing...
    }
}

Pass the parameters to the re-usable method:

clickUntilStatusIsChanged(By.xpath("Element#1"), By.xpath("Element#2"), "completed", 50);

CodePudding user response:

It's possible to implement a custom Expected condition:

import org.openqa.selenium.support.ui.ExpectedCondition

public static ExpectedCondition<Boolean> textToBePresentForElementWithRefreshByClick(By originalElement, Strint expectedText, By refreshElement) {
    return new ExpectedCondition<Boolean>() {
        private boolean hasText = false;
        private String currentText = "";

        @Override
        public Boolean apply(WebDriver driver) {
            currentText = driver.findElement(originalElement).getText();
            hasText = currentText == expectedText;
            if(!hasText) {
                driver.findElement(refreshElement).click();
                // Optionally some sleep might be added here, like Thread.sleep(1000);
            }
            return hasText;
        }

        @Override
        public String toString() {
            return String.format("element \"%s\" should have text \"%s\", found text: \"%s\"", originalElement.toString(), expectedText, currentText);
        }
    };
}

Usage:

By originalElement = ...;
By refreshElement = ...;

new WebDriverWait(driver, Duration.ofSeconds(10)).until(
    textToBePresentForElementWithRefreshByClick(originalElement, "completed", refreshElement)
)
  •  Tags:  
  • Related