2

After submitting a POST request with a button click from a webpage, I want to wait for 60 seconds and click a link on that same page.

The wait for 60 seconds part, is that possible to implement with any keyword from robot selenium2library?

  • you can define directly 60 seconds wait in code. No need to press any keyword for wait. – Helping Hands Feb 24 '15 at 6:46
  • Thanks @HelpingHands. I did exactly what you said. There is no keyword in robot selenium 2 library I found, which helps me wait for a random time. So, I implemented a keyword 'Wait For timeout'. def wait_for(self, timeout): timeout = robot.utils.timestr_to_secs(timeout) maxtime = time.time() + timeout while True: if time.time() > maxtime: break time.sleep(0.2) – Imran Chowdhury Feb 24 '15 at 7:51
  • Great..cheers... – Helping Hands Feb 24 '15 at 7:52
  • Your title says "random time" but your question specifically mentions 60 seconds. Are you wanting a keyword that can wait a random amount of time (eg: "wait 0-60 seconds"), or one that can wait on a specific amount of time given by the test case? – Bryan Oakley Feb 24 '15 at 12:13
  • Why do you want to wait? Most of the time sleeping is not the solution during automated tests, better to wait for the effect to happen. Actions that have no visible effects... – Niels van Reijmersdal Feb 24 '15 at 12:15
3

Putting a sleep in a selenium test is generally not the best solution. Selenium has keywords to wait for some condition on a page, such as wait for condition, wait until element is visible, wait until page contains, and wait until page contains element. These are preferable, since the wait will usually be as short as possible. With a fixed-time wait, you may be waiting considerably longer than necessary, or you may not be waiting long enough.

However, since you asked specifically about sleeping, the robot framework BuiltIn library has a sleep keyword. The time format it uses is described in the robot framework users guide.

sleep | time | reason=none

Pauses the test executed for the given time.

time may be either a number or a time string. Time strings are in a format such as '1 day 2 hours 3 minutes 4 seconds 5milliseconds' or '1d 2h 3m 4s 5ms', and they are fully explained in an appendix of Robot Framework User Guide. Optional reason can be used to explain why sleeping is necessary. Both the time slept and the reason are logged.

1

You mention in the comments:

What happens in my application is, after POSTing something, it takes sometime to update everything(indexing involved), for that specific reason we need to wait sometime and refresh the page, otherwise if I use wait for condition/wait until element is visible/wait until page contains/wait until page contains element , I cannot refresh the page when any of those keywords wait for the expected element and hence it will definitely fail.

You can define a custom wait action that will do exactly what you want, something like:

    Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
                .withTimeout(60, TimeUnit.SECONDS);

    wait.until( new ExpectedCondition<Boolean>() { 
        @Override 
        public Boolean apply( WebDriver webDriver ) {
            driver.navigate().to(appURL);
            //Check for data here. Return true if the data is what you want it to be
        }
      } );

The advantage to doing this is that if it takes less than 60 seconds, you will exit early, making your test faster.

0

Following code should be added to robot selenium2 library in order to use this keyword: Wait For | timeout

def wait_for(self, timeout):
    timeout = robot.utils.timestr_to_secs(timeout)
    maxtime = time.time() + timeout
    while True:
        if time.time() > maxtime:
            break
        time.sleep(0.2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.