3

Is there a way to wait a set amount of time between tests? I need a solution to compensate for server lag. When creating a record, it takes a little bit of time before the record is searchable in my environment.

In the following code example, how would I wait 30 seconds between the first test and the second test and have no wait time between second test and third test?

    class MySpec extends GebReportingSpec {
        // First Test
        def "should create a record named myRecord"() {
            given:
            to CreateRecordsPage

            when:
            name_field = "myRecord"

            and:
            saveButton.click()

            then:
            at IndexPage
        }

        // Second Test
        def "should find record named myRecord"() {
            given:
            to SearchPage

            when:
            search_query = "myRecord"

            and:
            searchButton.click()

            then:
            // haven't figured this part out yet, but would look for "myRecord" on the results page
        }

        // Third Test
        def "should delete the record named myRecord"() {
            // do the delete
        }
    }
Szuturon
  • 931
  • 4
  • 17
  • 28

3 Answers3

9

You probably don't want to wait a set amount of time - it will make your tests slow. You would ideally want to continue as soon as the record is added. You can use Geb's waitFor {} to poll for a condition to be fulfilled.

    // Second Test
    def "should find record named myRecord"() {
        when:
        to SearchPage

        then:
        waitFor(30) {
            search_query = "myRecord"
            searchButton.click()
            //verify that the record was found
        }
    }

This will poll every half a second for 30 seconds for the condition to be fulfilled passing as soon as it is and failing if it's still not fulfilled after 30 seconds.

To see what options you have for setting waiting time and interval have look at section on waiting in The Book of Geb. You might also want to check out the section on implicit assertions in waitFor blocks.

If your second feature method depends on success of the first one then you should probably consider annotating this specification with @Stepwise.

erdi
  • 6,944
  • 18
  • 28
  • is there anyway to add delays? I need to add a delay while waiting some background downloading to complete, but cant find any way to do it. – Giannis Jun 12 '14 at 14:40
  • What do you mean by delays? Are you talking about something like `Thread.sleep()`? That's never a good idea because of the things I mentioned above. – erdi Jun 12 '14 at 22:31
4

You should always try to use waitFor and check conditions wherever possible. However if you find there isn't a specific element you can check for, or any other condition to check, you can use this to wait for a specified amount of time:

def sleepForNSeconds(int n) {
    def originalMilliseconds = System.currentTimeMillis()
    waitFor(n + 1, 0.5) {
        (System.currentTimeMillis() - originalMilliseconds) > (n * 1000) 
    }
}

I had to use this while waiting for some chart library animations to complete before capturing a screenshot in a report.

mag382
  • 736
  • 1
  • 6
  • 12
  • 1
    No offense, but this is really just a convoluted way to call `Thread.sleep`. https://github.com/geb/geb/blob/master/module/geb-waiting/src/main/groovy/geb/waiting/Wait.groovy#L148 – Thomas Hirsch Nov 24 '17 at 16:06
2
Thread.sleep(30000)

also does the trick. Of course still agree to "use waitFor whenever possible".

Thomas Hirsch
  • 2,102
  • 3
  • 19
  • 39
  • When you're testing things that are built to measure the actual passage of time, this may be the best option. – jonnybot Nov 02 '16 at 18:51