All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.cjs.cypress-utils.commands.js Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.initCustomCommands = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _routes = require("./constants/routes");
var _common = require("./constants/selectors/common");
var _common2 = require("./constants/texts/common");
var _common3 = require("./utils/common");
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) { (0, _defineProperty2["default"])(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; }
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(_common.commonSelectors.toastMessage).should("be.visible").should("have.text", message);

    // close toast message
    cy.get(_common.commonSelectors.toastCloseButton).click();
    cy.get(_common.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(_common.commonSelectors.toastIcon).should("have.text", "👍");
    closeToastr && cy.document().its("body").find(_common.commonSelectors.toastCloseButton).click();
    cy.document().its("body").find(_common.commonSelectors.toastIcon).should("not.exist");
  });
  Cypress.Commands.add("continueOnAlert", function (args) {
    var props = (0, _typeof2["default"])(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(_common.commonSelectors.alertTitle).should("have.text", title);
    cy.interceptApi(alias, requestCount);
    cy.get(_common.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: _routes.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((0, _toConsumableArray2["default"])(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(_common.commonSelectors.selectOption(optionText)).invoke("click");
  });
  Cypress.Commands.add("clickDropdownOption", function (optionText) {
    var dropdownSelector = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _common.commonSelectors.dropdownIcon;
    cy.get(dropdownSelector).click();
    cy.get(_common.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(_common.commonSelectors.body).then(function ($body) {
      if ($body.find(selector).length) callback();
    });
  });
  Cypress.Commands.add("ifNotExist", function (selector, callback) {
    cy.get(_common.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(_common.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(_common.commonSelectors.alertTitle).should("have.text", _common2.commonTexts.unsavedChangesAlertTitle);
    cy.get(_common.commonSelectors.alertMessage).should("have.text", _common2.commonTexts.unsavedChangesAlertMessage);
  });
  Cypress.Commands.add("verifyTooltip", function (selector, text) {
    cy.get(selector).realHover();
    cy.document().its("body").find(_common.commonSelectors.tooltip).should("have.text", text);
    cy.document().find("body").click("topRight", {
      force: true
    });
    cy.document().its("body").find(_common.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 (!_common3.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");
        });
      });
    });
  });
};
exports.initCustomCommands = initCustomCommands;
//# sourceMappingURL=commands.js.map




© 2015 - 2024 Weber Informatics LLC | Privacy Policy