Skip to content

Instantly share code, notes, and snippets.

@erquhart
Last active August 29, 2015 14:08
Show Gist options
  • Save erquhart/8993b9fff5041fe55c1a to your computer and use it in GitHub Desktop.
Save erquhart/8993b9fff5041fe55c1a to your computer and use it in GitHub Desktop.
Galen Framework initialization
var GRID_URL = 'http://localhost:4444/wd/hub';
var GRID_URL_SAUCELABS = 'http://[email protected]:80/wd/hub';
var GRID_URL_BROWSERSTACK = 'http://[email protected]/wd/hub';
var domains = {
local: 'obfuscated-url',
dev: 'obfuscated-url',
prod: 'obfuscated-url'
};
var browsers = {
local: {
chrome: {
grid: null,
name: 'chrome',
key: 'localChrome',
browserTitle: 'Chrome (Local)'
},
firefox: {
grid: null,
name: 'firefox',
key: 'localFirefox',
browserTitle: 'Firefox (Local)'
},
ie: {
grid: null,
name: 'ie',
key: 'localIE',
browserTitle: 'IE (Local)'
}
},
vm: {
ie8: {
grid: {
url: GRID_URL,
platform: 'VISTA',
browser: 'internet explorer'
},
key: 'vmIE8',
browserTitle: 'IE8 / Windows 7 (Local VM)'
},
},
sauceLabs: {
ie11: {
grid: {
url: GRID_URL_SAUCELABS,
browser: 'internet explorer',
desiredCapabilities: {
platform: 'Windows 8.1',
version: '11'
}
},
key: 'sauceLabsWin8IE11',
browserTitle: 'IE11 / Win8 (SauceLabs)'
},
chrome: {
grid: {
url: GRID_URL_SAUCELABS,
browser: 'chrome',
desiredCapabilities: {
platform: 'Windows 8.1',
version: '38'
}
},
key: 'sauceLabsWin8Chrome',
browserTitle: 'Chrome 38 / Win8 (SauceLabs)'
},
ios7: {
noResize: true,
grid: {
url: GRID_URL_SAUCELABS,
browser: 'iphone',
desiredCapabilities: {
platform: 'OS X 10.9',
version: '7.1'
}
},
key: 'sauceLabsIOS7Safari',
browserTitle: 'Safari 7.1 / iOS 7 (SauceLabs)'
}
},
browserStack: {
ie11: {
grid: {
url: GRID_URL_BROWSERSTACK,
desiredCapabilities: {
os: 'Windows',
os_version: '8.1',
browser: 'IE',
browser_version: '11',
'browserstack.local': 'true'
}
},
key: 'browserStackWin8IE11',
browserTitle: 'IE11 / Win8 (BrowserStack)'
},
chrome: {
grid: {
url: GRID_URL_BROWSERSTACK,
desiredCapabilities: {
os: 'Windows',
os_version: '8.1',
browser: 'chrome',
browser_version: '36',
'browserstack.local': 'true'
}
},
key: 'browserStackWin8Chrome',
browserTitle: 'Chrome 38 / Win8 (BrowserStack)'
},
iphone5s: {
noResize: true,
grid: {
url: GRID_URL_BROWSERSTACK,
desiredCapabilities: {
device: 'iPhone 5S',
'browserstack.local': 'true'
}
},
key: 'browserStackIphone5s',
browserTitle: 'iPhone 5s / Safari 7 (BrowserStack)'
}
}
};
var layouts = {
mobile: {
layoutName: 'Mobile',
size: '320x600',
tags: ['mobile'],
key: 'layoutsMobile'
},
tablet: {
layoutName: 'Tablet',
size: '768x800',
tags: ['tablet'],
key: 'layoutsTablet'
},
desktop: {
layoutName: 'Desktop',
size: '1200x800',
tags: ['desktop'],
key: 'layoutsDesktop'
}
};
var users = {
local: {
username: 'obfuscated',
password: 'obfuscated'
},
engprod: {
username: 'obfuscated',
password: 'obfuscated'
}
};
function openDriver(size, browser) {
var driver;
var gridUrl;
// don't attempt to resize browsers that aren't resizeable,
// namely for mobile devices where browsers aren't windowed
if (browser.noResize) {
size = null;
}
if (browser.grid) {
// for drivers that utilize the Selenium Grid
if (browser.grid.hasOwnProperty('url')) {
gridUrl = browser.grid.url;
}
else {
gridUrl = GRID_URL;
}
browser.grid.size = size;
driver = createGridDriver(gridUrl, browser.grid, size);
}
else {
// for drivers that utilize a local browser
driver = createDriver(null, size, browser.name);
}
session.put('driver', driver);
return driver;
}
afterTest(function(test) {
var driver = session.get('driver');
if (driver != null) {
if (test.isFailed()) {
session.report().info('Screenshot').withAttachment('Screenshot', takeScreenshot(driver));
}
console.log('about to quit');
// close the browser instance at test completion
// driver.quit();
}
});
function _test(testNamePrefix, callback) {
test(testNamePrefix + ' - ${browserTitle} - ${layoutName}', function (browser, layout) {
console.log('running _test');
var size;
if (layout) {
size = layout.size;
}
var driver = openDriver(size, browser);
callback.call(this, driver, layout);
});
}
function runTest(testNamePrefix, browserSelection, layoutSelection, callback) {
var browserSelectionPrepped = {};
var layoutSelectionPrepped = {};
var isNonEmptyObjectLiteral = function(arg) {
if (Object.prototype.toString.call(arg) === '[object Object]') {
return Object.keys(arg).length !== 0 ? true : false;
}
return false;
};
var isTarget = function(obj, propertyToCheckFor) {
return isNonEmptyObjectLiteral(obj, propertyToCheckFor) && obj[propertyToCheckFor] ? true : false;
};
var addIfTarget = function(selection, dest, propertyToCheckFor) {
if (isTarget(selection, propertyToCheckFor)) {
dest[selection.key] = selection;
}
else if (isNonEmptyObjectLiteral(selection)) {
getNestedSelections(selection, dest, propertyToCheckFor);
}
};
var getNestedSelections = function(selection, dest, propertyToCheckFor) {
for(var prop in selection) {
addIfTarget(selection[prop], dest, propertyToCheckFor);
}
};
var prepSelection = function(selection, dest, propertyToCheckFor) {
if (Array.isArray(selection)) {
for (var i = 0; i < selection.length; i++) {
addIfTarget(selection[i], dest, propertyToCheckFor);
}
}
else {
addIfTarget(selection, dest, propertyToCheckFor);
}
};
prepSelection(browserSelection, browserSelectionPrepped, 'browserTitle');
prepSelection(layoutSelection, layoutSelectionPrepped, 'layoutName');
forAll(browserSelectionPrepped, function() {
forAll(layoutSelectionPrepped, function() {
_test(testNamePrefix, callback);
});
});
}
// Exporting functions to all other tests that will use this script
(function (export) {
export.domains = domains;
export.browsers = browsers;
export.layouts = layouts;
export.openDriver = openDriver;
export.runTest = runTest;
export.users = users;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment