isaac9A
isaac9A

Reputation: 903

WebDriver wait n seconds

I am writing some functional tests with Sauce Labs (Using Selenium + Webdriver + Nodejs). One of my test cases looks like the following:

  it('Should Not Have Any Errors', function(done) {
        browser
            .get(host + '/test.live.cgi?view=pixelTest')
            .elementById('errorHolder')
            .text()
            .should.eventually.equal('[]')
            .nodeify(done);
   });

How would I go about waiting 10 seconds between loading the page and checking the errorHolder element's text? I have been scanning through the api https://github.com/admc/wd/blob/master/doc/api.md but all of the wait functions look like they require an asserter function to test whether a given condition is true. I am trying to use waitFor(opts, cb) -> cb(err) method but I am unsure of how to chain it with the promises. Can I do this?

Upvotes: 14

Views: 22882

Answers (4)

Danny Leung
Danny Leung

Reputation: 1

You can try this:

browser.pause(10000)

Upvotes: 0

Badmaash
Badmaash

Reputation: 795

await driver.sleep(n * 1000)

The above code works for me. Make sure that this code is inside an async function.

Upvotes: 14

isaac9A
isaac9A

Reputation: 903

Found the answer in the sleep function provided by webdriver. Now the code looks like this:

it('Should Not Have Any Errors', function(done) {
        browser
            .get(host + '/test.live.cgi?view=pixelTest')
            .sleep(10000)
            .elementById('errorHolder')
            .text()
            .should.eventually.equal('[]')
            .nodeify(done);
  });

Upvotes: 15

Mellissa
Mellissa

Reputation: 138

You can use:

.delay(10)

If you really have to use a delay though, consider defining it as a variable. You will have more control over delays throughout your code and it will be easier to search for if you need to refactor.

Edit to add:

you can also use:

.then(function () {
    browser.waitForElementById('errorHolder');
})

Upvotes: 3

Related Questions