nightwatch
Version:
Easy to use Node.js based end-to-end testing solution for web applications using the W3C WebDriver API.
189 lines (175 loc) • 6.75 kB
JavaScript
const ErrorCode = {
INSECURE_CERTIFICATE: 'insecure certificate',
ELEMENT_CLICK_INTERCEPTED: 'element click intercepted',
ELEMENT_IS_NOT_SELECTABLE: 'element not selectable',
ELEMENT_IS_NOT_INTERACTABLE: 'element not interactable',
INVALID_ARGUMENT: 'invalid argument',
INVALID_COOKIE_DOMAIN: 'invalid cookie domain',
INVALID_ELEMENT_COORDINATES: 'invalid coordinates',
INVALID_ELEMENT_STATE: 'invalid element state',
INVALID_SELECTOR: 'invalid selector',
NO_SUCH_SESSION: 'invalid session id',
JAVASCRIPT_ERROR: 'javascript error',
MOVE_TARGET_OUT_OF_BOUNDS: 'move target out of bounds',
NO_SUCH_ALERT: 'no such alert',
NO_SUCH_COOKIE: 'no such cookie',
NO_SUCH_ELEMENT: 'no such element',
NO_SUCH_FRAME: 'no such frame',
NO_SUCH_WINDOW: 'no such window',
SCRIPT_TIMEOUT: 'script timeout',
SESSION_NOT_CREATED_EXCEPTION: 'session not created',
STALE_ELEMENT_REFERENCE: 'stale element reference',
TIMEOUT: 'timeout',
UNABLE_TO_SET_COOKIE: 'unable to set cookie',
UNABLE_TO_CAPTURE_SCREEN: 'unable to capture screen',
UNEXPECTED_ALERT_OPEN: 'unexpected alert open',
UNKNOWN_COMMAND: 'unknown command',
UNKNOWN_ERROR: 'unknown error',
UNKNOWN_METHOD: 'unknown method',
UNSUPPORTED_OPERATION: 'unsupported operation'
};
const Errors = {
[ ]: {
message: 'A modal dialog was open, blocking this operation.'
},
[ ]: {
message: 'A new session could not be created.'
},
[ ]: {
message: 'An attempt was made to operate on a modal dialog when one was not open.'
},
[ ]: {
message: 'An element command could not be completed because the element is in an invalid state (e.g. attempting to click an element that is no longer attached to the document).'
},
[ ]: {
message: 'An element could not be located on the page using the given search parameters.',
help: [
'Please inspect the html before the step',
'Verify if an element with the mentioned selector is present in the DOM tree'
]
},
[ ]: {
message: 'An error occurred while executing user supplied JavaScript.'
},
[ ]: {
message: 'An unknown server-side error occurred while processing the command.'
},
[ ]: {
message: 'No cookie matching the given path name was found amongst the cookies of the current active document.'
},
[ ]: {
message: 'The SSL certificate running on this host cannot be validated. ' +
'If you wish to force accepting insecure SSL certificates, set acceptInsecureCerts=true in the ' +
'desiredCapabilities options.'
},
[ ]: {
message: 'The arguments passed to the command are either invalid or malformed.'
},
[ ]: {
message: 'The arguments passed to the command are either invalid or malformed.'
},
[ ]: {
message: 'The command failed because the referenced element is no longer attached to the DOM.'
},
[ ]: {
message: 'The cookie domain name is not valid for the current page.'
},
[ ]: {
message: 'The coordinates provided to an interactions operation are invalid.'
},
[ ]: {
message: 'The element click command could not be completed because another element is receiving the click event.'
},
[ ]: {
message: 'The operation did not complete before its timeout expired.'
},
[ ]: {
message: 'The request to set a cookie\'s value could not be satisfied.'
},
[ ]: {
message: 'The requested command matched a known URL but did not match a method for that URL.'
},
[ ]: {
message: 'The requested element cannot be selected.'
},
[ ]: {
message: 'The requested element is not pointer or keyboard interactable.'
},
[ ]: {
message: 'The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource.'
},
[ ]: {
message: 'The screen capture failed.'
},
[ ]: {
message: 'The script did not complete before its timeout expired.'
},
[ ]: {
message: 'The session is either terminated or not started.'
},
[ ]: {
message: 'The specified frame could not be found.',
help: [
'Please inspect the html before the step.',
'Verify if an iframe with the id is present in the DOM tree.'
]
},
[ ]: {
message: 'The specified window could not be found.',
help: [
'Print existing window handles by using the command "print driver.window_handles".',
'Verify if the window name you have entered exists in the list.'
]
},
[ ]: {
message: 'The supplied argument was an invalid selector (e.g. XPath/CSS).'
},
[ ]: {
message: 'The target for mouse interaction is not in the browser\'s viewport and cannot be brought into the viewport.'
},
[ ]: {
message: 'Unsupported operation exception.'
}
};
const IosSessionErrors = {
SessionNotCreatedError: ErrorCode.SESSION_NOT_CREATED_EXCEPTION,
UnsupportedOperationError: ErrorCode.UNSUPPORTED_OPERATION
};
const SeleniumNightwatchErrorCodeMap = {
NoSuchElementError: ErrorCode.NO_SUCH_ELEMENT
};
module.exports = {
findErrorById(statusCode) {
return {
status: statusCode,
id: statusCode,
message: Errors[statusCode].message
};
},
getErrorObject(err) {
if (err && (err.name in SeleniumNightwatchErrorCodeMap)) {
const statusCode = SeleniumNightwatchErrorCodeMap[err.name];
for (const [key, value] of Object.entries(Errors[statusCode])) {
err[key] ||= value;
}
err.id = statusCode;
return err;
} else if (err && err.name && (err.stack || err.stackTrace)){
return err;
}
if (err instanceof Error) {
return err;
}
let error;
if (typeof err == 'string') {
error = new Error(err);
} else if (err.error instanceof Error) {
return err.error;
}
error = error || new Error('unknown error');
return error;
},
StatusCode: ErrorCode,
Response: Errors,
IosSessionErrors: IosSessionErrors
};