diff --git a/cypress.json b/cypress.json index f3b5e2cf7..68d5704a0 100644 --- a/cypress.json +++ b/cypress.json @@ -1,8 +1,5 @@ { "baseUrl": "http://localhost:9000", - "env": { - "API_URI": "https://pwa.demo.saleor.rocks/graphql/" - }, "defaultCommandTimeout": 15000, "requestTimeout": 15000, "viewportWidth": 1400, diff --git a/cypress/elements/account/left-menu/left-menu-selectors.js b/cypress/elements/account/left-menu/left-menu-selectors.js new file mode 100644 index 000000000..453e686ca --- /dev/null +++ b/cypress/elements/account/left-menu/left-menu-selectors.js @@ -0,0 +1,4 @@ +/* eslint-disable sort-keys */ +export const LEFT_MENU_SELECTORS = { + catalog: "[data-testid='catalogue']" +}; diff --git a/cypress/elements/account/login-selectors.js b/cypress/elements/account/login-selectors.js index 884b83dbd..388f6b458 100644 --- a/cypress/elements/account/login-selectors.js +++ b/cypress/elements/account/login-selectors.js @@ -1,7 +1,9 @@ export const LOGIN_SELECTORS = { + accountSettings: "[data-test=accountSettingsButton]", emailAddressInput: "input[name='email']", emailPasswordInput: "input[name='password']", signInButton: "[data-test=submit]", + userMenu: "[data-test=userMenu]", warningCredentialMessage: "[data-test=loginErrorMessage]", welcomePage: "[data-test=welcomeHeader]" }; diff --git a/cypress/elements/catalog/product-selectors.js b/cypress/elements/catalog/product-selectors.js new file mode 100644 index 000000000..aae2d0b9f --- /dev/null +++ b/cypress/elements/catalog/product-selectors.js @@ -0,0 +1,13 @@ +/* eslint-disable sort-keys */ +export const PRODUCTS_SELECTORS = { + products: "[href='/dashboard/products?']", + createProductBtn: "[data-test='add-product']", + productNameInput: "[name='name']", + productTypeInput: "[data-test='product-type']", + categoryInput: "[data-test='category']", + categoryItem: "[data-test='singleautocomplete-select-option']", + firstCategoryItem: "#downshift-0-item-0", + visibleRadioBtn: "[name='isPublished']", + saveBtn: "[data-test='button-bar-confirm']", + confirmationMsg: "[data-test='notification']" +}; diff --git a/cypress/integration/login_form.js b/cypress/integration/login_form.js index 812a852a5..e4af3a27a 100644 --- a/cypress/integration/login_form.js +++ b/cypress/integration/login_form.js @@ -6,37 +6,33 @@ describe("User authorization", () => { cy.clearSessionData(); }); - describe("Login", () => { - it("should successfully log in an user", () => { - cy.visit("/"); - cy.loginUser(); - cy.get(LOGIN_SELECTORS.welcomePage); - }); - - it("should fail for wrong password", () => { - cy.visit("/") - .get(LOGIN_SELECTORS.emailAddressInput) - .type("admin@example.com") - .get(LOGIN_SELECTORS.emailPasswordInput) - .type("wrong-password") - .get(LOGIN_SELECTORS.signInButton) - .click() - .get(LOGIN_SELECTORS.warningCredentialMessage); - }); + it("should successfully log in an user", () => { + cy.visit("/"); + cy.loginUser(); + cy.get(LOGIN_SELECTORS.welcomePage); }); - describe("Logout", () => { - it("should successfully log out an user", () => { - cy.window().then(win => { - win.sessionStorage.clear(); - }); - cy.visit("/"); - cy.loginUser(); - cy.get("[data-test=userMenu]") - .click() - .get("[data-test=accountSettingsButton]") - .click(); - cy.location("pathname").should("contains", "/staff/"); + it("should fail for wrong password", () => { + cy.visit("/") + .get(LOGIN_SELECTORS.emailAddressInput) + .type("admin@example.com") + .get(LOGIN_SELECTORS.emailPasswordInput) + .type("wrong-password") + .get(LOGIN_SELECTORS.signInButton) + .click() + .get(LOGIN_SELECTORS.warningCredentialMessage); + }); + + it("should successfully log out an user", () => { + cy.window().then(win => { + win.sessionStorage.clear(); }); + cy.visit("/"); + cy.loginUser(); + cy.get(LOGIN_SELECTORS.userMenu) + .click() + .get(LOGIN_SELECTORS.accountSettings) + .click(); + cy.location("pathname").should("contains", "/staff/"); }); }); diff --git a/cypress/integration/products.js b/cypress/integration/products.js new file mode 100644 index 000000000..b7a034c69 --- /dev/null +++ b/cypress/integration/products.js @@ -0,0 +1,43 @@ +import { LEFT_MENU_SELECTORS } from "../elements/account/left-menu/left-menu-selectors"; +import { PRODUCTS_SELECTORS } from "../elements/catalog/product-selectors"; + +// +describe("Products", () => { + beforeEach(() => { + cy.clearSessionData().loginUserViaRequest(); + }); + + it("should add new visible product", () => { + cy.visit("/") + .get(LEFT_MENU_SELECTORS.catalog) + .click() + .get(PRODUCTS_SELECTORS.products) + .click() + .get(PRODUCTS_SELECTORS.createProductBtn) + .click() + .get(PRODUCTS_SELECTORS.productNameInput) + .click() + .type("Visible test product") + .get(PRODUCTS_SELECTORS.productTypeInput) + .click() + .type("Cushion") + .get(PRODUCTS_SELECTORS.categoryItem) + .should("have.length", 1) + .get(PRODUCTS_SELECTORS.firstCategoryItem) + .click() + .get(PRODUCTS_SELECTORS.categoryInput) + .click() + .get(PRODUCTS_SELECTORS.categoryItem) + .first() + .click() + .get(PRODUCTS_SELECTORS.visibleRadioBtn) + .first() + .click() + .get(PRODUCTS_SELECTORS.saveBtn) + .click() + .get(PRODUCTS_SELECTORS.confirmationMsg, { + timeout: 1000 + }) + .contains("Product created"); + }); +}); diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js index d72976627..9c5d27c8c 100644 --- a/cypress/plugins/index.js +++ b/cypress/plugins/index.js @@ -16,6 +16,7 @@ * @type {Cypress.PluginConfig} */ module.exports = (on, config) => { - // `on` is used to hook into various events Cypress emits - // `config` is the resolved Cypress config + config.env.API_URI = process.env.API_URI; + + return config; }; diff --git a/cypress/support/user/index.js b/cypress/support/user/index.js index 4889d7d41..9edf99e27 100644 --- a/cypress/support/user/index.js +++ b/cypress/support/user/index.js @@ -1,3 +1,4 @@ +/* eslint-disable sort-keys */ import { LOGIN_SELECTORS } from "../../elements/account/login-selectors"; Cypress.Commands.add("loginUser", () => @@ -9,3 +10,39 @@ Cypress.Commands.add("loginUser", () => .get(LOGIN_SELECTORS.signInButton) .click() ); + +Cypress.Commands.add("loginUserViaRequest", () => { + const logInMutationQuery = `mutation TokenAuth($email: String!, $password: String!) { + tokenCreate(email: $email, password: $password) { + token + errors: accountErrors { + code + field + message + __typename + } + user { + id + __typename + } + __typename + } + }`; + + return cy + .request({ + method: "POST", + url: Cypress.env("API_URI"), + body: { + operationName: "TokenAuth", + variables: { + email: Cypress.env("USER_NAME"), + password: Cypress.env("USER_PASSWORD") + }, + query: logInMutationQuery + } + }) + .then(resp => { + window.sessionStorage.setItem("auth", resp.body.data.tokenCreate.token); + }); +});