ember-cli-page-object
Version:
This ember-cli addon eases the construction of page objects on your acceptance and integration tests
72 lines (64 loc) • 2.33 kB
JavaScript
import {
throwBetterError
} from '../better-errors';
import $ from '-jquery';
/**
* @private
*
* Fills inputs, textareas, or contenteditable elements with the passed-in content.
*
* @param {jQuery} $selection jQuery object containing collection of DOM elements to fill in
* @param {string} content Content to be inserted into the target element(s)
* @param {Object} options Options for error reporting
* @param {string} options.selector jQuery selector used to target element(s) to fill in
* @param {Ceibo} options.pageObjectNode PageObject node containing the method which, when invoked, resulted in this call to `fillElement`
* @param {string} options.pageObjectKey Key of method on PageObject which, when invoked, resulted in this call to `fillElement`
* @return
*
* @throws Will throw an error if called on a contenteditable element that has `contenteditable="false"`
*/
export function fillElement(selection, content, { selector, pageObjectNode, pageObjectKey }) {
const $selection = $(selection);
if ($selection.is('[contenteditable][contenteditable!="false"]')) {
$selection.html(content);
} else if ($selection.is('[contenteditable="false"]')) {
throwBetterError(
pageObjectNode,
pageObjectKey,
'Element cannot be filled because it has `contenteditable="false"`.', {
selector
}
);
} else {
$selection.val(content);
}
}
/**
* @private
*
* Given an element, asserts that element is focusable/blurable
*
* @param {Element} element - the element to check
*/
export function assertFocusable(element, { selector, pageObjectNode, pageObjectKey }) {
let $element = $(element);
let error;
if ($element.is(':hidden')) {
error = 'hidden';
} else if ($element.is(':disabled')) {
error = 'disabled';
} else if ($element.is('[contenteditable="false"]')) {
error = 'contenteditable="false"';
} else if (!$element.is(':input, a[href], area[href], iframe, [contenteditable], [tabindex]')) {
error = 'not a link, input, form element, contenteditable, iframe, or an element with tabindex';
}
if (error) {
throwBetterError(
pageObjectNode,
pageObjectKey,
`Element is not focusable because it is ${error}`, {
selector
}
);
}
}