The following facts:
To step 3: If I only use the command 'cy.visit('X);' then Cypress clears the cache and my customisations are lost.
I know that there is a command 'Cypress.LocalStorage', but I don't know how exactly to use it for my case.
Thanks for your help!
I believe you can visit your page, set the local storage value, and then validate the checkbox. Roughly, it would look like:
cy.visit('/projects');
cy.window().then((window) => {
window.localStorage.setItem('myLocalStorageItem', value);
})
cy.VerifyCheckBox('A', 'checked');
It doesn't mimic an exact user's workflow, but it works the same overall -- the checkbox is checked when a localStorage item is set to some value.
I would follow the pattern that @agoff uses, but use window:before:load event.
That way will catch any regression that clears the value during page load.
// verify that CheckboxA sets local storage
cy.visit('/projects')
cy.checkCheckbox('A')
cy.window().then((window) => {
expect(window.localStorage.getItem('myLocalStorageItem')).to.eq(value)
})
// verify that reload retains localStorage value
cy.on('window:before:load', (win) => {
// should fire after Cypress clears localStorage
window.localStorage.setItem('myLocalStorageItem', value)
})
cy.reload()
cy.VerifyCheckBox('A', 'checked')
I have found a second solution for my problem. I added at the beginning of my test case (in describe, bevor it) this:
beforeEach(() => {
cy.restoreLocalStorage().then(() => {
cy.log('Storage restored');
})
});
afterEach(() => {
cy.saveLocalStorage().then(() => {
cy.log('Storage saved');
})
});
I hope I can help you with it! Let me know it!