Created
August 31, 2017 18:43
-
-
Save isaaclyman/1c6a70237ca83950da945bcd85771f69 to your computer and use it in GitHub Desktop.
A Protractor setup with some nice goodies (start maximized, watch console errors, take screenshots)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Imagine there's a page helper that provides access to actual page elements in a unified place | |
var loginPage = require('./Helpers/Login.helper.js'); | |
var fs = require('fs'); | |
exports.config = { | |
specs: ['**/*.spec.js'], | |
capabilities: { | |
browserName: 'chrome', | |
chromeOptions: { | |
args: [ | |
'--start-maximized', | |
'--no-sandbox' | |
] | |
} | |
}, | |
allScriptsTimeout: 30000, | |
getPageTimeout: 30000, | |
jasmineNodeOpts: { | |
defaultTimeoutInterval: 30000 | |
}, | |
directConnect: true, | |
params: { | |
environment: 'local', | |
liveUrl: '', | |
client: '' | |
}, | |
onPrepare: function () { | |
setupReporters(); | |
var baseUrl = getBaseUrl(browser.params.environment, browser.params.client); | |
browser.get(baseUrl); | |
var page = new loginPage(); | |
page.setUsername('user'); | |
page.setPassword('password'); | |
page.logIn(); | |
} | |
}; | |
function getBaseUrl(environment, client) { | |
return 'http://example.com'; | |
} | |
function setupReporters() { | |
var ignoredErrors = ['materialdesignicons', 'Mixed Content', 'Failed to load resource']; | |
// Check for console errors after every test, and fail if there are any | |
afterEach(function () { | |
browser.manage().logs().get('browser').then(function (browserLog) { | |
if (browserLog.length) { | |
console.error('[browser errors:] ' + require('util').inspect(browserLog)); | |
} | |
expect(browserLog.filter(function (log) { | |
return ignoredErrors.every(function(err) { | |
return !~log.message.indexOf(err); | |
}); | |
}).length).toEqual(0); | |
}); | |
}); | |
// If there is a failure of any kind, take a screenshot and save it | |
jasmine.getEnv().addReporter(new function() { | |
this.specDone = function (result) { | |
if (result.failedExpectations.length > 0) { | |
browser.takeScreenshot().then(function (png) { | |
var stream = fs.createWriteStream('screenshot-' + Date.now() + '.png'); | |
stream.write(new Buffer(png, 'base64')); | |
stream.end(); | |
}); | |
} | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment