Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 454
isaac9A Avatar asked Nov 24 '15 19:11

isaac9A


3 Answers

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);
  });
like image 169
isaac9A Avatar answered Nov 15 '22 23:11

isaac9A


await driver.sleep(n * 1000)

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

like image 41
Badmaash Avatar answered Nov 15 '22 22:11

Badmaash


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');
})
like image 28
Mellissa Avatar answered Nov 15 '22 22:11

Mellissa