2019-10-05 17:07:50 +00:00
|
|
|
// ***********************************************
|
|
|
|
// This example commands.js shows you how to
|
|
|
|
// create various custom commands and overwrite
|
|
|
|
// existing commands.
|
|
|
|
//
|
|
|
|
// For more comprehensive examples of custom
|
|
|
|
// commands please read more here:
|
|
|
|
// https://on.cypress.io/custom-commands
|
|
|
|
// ***********************************************
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// -- This is a parent command --
|
|
|
|
// Cypress.Commands.add("login", (email, password) => { ... })
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// -- This is a child command --
|
2020-11-30 09:24:11 +00:00
|
|
|
// Cypress.Commands.add("drag",
|
|
|
|
// { prevSubject: 'element' }, (subject, options) => { ... })
|
2019-10-05 17:07:50 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// -- This is a dual command --
|
2020-11-30 09:24:11 +00:00
|
|
|
// Cypress.Commands.add("dismiss",
|
|
|
|
// { prevSubject: 'optional' }, (subject, options) => { ... })
|
2019-10-05 17:07:50 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// -- This is will overwrite an existing command --
|
|
|
|
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
const AUTH_ACCESS_TOKEN = "auth-access-token";
|
|
|
|
const AUTH_REFRESH_TOKEN = "auth-refresh-token";
|
|
|
|
const AUTH_USER_ID = "auth-user-id";
|
|
|
|
const AUTH_USER_EMAIL = "auth-user-email";
|
|
|
|
const AUTH_USER_ACTOR_ID = "auth-user-actor-id";
|
|
|
|
const AUTH_USER_ROLE = "auth-user-role";
|
2019-10-12 11:16:36 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
const LOCAL_STORAGE_MEMORY = {};
|
2019-10-05 17:07:50 +00:00
|
|
|
|
|
|
|
Cypress.Commands.add("saveLocalStorage", () => {
|
2020-02-18 07:57:00 +00:00
|
|
|
Object.keys(localStorage).forEach((key) => {
|
|
|
|
LOCAL_STORAGE_MEMORY[key] = localStorage[key];
|
|
|
|
});
|
2019-10-05 17:07:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Cypress.Commands.add("restoreLocalStorage", () => {
|
2020-02-18 07:57:00 +00:00
|
|
|
Object.keys(LOCAL_STORAGE_MEMORY).forEach((key) => {
|
|
|
|
localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]);
|
|
|
|
});
|
2019-10-05 17:07:50 +00:00
|
|
|
});
|
|
|
|
|
2019-10-12 11:16:36 +00:00
|
|
|
Cypress.Commands.add("clearLocalStorage", () => {
|
2020-02-18 07:57:00 +00:00
|
|
|
Object.keys(LOCAL_STORAGE_MEMORY).forEach((key) => {
|
|
|
|
localStorage.removeItem(key);
|
|
|
|
});
|
2019-10-12 11:16:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Cypress.Commands.add("loginUser", () => {
|
2020-02-18 07:57:00 +00:00
|
|
|
console.log("Going to login an user");
|
|
|
|
const loginMutation = `
|
2019-10-12 11:16:36 +00:00
|
|
|
mutation Login($email: String!, $password: String!) {
|
2020-02-18 07:57:00 +00:00
|
|
|
login(email: $email, password: $password) {
|
|
|
|
accessToken,
|
|
|
|
refreshToken,
|
|
|
|
user {
|
|
|
|
id,
|
|
|
|
email,
|
|
|
|
role
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}`;
|
2019-10-12 11:16:36 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
const body = JSON.stringify({
|
|
|
|
operationName: "Login",
|
|
|
|
query: loginMutation,
|
|
|
|
variables: { email: "user@email.com", password: "some password" },
|
|
|
|
});
|
2019-10-12 11:16:36 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
cy.request({
|
|
|
|
url: "http://localhost:4000/api",
|
|
|
|
body,
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}).then((res) => {
|
|
|
|
console.log("Reply from server when logging-in", res);
|
|
|
|
const obj = res.body.data.login;
|
|
|
|
console.log("Login data: ", obj);
|
|
|
|
|
|
|
|
localStorage.setItem(AUTH_USER_ID, `${obj.user.id}`);
|
|
|
|
localStorage.setItem(AUTH_USER_EMAIL, obj.user.email);
|
|
|
|
localStorage.setItem(AUTH_USER_ROLE, obj.user.role);
|
|
|
|
|
|
|
|
localStorage.setItem(AUTH_USER_ACTOR_ID, `${obj.id}`);
|
|
|
|
localStorage.setItem(AUTH_ACCESS_TOKEN, obj.accessToken);
|
|
|
|
localStorage.setItem(AUTH_REFRESH_TOKEN, obj.refreshToken);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// const increaseFetches = () => {
|
|
|
|
// const count = Cypress.env('fetchCount') || 0;
|
|
|
|
// Cypress.env('fetchCount', count + 1);
|
|
|
|
// };
|
|
|
|
|
|
|
|
const decreaseFetches = () => {
|
|
|
|
const count = Cypress.env("fetchCount") || 0;
|
|
|
|
Cypress.env("fetchCount", count - 1);
|
|
|
|
};
|
|
|
|
|
2020-11-30 09:24:11 +00:00
|
|
|
const buildTrackableFetchWithSessionId = (fetch) => (
|
|
|
|
fetchUrl,
|
|
|
|
fetchOptions
|
|
|
|
) => {
|
2020-02-18 07:57:00 +00:00
|
|
|
const { headers } = fetchOptions;
|
|
|
|
const modifiedHeaders = {
|
|
|
|
"x-session-id": Cypress.env("sessionId"),
|
|
|
|
...headers,
|
|
|
|
};
|
|
|
|
|
|
|
|
const modifiedOptions = { ...fetchOptions, headers: modifiedHeaders };
|
2019-10-12 11:16:36 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
return fetch(fetchUrl, modifiedOptions)
|
|
|
|
.then((result) => {
|
|
|
|
decreaseFetches();
|
|
|
|
return Promise.resolve(result);
|
|
|
|
})
|
|
|
|
.catch((result) => {
|
|
|
|
decreaseFetches();
|
|
|
|
return Promise.reject(result);
|
2019-10-05 17:07:50 +00:00
|
|
|
});
|
2020-02-18 07:57:00 +00:00
|
|
|
};
|
2019-10-05 17:07:50 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
Cypress.on("window:before:load", (win) => {
|
|
|
|
cy.stub(win, "fetch", buildTrackableFetchWithSessionId(fetch));
|
2019-10-05 17:07:50 +00:00
|
|
|
});
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
Cypress.Commands.add("waitForFetches", () => {
|
|
|
|
if (Cypress.env("fetchCount") <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-05 17:07:50 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
cy.wait(100).then(() => cy.waitForFetches());
|
|
|
|
});
|
2019-10-05 17:07:50 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
Cypress.Commands.add("iframeLoaded", { prevSubject: "element" }, ($iframe) => {
|
|
|
|
const contentWindow = $iframe.prop("contentWindow");
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (contentWindow && contentWindow.document.readyState === "complete") {
|
|
|
|
resolve(contentWindow);
|
|
|
|
} else {
|
|
|
|
$iframe.on("load", () => {
|
|
|
|
resolve(contentWindow);
|
2019-10-05 17:07:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-02-18 07:57:00 +00:00
|
|
|
});
|
2019-10-05 17:07:50 +00:00
|
|
|
|
2020-11-30 09:24:11 +00:00
|
|
|
Cypress.Commands.add(
|
|
|
|
"getInDocument",
|
|
|
|
{ prevSubject: "document" },
|
|
|
|
(document, selector) => Cypress.$(selector, document)
|
2020-02-18 07:57:00 +00:00
|
|
|
);
|