Skip to content

Instantly share code, notes, and snippets.

@mvasin
Created November 5, 2018 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvasin/ec57d4ada383c8a467c1cafc1ebfe435 to your computer and use it in GitHub Desktop.
Save mvasin/ec57d4ada383c8a467c1cafc1ebfe435 to your computer and use it in GitHub Desktop.
Hello World on BrowserStack
const {Builder, By} = require('selenium-webdriver');
const {BROWSERSTACK_USERNAME, BROWSERSTACK_KEY} = process.env;
if (!BROWSERSTACK_USERNAME || !BROWSERSTACK_KEY)
throw Error('Specify BROWSERSTACK_USERNAME and BROWSERSTACK_KEY env vars!');
const chrome = {
browserName: 'Chrome',
browser_version: '70.0',
os: 'Windows',
os_version: '10',
resolution: '1024x768',
'browserstack.user': BROWSERSTACK_USERNAME,
'browserstack.key': BROWSERSTACK_KEY
}
const ie = {
browserName: 'IE',
browser_version: '11.0',
os: 'Windows',
os_version: '10',
resolution: '1024x768',
'browserstack.user': BROWSERSTACK_USERNAME,
'browserstack.key': BROWSERSTACK_KEY
}
const browsers = [chrome, ie];
async function h1IsPresent(driver) {
let isSuccessful = false;
try {
await driver.get('http://react-div-100vh.netlify.com');
await driver.findElement(By.css('h1'));
isSuccessful = true; // if we ran all selenium code and didn't blow up
} finally {
return isSuccessful;
}
};
// one for now, maybe more later
const tests = [h1IsPresent];
// run tests, array of tests each containing array of environments
const runs = tests.map(test => browsers.map(browser => {
const driver = new Builder()
.usingServer('http://hub-cloud.browserstack.com/wd/hub')
.withCapabilities(browser)
.build()
return {
name: test.name, // test function name
browser,
result: test(driver) // Result promise. Execution starts here!
}
})).reduce((acc, val) => acc.concat(val), []) // flatten
// run this only after all promises have been resolved in parallel
// otherwise it will execute tests sequentially
async function report(runs) {
return Promise.all(runs.map(r => r.result)).then(results => {
// put actual results (booleans) into result properties
resolvedRuns = runs.map((run, i) => ({...run, result: results[i]}));
// acquire some statistics
const browsersCount = (new Set(runs.map(r => r.browser))).size;
const testsCount = (new Set(runs.map(r => r.name))).size;
const failedRuns = resolvedRuns.filter(run => !run.result);
runsCount = runs.length;
failedRunsCount = failedRuns.length;
console.log(`\nExecuted ${testsCount} tests in ${browsersCount} browsers resulting in ${runsCount} runs.`)
if (failedRunsCount === 0) {
console.log('All runs succeeded.')
};
// if some tests failed
console.log(`${failedRunsCount} runs failed`)
console.log('Here are the problematic combinations:')
failedRuns.forEach(r => {
const browser = `${r.browser.browserName} v.${r.browser.browser_version} on ${r.browser.os}`;
console.log(`"${r.name}" test on ${browser} failed`);
});
})
}
report(runs);
// updateBrowserStackStatuses(runs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment