cypress-cucumber-steps
Version:
Cypress Cucumber step definitions
73 lines • 2.84 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getByLabelText = getByLabelText;
exports.getLabelElements = getLabelElements;
var queries_1 = require("../queries");
var element_1 = require("./element");
/**
* Get first Cypress element by label text.
*
* @param element - Element name.
* @param text - Label text.
* @param options - Options.
* @returns - Cypress element.
* @private
*/
function getByLabelText(element, text, options) {
(0, queries_1.When_I_find_element_by_label_text)(text, options);
return (0, element_1.getCypressElement)().then(function ($element) {
var tagName = $element.prop('tagName').toLowerCase();
// https://developer.mozilla.org/docs/Web/HTML/Element/label
if (tagName === 'label') {
var forValue = $element.attr('for');
if (forValue) {
return cy.get("#".concat(forValue));
}
else {
return $element.find(element).first();
}
}
// https://developer.mozilla.org/docs/Web/Accessibility/ARIA/Attributes/aria-labelledby
if ($element.attr('aria-labelledby') === text) {
return cy.get("#".concat(text));
}
// https://developer.mozilla.org/docs/Web/Accessibility/ARIA/Attributes/aria-label
if (tagName === element && $element.attr('aria-label') === text) {
return $element;
}
throw new Error("Unable to get ".concat(element, " by label text: ").concat(text));
});
}
/**
* Get label elements.
*
* @param text - Label text.
* @param options - Options.
* @returns - Cypress element.
* @private
*/
function getLabelElements(text, options) {
var selectors = [
"label:contains(".concat(JSON.stringify(text), ")"),
"[aria-labelledby=".concat(JSON.stringify(text), "]"),
"[aria-label=".concat(JSON.stringify(text), "]"),
];
// @ts-expect-error Property 'pseudoSelector' does not exist on type 'object | undefined'.
var pseudoSelector = options.pseudoSelector, opts = __rest(options, ["pseudoSelector"]);
if (pseudoSelector) {
selectors = selectors.map(function (label) { return "".concat(label, ":").concat(pseudoSelector); });
}
return cy.get(selectors.join(','), opts);
}
//# sourceMappingURL=label.js.map