WontonJon
WontonJon

Reputation: 413

What are the best practices for using waitFor{} and sleep() in geb automated testing?

Currently in the process of learning how to write automated tests using geb so this may be bit of a noob observation and question. Is it just me or when running many tests one after another seem to speed up the the execution of the tests? For example, when write a new test I'll comment out my other tests just to run a single method or two to make sure its working properly. Everything will run fine and pass. Then when I uncomment everything to run the full test, the test appears to run extremely fast and to the point where the web application I'm automating cant keep up and will cause my tests to fail due to elements not being loaded. Even when using waitFor{} blocks. i found that using sleep(1000) in certain places has helped but I feel as though there is probably a better way to approach this problem. The web application I'm working with seems to refresh the page a lot whenever the user does anything with a field which may be part of the problem that I don't really have control over. at one part in my test I need to fill out a form but the page refreshes after fill out out an input so I've written the code below that works but looks kind of meh because of all the sleep statements.

void populateRequiredFields(){
    def fName = "Test"
    def lName = "User"
    def email = "[email protected]"
    def question = "Do you even test bro?"

    clear.click()
    //sleep() to slow down test in order to get correct elements due to page refreshing
    sleep(3000)
    firstName << fName
    sleep(1000)
    lastName << lName
    sleep(1000)
    emailAddress << email
    sleep(1000)
    veryifyEmail << email
    sleep(1000)
    questionField << question
    sleep(1000)
}

Upvotes: 1

Views: 5880

Answers (3)

btb
btb

Reputation: 21

If you need to wait longer than the standard 10 seconds provided by waitFor, you can do this:

waitFor(30, 0.5){ firstName << fName }

This will wait for 30 seconds and check every half second.

Upvotes: 2

erdi
erdi

Reputation: 6954

I found that it's best to enclose logic that waits for the page to be in the expected state after a typically asynchronous action is performed inside of methods on page objects and modules. This way you don't litter your tests with all the waitFor {} noise and you won't forget to wait for things when you add more tests because waiting is part of the reusable logic.

So in your case:

import geb.Page

class APageThatCanBeCleared extends Page {

    static content = {
        clear { $(/*whatever the selector for the clearing element is*/) }
        firstName { $(/*whatever the selector for the first name element is*/) }
    }

    void clear() {
        clear.click()
        waitFor { firstName.displayed } 
    }
}

and then in your test:

to APageThatCanBeCleared
clear()
firstName << "Test"

Upvotes: 1

mariia
mariia

Reputation: 137

Using sleep() is not a good idea. Try to use waitFor {some results are present}

Upvotes: 0

Related Questions