Geb: Waiting/sleeping between tests

时光怂恿深爱的人放手 提交于 2019-12-05 20:50:43

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.

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.

Thread.sleep(30000)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!