Fix initial test script to wait for app to load

This commit is contained in:
Brett Sun 2016-01-25 17:32:04 +01:00
parent f246005d9c
commit bdc13473a1
2 changed files with 54 additions and 16 deletions

View File

@ -168,20 +168,19 @@ describe('Login logs users in', function() {
let browser;
```
Create the driver to control the browser.
Create the driver to control the browser. `before` will be executed once at the
start of the test before any `it` functions. Use `beforeEach` instead if you'd
like to run some code before each `it` function.
```javascript
before(function() {
browser = wd.promiseChainRemote('ondemand.saucelabs.com', 80);
return browser.init({ browserName: 'chrome' });
});
```
This function will be executed before each `it` function. Here we point the
browser to a specific URL.
```javascript
beforeEach(function() {
return browser.get('http://www.ascribe.ninja/app/login');
// Start the browser, go to /login, and wait for the react app to render
return browser
.init({ browserName, version, platform })
.get(config.APP_URL + '/login')
.waitForElementByCss('.ascribe-default-app', asserters.isDisplayed, 10000);
});
```
@ -208,6 +207,38 @@ without writing new functions.
});
```
All together:
```javascript
function testSuite(browserName, version, platform) {
describe(`[${browserName} ${version} ${platform}] Login logs users in`, function() {
// Set timeout to zero so Mocha won't time out.
this.timeout(0);
let browser;
before(function() {
// No need to inject `username` or `access_key`, by default the constructor
// looks up the values in `process.env.SAUCE_USERNAME` and `process.env.SAUCE_ACCESS_KEY`
browser = wd.promiseChainRemote('ondemand.saucelabs.com', 80);
// Start the browser, go to /login, and wait for the react app to render
return browser
.init({ browserName, version, platform })
.get(config.APP_URL + '/login')
.waitForElementByCss('.ascribe-default-app', asserters.isDisplayed, 10000);
});
after(function() {
return browser.quit();
});
it('should contain "Log in" in the title', function() {
return browser.title().should.become('Log in');
});
});
}
```
## How to run the test suite
To run the tests, type:
```bash

View File

@ -1,14 +1,16 @@
'use strict';
const Q = require('q');
const wd = require('wd');
const asserters = wd.asserters; // Commonly used asserters for async waits in the browser
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const config = require('./config.js');
chai.use(chaiAsPromised);
chai.should();
function testSuite(browserName, version, platform) {
describe(`[${browserName} ${version} ${platform}] Login logs users in`, function() {
// Set timeout to zero so Mocha won't time out.
@ -19,11 +21,17 @@ function testSuite(browserName, version, platform) {
// No need to inject `username` or `access_key`, by default the constructor
// looks up the values in `process.env.SAUCE_USERNAME` and `process.env.SAUCE_ACCESS_KEY`
browser = wd.promiseChainRemote('ondemand.saucelabs.com', 80);
return browser.init({ browserName, version, platform });
});
beforeEach(function() {
return browser.get(config.APP_URL + '/login');
// Start the browser, go to /login, and wait for the react app to render
return browser
.init({ browserName, version, platform })
.get(config.APP_URL + '/login')
.waitForElementByCss('.ascribe-default-app', asserters.isDisplayed, 1000)
.catch(function (err) {
console.log('Failure -- unable to load app.');
console.log('Skipping tests for this browser...');
return Q.reject(err);
});
});
after(function() {
@ -33,7 +41,6 @@ function testSuite(browserName, version, platform) {
it('should contain "Log in" in the title', function() {
return browser.title().should.become('Log in');
});
});
}