@atomic-testing/component-driver-html
Version:
HTML component driver for atomic-testing
448 lines (435 loc) • 12.8 kB
JavaScript
//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
const __atomic_testing_core = __toESM(require("@atomic-testing/core"));
//#region src/components/HTMLAnchorDriver.ts
var HTMLAnchorDriver = class extends __atomic_testing_core.ComponentDriver {
/**
* Trigger a click on the anchor element.
*/
async click(option) {
await this.interactor.click(this.locator, option);
}
/**
* Hover over the anchor element.
*/
async hover(option) {
await this.interactor.hover(this.locator, option);
}
/**
* Retrieve the link's `href` attribute.
*/
async getHref() {
return this.interactor.getAttribute(this.locator, "href");
}
/**
* Retrieve the link's `target` attribute.
*/
async getTarget() {
return this.interactor.getAttribute(this.locator, "target");
}
/**
* Identifier for this driver.
*/
get driverName() {
return "HTMLAnchorDriver";
}
};
//#endregion
//#region src/components/HTMLButtonDriver.ts
var HTMLButtonDriver = class extends __atomic_testing_core.ComponentDriver {
/**
* Check if the button element is disabled.
*
* @returns A promise that resolves to `true` when the underlying
* `disabled` attribute is present.
*/
isDisabled() {
return this.interactor.isDisabled(this.locator);
}
/**
* Identifier for this driver. Primarily used by debugging utilities.
*/
get driverName() {
return "HTMLButtonDriver";
}
};
//#endregion
//#region src/components/HTMLCheckboxDriver.ts
var HTMLCheckboxDriver = class extends __atomic_testing_core.ComponentDriver {
/**
* Read the checkbox value attribute.
*
* @returns The value assigned to the checkbox or `null` when the attribute is
* not present.
*/
async getValue() {
const value = await this.interactor.getAttribute(this.locator, "value");
return value ?? null;
}
/**
* Determine if the checkbox is currently checked.
*/
async isSelected() {
const isChecked = await this.interactor.isChecked(this.locator);
return isChecked;
}
/**
* Change the checked state of the checkbox.
*
* @param selected Desired checked state.
*/
async setSelected(selected) {
const currentSelected = await this.isSelected();
if (currentSelected !== selected) await this.interactor.click(this.locator);
}
/**
* Check whether the checkbox element is disabled.
*/
isDisabled() {
return this.interactor.isDisabled(this.locator);
}
/**
* Check whether the checkbox is read only.
*/
isReadonly() {
return this.interactor.isReadonly(this.locator);
}
/**
* Identifier for this driver.
*/
get driverName() {
return "HTMLCheckboxDriver";
}
};
//#endregion
//#region src/components/HTMLCheckboxGroupDriver.ts
var HTMLCheckboxGroupDriver = class extends __atomic_testing_core.ComponentDriver {
/**
* Retrieve the list of values currently selected within the group.
*
* Iterates over every checkbox rather than relying on a CSS selector so that
* an empty selection does not cause a driver error.
*/
async getValue() {
const availableValues = await this.interactor.getAttribute(this.locator, "value", true);
const value = [];
for (const val of availableValues) {
const itemLocator = (0, __atomic_testing_core.byValue)(val, "Same");
const locator = __atomic_testing_core.locatorUtil.append(this.locator, itemLocator);
const isChecked = await this.interactor.isChecked(locator);
if (isChecked) value.push(val);
}
return value;
}
/**
* Select or deselect checkboxes so that their combined values equal the
* provided list.
*
* @param values Values that should be selected after the operation.
* @returns Always resolves to `true` once the selection has been adjusted.
*/
async setValue(values) {
const currentValues = await this.getValue();
const { toAdd, toRemove } = __atomic_testing_core.collectionUtil.getDifference(currentValues, values);
for (const val of toAdd) await this.setSelectedByValue(val, true);
for (const val of toRemove) await this.setSelectedByValue(val, false);
return true;
}
/**
* Helper used by {@link setValue} to change the checked state of a checkbox
* with a specific value.
*/
async setSelectedByValue(value, selected) {
const itemLocator = (0, __atomic_testing_core.byValue)(value, "Same");
const locator = __atomic_testing_core.locatorUtil.append(this.locator, itemLocator);
const checkBoxDriver = new HTMLCheckboxDriver(locator, this.interactor);
await checkBoxDriver.setSelected(selected);
}
/**
* Identifier for this driver.
*/
get driverName() {
return "HTMLCheckboxGroupDriver";
}
};
//#endregion
//#region src/components/HTMLElementDriver.ts
/**
* Generic HTML element driver for non-interactive elements
*/
var HTMLElementDriver = class extends __atomic_testing_core.ComponentDriver {
/**
* Identifier for this driver.
*/
get driverName() {
return "HTMLElementDriver";
}
};
//#endregion
//#region src/components/HTMLHiddenInputDriver.ts
var HTMLHiddenInputDriver = class extends __atomic_testing_core.ComponentDriver {
/**
* Create a driver for a hidden input element.
*/
constructor(locator, interactor, option) {
super(locator, interactor, {
...option,
parts: {}
});
}
/**
* Retrieve the value attribute of the hidden input.
*/
async getValue() {
const value = await this.interactor.getAttribute(this.locator, "value");
return value ?? null;
}
/**
* Setting the value of a hidden input is not supported.
*/
setValue(_value) {
throw new Error("Not implemented");
}
/**
* Identifier for this driver.
*/
get driverName() {
return "HTMLHiddenInput";
}
};
//#endregion
//#region src/components/HTMLOptionDriver.ts
var HTMLOptionDriver = class extends __atomic_testing_core.ComponentDriver {
/**
* Text content of the option element.
*/
async label() {
const label = await this.getText();
return label?.trim() || null;
}
/**
* Value attribute for the option element.
*
* When no explicit value is set, the trimmed label is returned.
*/
async value() {
const val = await this.interactor.getAttribute(this.locator, "value") ?? await this.label();
return val?.trim() || null;
}
/**
* Identifier for this driver.
*/
get driverName() {
return "HTMLOptionDriver";
}
};
//#endregion
//#region src/components/HTMLRadioButtonGroupDriver.ts
var HTMLRadioButtonGroupDriver = class extends __atomic_testing_core.ComponentDriver {
/**
* Get the value of the currently selected radio button in the group.
*/
async getValue() {
const checkedLocator = (0, __atomic_testing_core.byChecked)(true);
const locator = __atomic_testing_core.locatorUtil.append(this.locator, checkedLocator);
const value = await this.interactor.getAttribute(locator, "value");
return value ?? null;
}
/**
* Select the radio button with the specified value.
*
* @param value Value attribute of the radio button to select.
*/
async setValue(value) {
if (value == null) throw new Error("Cannot be done");
const valueLocator = (0, __atomic_testing_core.byValue)(value, "Same");
const locator = __atomic_testing_core.locatorUtil.append(this.locator, valueLocator);
await this.interactor.click(locator);
return true;
}
/**
* Identifier for this driver.
*/
get driverName() {
throw "HTMLRadioButtonGroupDriver";
}
};
//#endregion
//#region src/components/HTMLSelectDriver.ts
const optionLocator = (0, __atomic_testing_core.byTagName)("option");
var HTMLSelectDriver = class extends __atomic_testing_core.ComponentDriver {
/**
* Determine whether the select element allows multiple selections.
*/
async isMultiple() {
const multiple = await this.interactor.getAttribute(this.locator, "multiple");
return multiple != null;
}
/**
* Get the current value(s) of the select element.
*
* @returns `null` when nothing is selected, otherwise the selected value or
* array of values depending on the `multiple` attribute.
*/
async getValue() {
const isMultiple = await this.isMultiple();
const values = await this.interactor.getSelectValues(this.locator);
const returnedValue = isMultiple ? values : values?.[0];
return returnedValue ?? null;
}
/**
* Select one or more options based on their value attribute.
*
* @param value Value or list of values to select. Use `null` to clear the
* selection.
*/
async setValue(value) {
let selectedValues = [];
if (value != null) selectedValues = Array.isArray(value) ? value : [value];
await this.interactor.selectOptionValue(this.locator, selectedValues);
return true;
}
/**
* Resolve option values from their visible labels.
*/
async getValuesByLabels(labels) {
const labelSet = new Set(labels);
const values = [];
const itemLocatorBase = __atomic_testing_core.locatorUtil.append(this.locator, optionLocator);
for await (const item of __atomic_testing_core.listHelper.getListItemIterator(this, itemLocatorBase, HTMLOptionDriver)) {
const label = await item.label();
const value = await item.value();
if (label != null && labelSet.has(label) && value != null) values.push(value);
}
return values;
}
/**
* Select option(s) using their labels rather than values.
*/
async selectByLabel(label) {
const labels = __atomic_testing_core.collectionUtil.toArray(label);
const values = await this.getValuesByLabels(labels);
await this.setValue(values);
}
/**
* Retrieve the label text for the currently selected option(s).
*/
async getSelectedLabel(isMultiple) {
if (await this.exists()) {
const labels = await this.interactor.getSelectLabels(this.locator);
if (isMultiple) return labels;
return labels[0] ?? null;
}
return null;
}
/**
* Check whether the select element is disabled.
*/
isDisabled() {
return this.interactor.isDisabled(this.locator);
}
/**
* Check whether the select element is read only.
*/
isReadonly() {
return this.interactor.isReadonly(this.locator);
}
/**
* Identifier for this driver.
*/
get driverName() {
return "HTMLSelectDriver";
}
};
//#endregion
//#region src/components/HTMLTextInputDriver.ts
var HTMLTextInputDriver = class extends __atomic_testing_core.ComponentDriver {
/**
* Create a text input driver.
*/
constructor(locator, interactor, option) {
super(locator, interactor, {
...option,
parts: {}
});
}
/**
* Read the value of the input element.
*/
async getValue() {
const value = await this.interactor.getInputValue(this.locator);
return value ?? null;
}
/**
* Set the value of the input, if the input is date, the value should be in the format of 'yyyy-MM-dd'.
* If the input is time, the value should be in the format of 'HH:mm:'.
* If the input is datetime-local, the value should be in the format of 'yyyy-MM-ddTHH:mm'.
* @param value Value to be set.
* @returns
*/
async setValue(value) {
await this.interactor.enterText(this.locator, value ?? "");
return true;
}
/**
* Check whether the text input is disabled.
*/
isDisabled() {
return this.interactor.isDisabled(this.locator);
}
/**
* Check whether the text input is read only.
*/
isReadonly() {
return this.interactor.isReadonly(this.locator);
}
/**
* Identifier for this driver.
*/
get driverName() {
return "HTMLTextInput";
}
};
//#endregion
//#region src/components/HTMLTextAreaDriver.ts
var HTMLTextAreaDriver = class extends HTMLTextInputDriver {
/**
* Identifier for this driver.
*/
get driverName() {
return "HTMLTextArea";
}
};
//#endregion
exports.HTMLAnchorDriver = HTMLAnchorDriver;
exports.HTMLButtonDriver = HTMLButtonDriver;
exports.HTMLCheckboxDriver = HTMLCheckboxDriver;
exports.HTMLCheckboxGroupDriver = HTMLCheckboxGroupDriver;
exports.HTMLElementDriver = HTMLElementDriver;
exports.HTMLHiddenInputDriver = HTMLHiddenInputDriver;
exports.HTMLOptionDriver = HTMLOptionDriver;
exports.HTMLRadioButtonGroupDriver = HTMLRadioButtonGroupDriver;
exports.HTMLSelectDriver = HTMLSelectDriver;
exports.HTMLTextAreaDriver = HTMLTextAreaDriver;
exports.HTMLTextInputDriver = HTMLTextInputDriver;
//# sourceMappingURL=index.js.map