javascript - mocha assertions passed before page loads -


the below mocha , webdriverio script passing assertions before page loads.

i not understanding how passing when elements not yet present. after page loads, see element not clicked. fake pass? how fix in code?

var webdriverio = require('webdriverio'); var should = require('chai').should() var expect = require('chai').expect() var options = {     desiredcapabilities: {         browsername: 'chrome'     } };  before(function() {      browser=webdriverio.remote(options)     return browser.init()    });   describe('sauce labs page test', function() {     it('should assert page title', function(done) {                browser.url('https://docs.saucelabs.com/reference/platforms-configurator/?_ga=1.5883444.608313.1428365147#/');            browser.gettitle().then(function(title){           title.should.equal('platform configurator');            });             done();     });        it('should assert sub heading', function(done) {      browser.gettext('h3').then(function(text) {     text.should.equal('api');     console.log(text);    });    done(); });      it('should click on selenium', function(done) {      browser.click('#main-content > div > ng-view > div > div:nth-child(1) > div.choice-buttons.choice-api > div:nth-child(2)')      done(); });    }); 

you should call done callback inside .then() handler, otherwise it's called before browser has had chance load page:

it('should assert page title', function(done) {   browser.url('https://docs.saucelabs.com/reference/platforms-configurator/?_ga=1.5883444.608313.1428365147#/');   browser.gettitle().then(function(title) {     title.should.equal('platform configurator');     done();   }); }); 

however, introduces problem: if assertion fails, done never called (because exception thrown before it).

instead, can use fact mocha has built-in promises support. instead of using done callback, return promise , mocha handle (and exceptions) properly:

it('should assert page title', function() {   browser.url('https://docs.saucelabs.com/reference/platforms-configurator/?_ga=1.5883444.608313.1428365147#/');   return browser.gettitle().then(function(title) {     title.should.equal('platform configurator');   }); }); 

Comments