package.cypress-utils.commands.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _typeof from "@babel/runtime/helpers/typeof";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import { requestApis } from "./constants/routes";
import { commonSelectors } from "./constants/selectors/common";
import { commonTexts } from "./constants/texts/common";
import { isStagingEnv } from "./utils/common";
export var initCustomCommands = function initCustomCommands() {
Cypress.Commands.add("clearAndType", function (selector, text) {
cy.get(selector).clear().type(text);
});
Cypress.Commands.add("clearAndTypeFast", function (selector, text) {
cy.get(selector).clear().type(text, {
delay: 0
});
});
Cypress.Commands.add("clearByClickAndTypeFast", function (selector, text) {
cy.get(selector).click().clear().type(text, {
delay: 0
});
});
Cypress.Commands.add("typeFast", function (selector, text) {
cy.get(selector).type(text, {
delay: 0
});
});
Cypress.Commands.add("typeAndEnter", function (selector, text) {
cy.get(selector).clear().type("".concat(text, "{enter}"));
});
Cypress.Commands.add("verifyToastMessage", function (message) {
cy.get(commonSelectors.toastMessage).should("be.visible").should("have.text", message);
// close toast message
cy.get(commonSelectors.toastCloseButton).click();
cy.get(commonSelectors.toastMessage).should("not.exist");
});
Cypress.Commands.add("verifyToastIcon", function () {
var closeToastr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
cy.document().its("body").find(commonSelectors.toastIcon).should("have.text", "👍");
closeToastr && cy.document().its("body").find(commonSelectors.toastCloseButton).click();
cy.document().its("body").find(commonSelectors.toastIcon).should("not.exist");
});
Cypress.Commands.add("continueOnAlert", function (args) {
var props = _typeof(args) === "object" ? args : {
alias: args
};
var alias = props.alias,
title = props.title,
_props$requestCount = props.requestCount,
requestCount = _props$requestCount === void 0 ? 1 : _props$requestCount,
verifyToastIcon = props.verifyToastIcon,
toastMessage = props.toastMessage;
var hasMoreThanOneRequest = requestCount > 1;
title && cy.get(commonSelectors.alertTitle).should("have.text", title);
cy.interceptApi(alias, requestCount);
cy.get(commonSelectors.alertModalSubmitButton).click();
cy.wait("@".concat(alias));
verifyToastIcon && cy.verifyToastIcon();
toastMessage && cy.verifyToastMessage(toastMessage);
hasMoreThanOneRequest && cy.waitForMultipleRequest("@".concat(alias), requestCount - 1);
});
Cypress.Commands.add("interceptApi", function (alias) {
var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
return times > 0 && cy.intercept({
url: requestApis.allPath,
resourceType: "xhr",
times: times
}).as(alias);
});
Cypress.Commands.add("waitForMultipleRequest", function (alias) {
var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
return cy.wrap(_toConsumableArray(new Array(times))).each(function () {
return cy.wait(alias);
});
});
Cypress.Commands.add("apiRequest", function (options) {
return cy.get("@requestHeaders").then(function (requestHeaders) {
return requestHeaders ? cy.request(_objectSpread(_objectSpread({}, options), {}, {
headers: _objectSpread(_objectSpread({}, requestHeaders), {}, {
"accept-encoding": "gzip"
})
})) : cy.log("No request headers found");
});
});
Cypress.Commands.add("reloadAndWait", function () {
var requestCount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
if (!(requestCount > 0)) return;
cy.interceptApi("reloadAllRequests", requestCount);
cy.reload();
cy.waitForMultipleRequest("@reloadAllRequests", requestCount);
});
Cypress.Commands.add("selectOption", function (containerSelector, optionText) {
cy.get(containerSelector).click().type(optionText);
cy.get(commonSelectors.selectOption(optionText)).invoke("click");
});
Cypress.Commands.add("clickDropdownOption", function (optionText) {
var dropdownSelector = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : commonSelectors.dropdownIcon;
cy.get(dropdownSelector).click();
cy.get(commonSelectors.dropdownContainer).contains(optionText).invoke("click");
});
Cypress.Commands.add("getText", function (selector) {
return cy.get(selector).invoke("text");
});
Cypress.Commands.add("getValue", function (selector) {
return cy.get(selector).invoke("val");
});
// Deprecated. Use withinIframe.
Cypress.Commands.add("getIframe", function (iframeSelector) {
return cy.get(iframeSelector).its("0.contentDocument.body").should("not.be.empty");
});
Cypress.Commands.add("withinIframe", function (selector, callback) {
cy.get(selector, {
timeout: 10000
}).its("0.contentDocument.body").should("be.visible").and("not.be.empty");
cy.get(selector, {
timeout: 10000
}).its("0.contentDocument.body").eq(0).within(callback);
});
Cypress.Commands.add("openInSameTabOnClick", function (_ref) {
var url = _ref.url,
alias = _ref.alias,
selector = _ref.selector;
cy.window().then(function (win) {
cy.stub(win, "open").as(alias)
// eslint-disable-next-line xss/no-location-href-assign
.callsFake(function (newUrl) {
return win.location.href = newUrl;
});
});
cy.get(selector).click();
cy.get("@".concat(alias)).should("be.called");
cy.url().should("include", url);
});
Cypress.Commands.add("globalState", function (key) {
return cy.task("getGlobalState", key);
});
Cypress.Commands.add("ifExist", function (selector, callback) {
cy.get(commonSelectors.body).then(function ($body) {
if ($body.find(selector).length) callback();
});
});
Cypress.Commands.add("ifNotExist", function (selector, callback) {
cy.get(commonSelectors.body).then(function ($body) {
if (!$body.find(selector).length) callback();
});
});
Cypress.Commands.add("dragAndDrop", function (_ref2) {
var subject = _ref2.subject,
target = _ref2.target,
_ref2$dragIndex = _ref2.dragIndex,
dragIndex = _ref2$dragIndex === void 0 ? 0 : _ref2$dragIndex,
_ref2$dropIndex = _ref2.dropIndex,
dropIndex = _ref2$dropIndex === void 0 ? 0 : _ref2$dropIndex;
cy.get(subject).should("be.visible", {
timeout: 20000
});
Cypress.log({
name: "DRAGNDROP",
message: "Dragging element ".concat(subject, " to ").concat(target),
consoleProps: function consoleProps() {
return {
subject: subject,
target: target
};
}
});
cy.get(target).eq(dropIndex).then(function ($target) {
var coordsDrop = $target[0].getBoundingClientRect();
cy.get(subject).eq(dragIndex).then(function (subject) {
var coordsDrag = subject[0].getBoundingClientRect();
cy.wrap(subject).trigger("mousedown", {
button: 0,
// BUTTON_INDEX
clientX: coordsDrag.x,
clientY: coordsDrag.y,
force: true
}).trigger("mousemove", {
button: 0,
// BUTTON_INDEX
clientX: coordsDrag.x + 10,
// SLOPPY_CLICK_THRESHOLD
clientY: coordsDrag.y,
force: true
});
cy.get("body").trigger("mousemove", {
button: 0,
// BUTTON_INDEX
clientX: coordsDrop.x,
clientY: coordsDrop.y,
force: true
}).trigger("mouseup");
});
});
});
Cypress.Commands.add("verifyPlaceholder", function (textInputSelector, value) {
cy.get(textInputSelector).should("have.attr", "placeholder").and("eq", value);
});
Cypress.Commands.add("enableOrDisableSwitch", function (switchButton, value) {
cy.get(switchButton).invoke("click");
cy.get(switchButton).should("have.prop", "value").and("eq", value);
});
Cypress.Commands.add("interceptNonVersionedApi", function (_ref3) {
var _ref3$url = _ref3.url,
url = _ref3$url === void 0 ? "*" : _ref3$url,
alias = _ref3.alias,
_ref3$times = _ref3.times,
times = _ref3$times === void 0 ? 1 : _ref3$times;
cy.intercept({
url: url,
times: times
}).as(alias);
});
Cypress.Commands.add("typeOnSearchField", function (_ref4) {
var selector = _ref4.selector,
value = _ref4.value,
_ref4$skipSearchReque = _ref4.skipSearchRequest,
skipSearchRequest = _ref4$skipSearchReque === void 0 ? true : _ref4$skipSearchReque,
delay = _ref4.delay;
cy.get(selector).invoke("val").then(function (currentValue) {
if (currentValue !== value) {
!skipSearchRequest && cy.interceptApi("searchRequest");
cy.clearAndTypeFast(selector, value);
!skipSearchRequest && cy.wait("@searchRequest");
} else delay && cy.wait(delay);
});
});
Cypress.Commands.add("clickByRemoveTargetAttr", function (selector) {
cy.get(selector).invoke("removeAttr", "target").click();
});
Cypress.Commands.add("navigateToPreviewPage", function (selector) {
var fetchRequestCount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
cy.interceptApi("fetchRequests", fetchRequestCount);
cy.clickByRemoveTargetAttr(selector);
fetchRequestCount === 1 ? cy.wait("@fetchRequests") : cy.waitForMultipleRequest("@fetchRequests", fetchRequestCount);
});
Cypress.Commands.add("saveChanges", function (aliasName) {
var requestCount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
cy.interceptApi(aliasName, requestCount);
cy.get(commonSelectors.saveChangesButton).click();
cy.wait("@".concat(aliasName));
cy.verifyToastIcon();
requestCount > 1 && cy.waitForMultipleRequest("@".concat(aliasName), requestCount - 1);
});
Cypress.Commands.add("verifyAttribute", function (_ref5) {
var selector = _ref5.selector,
attr = _ref5.attr,
value = _ref5.value;
cy.get(selector).should("have.attr", attr).and("include", value);
});
Cypress.Commands.add("verifyUnsavedAlertModal", function (cancelButton) {
cy.get(cancelButton).click();
cy.get(commonSelectors.alertTitle).should("have.text", commonTexts.unsavedChangesAlertTitle);
cy.get(commonSelectors.alertMessage).should("have.text", commonTexts.unsavedChangesAlertMessage);
});
Cypress.Commands.add("verifyTooltip", function (selector, text) {
cy.get(selector).realHover();
cy.document().its("body").find(commonSelectors.tooltip).should("have.text", text);
cy.document().find("body").click("topRight", {
force: true
});
cy.document().its("body").find(commonSelectors.tooltip).should("not.exist");
});
Cypress.Commands.add("getEmailContent", function (props, options) {
return cy.mailosaurGetMessage(Cypress.env("MAILOSAUR_SERVER_ID"), props, options);
});
Cypress.Commands.add("rewriteHeaders", function () {
if (!isStagingEnv) return;
cy.intercept("*", function (req) {
return req.on("response", function (res) {
var setCookies = res.headers["set-cookie"];
res.headers["set-cookie"] = (Array.isArray(setCookies) ? setCookies : [setCookies]).filter(Boolean).map(function (headerContent) {
return headerContent.concat("secure; samesite=none");
});
});
});
});
};
//# sourceMappingURL=commands.js.map