You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
5.8 KiB
168 lines
5.8 KiB
7 years ago
|
const fs = require('fs')
|
||
|
const mkdirp = require('mkdirp')
|
||
|
const pify = require('pify')
|
||
6 years ago
|
const assert = require('assert')
|
||
5 years ago
|
const { delay } = require('./func')
|
||
6 years ago
|
const { until } = require('selenium-webdriver')
|
||
7 years ago
|
|
||
|
module.exports = {
|
||
6 years ago
|
assertElementNotPresent,
|
||
7 years ago
|
checkBrowserForConsoleErrors,
|
||
6 years ago
|
closeAllWindowHandlesExcept,
|
||
7 years ago
|
findElement,
|
||
|
findElements,
|
||
6 years ago
|
loadExtension,
|
||
6 years ago
|
openNewPage,
|
||
6 years ago
|
switchToWindowWithTitle,
|
||
6 years ago
|
switchToWindowWithUrlThatMatches,
|
||
6 years ago
|
verboseReportOnFailure,
|
||
|
waitUntilXWindowHandles,
|
||
7 years ago
|
}
|
||
|
|
||
|
async function loadExtension (driver, extensionId) {
|
||
|
switch (process.env.SELENIUM_BROWSER) {
|
||
|
case 'chrome': {
|
||
|
await driver.get(`chrome-extension://${extensionId}/home.html`)
|
||
|
break
|
||
|
}
|
||
|
case 'firefox': {
|
||
|
await driver.get(`moz-extension://${extensionId}/home.html`)
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function checkBrowserForConsoleErrors (driver) {
|
||
|
const ignoredLogTypes = ['WARNING']
|
||
|
const ignoredErrorMessages = [
|
||
|
// React throws error warnings on "dataset", but still sets the data-* properties correctly
|
||
|
'Warning: Unknown prop `dataset` on ',
|
||
|
// Third-party Favicon 404s show up as errors
|
||
|
'favicon.ico - Failed to load resource: the server responded with a status of 404 (Not Found)',
|
||
|
// React Development build - known issue blocked by test build sys
|
||
|
'Warning: It looks like you\'re using a minified copy of the development build of React.',
|
||
|
// Redux Development build - known issue blocked by test build sys
|
||
|
'This means that you are running a slower development build of Redux.',
|
||
|
]
|
||
|
const browserLogs = await driver.manage().logs().get('browser')
|
||
|
const errorEntries = browserLogs.filter(entry => !ignoredLogTypes.includes(entry.level.toString()))
|
||
|
const errorObjects = errorEntries.map(entry => entry.toJSON())
|
||
|
return errorObjects.filter(entry => !ignoredErrorMessages.some(message => entry.message.includes(message)))
|
||
|
}
|
||
|
|
||
|
async function verboseReportOnFailure (driver, test) {
|
||
|
let artifactDir
|
||
|
if (process.env.SELENIUM_BROWSER === 'chrome') {
|
||
|
artifactDir = `./test-artifacts/chrome/${test.title}`
|
||
|
} else if (process.env.SELENIUM_BROWSER === 'firefox') {
|
||
|
artifactDir = `./test-artifacts/firefox/${test.title}`
|
||
|
}
|
||
|
const filepathBase = `${artifactDir}/test-failure`
|
||
|
await pify(mkdirp)(artifactDir)
|
||
|
const screenshot = await driver.takeScreenshot()
|
||
|
await pify(fs.writeFile)(`${filepathBase}-screenshot.png`, screenshot, { encoding: 'base64' })
|
||
|
const htmlSource = await driver.getPageSource()
|
||
|
await pify(fs.writeFile)(`${filepathBase}-dom.html`, htmlSource)
|
||
|
}
|
||
7 years ago
|
|
||
|
async function findElement (driver, by, timeout = 10000) {
|
||
|
return driver.wait(until.elementLocated(by), timeout)
|
||
|
}
|
||
|
|
||
|
async function findElements (driver, by, timeout = 10000) {
|
||
|
return driver.wait(until.elementsLocated(by), timeout)
|
||
|
}
|
||
6 years ago
|
|
||
|
async function openNewPage (driver, url) {
|
||
|
await driver.executeScript('window.open()')
|
||
|
await delay(1000)
|
||
|
|
||
|
const handles = await driver.getAllWindowHandles()
|
||
6 years ago
|
const lastHandle = handles[handles.length - 1]
|
||
|
await driver.switchTo().window(lastHandle)
|
||
6 years ago
|
|
||
|
await driver.get(url)
|
||
|
await delay(1000)
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
async function waitUntilXWindowHandles (driver, x, delayStep = 1000, timeout = 5000) {
|
||
|
let timeElapsed = 0
|
||
|
async function _pollWindowHandles () {
|
||
|
const windowHandles = await driver.getAllWindowHandles()
|
||
|
if (windowHandles.length === x) {
|
||
|
return
|
||
|
}
|
||
|
await delay(delayStep)
|
||
|
timeElapsed += delayStep
|
||
|
if (timeElapsed > timeout) {
|
||
|
throw new Error('waitUntilXWindowHandles timed out polling window handles')
|
||
|
} else {
|
||
|
await _pollWindowHandles()
|
||
|
}
|
||
|
}
|
||
|
return await _pollWindowHandles()
|
||
6 years ago
|
}
|
||
|
|
||
|
async function switchToWindowWithTitle (driver, title, windowHandles) {
|
||
|
if (!windowHandles) {
|
||
|
windowHandles = await driver.getAllWindowHandles()
|
||
|
} else if (windowHandles.length === 0) {
|
||
|
throw new Error('No window with title: ' + title)
|
||
|
}
|
||
|
const firstHandle = windowHandles[0]
|
||
|
await driver.switchTo().window(firstHandle)
|
||
|
const handleTitle = await driver.getTitle()
|
||
|
|
||
|
if (handleTitle === title) {
|
||
|
return firstHandle
|
||
|
} else {
|
||
|
return await switchToWindowWithTitle(driver, title, windowHandles.slice(1))
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
/**
|
||
|
* Closes all windows except those in the given list of exceptions
|
||
|
* @param {object} driver the WebDriver instance
|
||
|
* @param {string|Array<string>} exceptions the list of window handle exceptions
|
||
|
* @param {Array?} windowHandles the full list of window handles
|
||
|
* @returns {Promise<void>}
|
||
|
*/
|
||
6 years ago
|
async function closeAllWindowHandlesExcept (driver, exceptions, windowHandles) {
|
||
|
exceptions = typeof exceptions === 'string' ? [ exceptions ] : exceptions
|
||
|
windowHandles = windowHandles || await driver.getAllWindowHandles()
|
||
|
const lastWindowHandle = windowHandles.pop()
|
||
|
if (!exceptions.includes(lastWindowHandle)) {
|
||
|
await driver.switchTo().window(lastWindowHandle)
|
||
|
await delay(1000)
|
||
|
await driver.close()
|
||
|
await delay(1000)
|
||
|
}
|
||
|
return windowHandles.length && await closeAllWindowHandlesExcept(driver, exceptions, windowHandles)
|
||
|
}
|
||
6 years ago
|
|
||
|
async function assertElementNotPresent (webdriver, driver, by) {
|
||
6 years ago
|
let dataTab
|
||
6 years ago
|
try {
|
||
6 years ago
|
dataTab = await findElement(driver, by, 4000)
|
||
6 years ago
|
} catch (err) {
|
||
6 years ago
|
assert(err instanceof webdriver.error.NoSuchElementError || err instanceof webdriver.error.TimeoutError)
|
||
|
}
|
||
6 years ago
|
assert.ok(!dataTab, 'Found element that should not be present')
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
async function switchToWindowWithUrlThatMatches (driver, regexp, windowHandles) {
|
||
|
if (!windowHandles) {
|
||
|
windowHandles = await driver.getAllWindowHandles()
|
||
|
} else if (windowHandles.length === 0) {
|
||
|
throw new Error('No window that matches: ' + regexp)
|
||
|
}
|
||
|
const firstHandle = windowHandles[0]
|
||
|
await driver.switchTo().window(firstHandle)
|
||
|
const windowUrl = await driver.getCurrentUrl()
|
||
|
if (windowUrl.match(regexp)) {
|
||
|
return firstHandle
|
||
|
} else {
|
||
|
return await switchToWindowWithUrlThatMatches(driver, regexp, windowHandles.slice(1))
|
||
|
}
|
||
|
}
|