Helping Hands
Helping Hands

Reputation: 5396

How to wait in selenium for long time effectively

I am automating once test case and steps are :

  1. Go to website
  2. Go to Report Page
  3. Fill information form to request report
  4. Download report once it display on page

Here main thing is after filling form to request report , Report available to download in same page after 25 to 30 minutes of request.

So is there any better way to wait for 30 minutes until here my report available to download?

After submit request , I am thinking to put logic like :

do{

   //click somewhere on page constantly where nothing happens but just to be active

}while(reportelement.size!=1);

And as soon as I get report size > 0 , I will click on download link.

I know that selenium provides explicit wait but bit confuse about how that can be implemented here.

I am not looking for whole scenario code, just a good logic would help me a lot to automate this wait stuff.

Upvotes: 0

Views: 2784

Answers (2)

Tarun Lalwani
Tarun Lalwani

Reputation: 146510

In case you can't split the test I would suggest to keep it simple like below

int timeTaken = 0;
int TIMEOUT = 30 * 60;

do {
    Thread.Sleep(1000);
    timeTaken = timeTaken + 1;
    reportelement = driver.findElements(...);
} while (timeTaken < TIMEOUT && reportelement.size != 1)

FluentWait is good to not waste time when the element is available and avoid hard coded waits. Since here we are anyways expecting a 30 min kind of delay, so and extra min wasted to identify the element won't matter. But the code is quite simplified for the use case.

Since you are doing findElements, you don't need to take any other actions on the page as such, the connection will still be active with the driver

Upvotes: 2

df778899
df778899

Reputation: 10931

Probably the cleanest way to do this using the FluentWait approach would be a custom Sleeper. This can do what you like between the checks.

Sleeper is one of the parameters on the full constructor to WebDriverWait, e.g. :

    Sleeper sleeper = duration -> {
        // Click somewhere
        Sleeper.SYSTEM_SLEEPER.sleep(duration);
    };

    long longTimeout = 1800_000;
    FluentWait<WebDriver> wait = new WebDriverWait(driver,
            new SystemClock(), sleeper, longTimeout,
            WebDriverWait.DEFAULT_SLEEP_TIMEOUT)
                    .ignoring(StaleElementReferenceException.class);

    By by = By.id(id); // (As appropriate)

    wait.until(ExpectedConditions.numberOfElementsToBe(by, 1));

- the other constructor parameters there are just the normal defaults. Probably a longer sleepTimeOut would make sense here too.

Upvotes: 0

Related Questions