1
guys. I am very fresh in Cypress. Would like to get your help/advice: 1.For login I used below-mentioned way but I get error of: "cy.saveLocalStorage is not a functionBecause this error occurred during a after each hook we are skipping the remaining tests in the current suite: LoginFunctionality"
And I do not know where to store my credentials. Support->Command.js
let LOCAL_STORAGE_MEMORY = {};
Cypress.Commands.add('saveLocalStorage', () => {
Object.keys(localStorage).forEach(key => {
LOCAL_STORAGE_MEMORY[key] = localStorage[key]
})
})
Cypress.Commands.add('restoreLocalStorage', () => {
Object.keys(LOCAL_STORAGE_MEMORY).forEach(key => {
localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key])
})
})
Cypress.Commands.add('visitMultipleURL', url => {
cy.window().then(win => {
return win.open(url, '_self')
})
})
import 'cypress-iframe';
In login.spec.js
describe('LoginFunctionality', function () {
beforeEach(() =>{
cy.restoreLocalStorage();
Cypress.Cookies.defaults({
preserve: /[\s\S]*/,
})
})//
afterEach(() =>{
cy.saveLocalStorage();
})
let href;
it.only('LoginFunctionality', function () {
//cy.visit(Cypress.env('url'))
cy.visit('xxx')
cy.get('.chakra-stack > .css-1n94901').click()
cy.contains('Login with xxxx').click()
cy.url().then((url) => {
href = url;
cy.log('href ', href);
})
})
it.only('LoginFunctionality', function () {
cy.visit(href)
cy.url().should('include','xxxxx[![enter image description here][1]][1]')//assertion of that we are in this url
})
})
Something similar happened to me before.
I suggest you debug it by trying the code directly in your test first to check what's happening.
Also, do a console log in LOCAL_STORAGE_MEMORY and see if you have access to its value because cypress does not handle variables the way we would expect it if this was pure javascript (even when it might seem as if it's taking the value)
try
let LOCAL_STORAGE_MEMORY = {anyAttribute: "VALUE"};
To see if you get that attribute from inside the command
Usually that's what my problem was related to.