From 3485b49287c79a4c8bedd139f7e580f9583651b5 Mon Sep 17 00:00:00 2001 From: Gregor Pollak Date: Sun, 16 Jul 2023 12:00:42 +0200 Subject: [PATCH] Refactor luma account tests --- cypress/integration/luma/user/account.spec.js | 126 +++++++++++------- cypress/page-objects/luma/account.js | 28 +++- cypress/support/magento2-rest-api.js | 24 ++++ 3 files changed, 128 insertions(+), 50 deletions(-) diff --git a/cypress/integration/luma/user/account.spec.js b/cypress/integration/luma/user/account.spec.js index 678b812..6f57530 100644 --- a/cypress/integration/luma/user/account.spec.js +++ b/cypress/integration/luma/user/account.spec.js @@ -10,43 +10,50 @@ import {isMobile} from "../../../support/utils"; describe('Account test creation', () => { it('Can create an account', () => { cy.visit(account.routes.accountCreate) - Account.createNewCustomer(account.customer.customer.firstname, account.customer.customer.lastname, Date.now() + account.customer.customer.email, account.customer.password) + Account.createNewCustomer( + account.customer.customer.firstname, + account.customer.customer.lastname, + Date.now() + account.customer.customer.email, account.customer.password + ) cy.contains('Thank you for registering with Main Website Store.').should('exist') cy.wait(2000) Account.logout() }) }) +describe('Account login/logout', () => { + before(() => { + Magento2RestApi.createCustomerAccount(account.customer) + Account.login(account.customer.customer.email, account.customer.password) + }) + + // TODO: Create login test without using Account.login ( clicking the "Sign In" in the Header and then doing login.) + + it('Can log out', () => { + cy.get('.base').should('contain.text', 'My Account') + cy.get(selectorsLuma.accountIcon).click({force: true}) + cy.get(selectorsLuma.accountMenuItems).contains('Sign Out').click({force: true}) + cy.get(selectorsLuma.signedOutHeader).should('contain.text', 'You are signed out') + cy.wait(2000) + }) +}) + describe('Account activities', () => { beforeEach(() => { + Account.login(account.customer.customer.email, account.customer.password) cy.wait(2500) }) before(() => { cy.wait(2500) Magento2RestApi.createCustomerAccount(account.customer) - Account.login(account.customer.customer.email, account.customer.password) - Account.createAddress(account.customerInfo) - // We need to logout or the beforeEach will fail + if(isMobile()) { cy.wait(2000) } cy.wait(2500) }) - after(() => { - // Remove the added address - cy.wait(4000) - Account.login(account.customer.customer.email, account.customer.password) - cy.visit('/customer/address') - cy.wait(4000) - cy.get('.additional-addresses a.delete').eq(0).click({force: true}) - cy.wait(1000) - cy.get('.modal-content').contains('Are you sure you want to delete this address?') - cy.get('.action-primary').click() - cy.wait(2500) - }) - it('Can check your profile', () => { cy.visit(account.routes.accountEdit) Account.checkAllProfileSpecs() @@ -62,9 +69,11 @@ describe('Account activities', () => { Account.login(account.customer.customer.email, account.tempCustomerInfo.password) cy.visit(account.routes.accountEdit) // Change password back to normal - cy.get('#change-password').check({force: true}) - Account.changePassword(account.tempCustomerInfo.password, account.customer.password) - cy.contains('You saved the account information.').should('exist') + Magento2RestApi.replacePassword( + account.customer.customer.email, + account.tempCustomerInfo.password, + account.customer.password + ) }) it('Can change the profile values', () => { @@ -103,10 +112,48 @@ describe('Account activities', () => { }) }) - it('Can add an address', () => { - cy.visit(account.routes.accountAddAddress) - Account.createAddress(account.customerInfo) + it('Can change the newsletter subscription', () => { + Account.login(account.customer.customer.email, account.customer.password) + cy.visit(account.routes.manageNewsletter) + cy.contains('General Subscription') + cy.get('#subscription').should('be.checked') + }) + + it('Can add a product the a wishlist', () => { + cy.visit(product.simpleProductUrl) cy.wait(2000) + cy.get(selectorsLuma.addToWishlistButton).eq(0).click({force: true}) + cy.get(selectorsLuma.wishlistTitle).should('contain.text', 'My Wish List').should('exist') + cy.visit(account.routes.wishlist).then(() => { + cy.get('.toolbar-number').should('exist') + cy.contains(product.simpleProductName).should('exist') + }) + }) + + +}) + +describe('Account Address Activities', () => { + beforeEach(() => { + Account.login(account.customer.customer.email, account.customer.password) + cy.wait(1500) + }) + + before(() => { + Magento2RestApi.createCustomerAccount(account.customer) + if(isMobile()) { + cy.wait(2000) + } + cy.wait(2500) + }) + + after(() => { + cy.wait(4000) + Account.login(account.customer.customer.email, account.customer.password) + Account.deleteAddress() + }) + + it('Can add an address', () => { cy.contains(selectorsLuma.addNewAddressButton, 'Add New Address').click() cy.get(selectorsLuma.newAddressStreetInput).type(account.customerInfo.streetAddress) cy.get(selectorsLuma.newAddressCityInput).type(account.customerInfo.city) @@ -114,12 +161,18 @@ describe('Account activities', () => { cy.get(selectorsLuma.newAddressZipcodeInput).type(account.customerInfo.zip) cy.get(selectorsLuma.newAddressCountryInput).select(account.customerInfo.country) cy.contains('Save Address').click() + + cy.get('.messages') + .should('contain.text', 'You saved the address') }) it('Can change an address', () => { - const timeStamp = Date.now().toString() + // TODO: Don't go directly to Addresses page, go to /customer/account/index and click on "Manage Addresses" + cy.visit(account.routes.accountAddresses) cy.get(selectorsLuma.editAddress).first().click() + + const timeStamp = Date.now().toString() cy.get(selectorsLuma.addressEditStreetInput).eq(0).type(timeStamp) cy.get(selectorsLuma.saveAddressButton).contains('Save Address').click() cy.contains('You saved the address.').should('exist') @@ -150,31 +203,6 @@ describe('Account activities', () => { cy.get('.action-primary').click() cy.contains('You deleted the address.').should('exist') }) - - it('Can change the newsletter subscription', () => { - Account.login(account.customer.customer.email, account.customer.password) - cy.visit(account.routes.manageNewsletter) - cy.contains('General Subscription') - cy.get('#subscription').should('be.checked') - }) - - it('Can add a product the a wishlist', () => { - cy.visit(product.simpleProductUrl) - cy.wait(2000) - cy.get(selectorsLuma.addToWishlistButton).eq(0).click({force: true}) - cy.get(selectorsLuma.wishlistTitle).should('contain.text', 'My Wish List').should('exist') - cy.visit(account.routes.wishlist).then(() => { - cy.get('.toolbar-number').should('exist') - cy.contains(product.simpleProductName).should('exist') - }) - }) - - it('Can log out', () => { - cy.get(selectorsLuma.accountIcon).click({force: true}) - cy.get(selectorsLuma.accountMenuItems).contains('Sign Out').click({force: true}) - cy.get(selectorsLuma.signedOutHeader).should('contain.text', 'You are signed out') - cy.wait(2000) - }) }) describe('Guest user test', () => { diff --git a/cypress/page-objects/luma/account.js b/cypress/page-objects/luma/account.js index eb79501..2a8478c 100644 --- a/cypress/page-objects/luma/account.js +++ b/cypress/page-objects/luma/account.js @@ -12,7 +12,7 @@ export class Account { static isLoggedIn() { cy.contains(selectors.myAccountHeaderSelector, 'My Account') } - + static logout() { cy.visit(account.routes.accountIndex); cy.get('.base').then(($text) => { @@ -85,6 +85,32 @@ export class Account { }) } + static deleteAddress() { + cy.visit(account.routes.accountAddresses) + cy.wait(2000) + + cy.get('.block-addresses-list').then($block => { + cy.log($block.find('.empty').length) + if ($block.find('.empty').length === 1) { + cy.log('No more addresses to remove'); + } else { + // TODO: Replace this with REST API call at some point? + cy.get('.additional-addresses a.delete').then(($links) => { + if ($links.length > 0) { + cy.wrap($links.first()).click({force: true}); + cy.wait(2000) + cy.get('.modal-content').then(($modal) => { + if ($modal.text().indexOf('Are you sure you want to delete this address?') >= 0) { + cy.get('.action-primary').click() + } + this.deleteAddress(); // calling as long as we have addresses to remove! + }) + } + }) + } + }) + } + static addItemToWishlist(itemUrl = '') { cy.visit(itemUrl) cy.get('button[aria-label="Add to Wish List"]').click() diff --git a/cypress/support/magento2-rest-api.js b/cypress/support/magento2-rest-api.js index f5a3d6a..efb1bff 100644 --- a/cypress/support/magento2-rest-api.js +++ b/cypress/support/magento2-rest-api.js @@ -20,6 +20,30 @@ export class Magento2RestApi { }); } + static replacePassword(username, oldPassword, newPassword) { + cy.request({ + method: 'POST', + url: '/rest/all/V1/integration/customer/token', + body: { + "username": username, + "password": oldPassword + }, + }).then((response) => { + let token = response.body + cy.request({ + method: 'PUT', + url: '/rest/default/V1/customers/me/password', + headers: { + Authorization: "Bearer " + token + }, + body: { + "currentPassword": oldPassword, + "newPassword": newPassword + } + }) + }) + } + static logCustomerIn(customer) { cy.request({ method: 'POST',