简体   繁体   English

如何在 Cypress 测试中清除本地存储?

[英]How to clear local storage in Cypress test?

At the beginning of my Cypress test scenario, I need to click the Agree and Proceed button on the below IFrame:在我的赛普拉斯测试场景开始时,我需要单击下面 IFrame 上的同意并继续按钮:

在此处输入图像描述

The problem is that after clicking this button during my first test run, the user's consent is stored in the local storage as you can see here:问题是在我第一次测试运行期间单击此按钮后,用户的同意存储在本地存储中,如您在此处看到的:

贮存

If I re-run my test again, it will fail because the local storage is storing the user's consent, so the IFrame asking for consent won't appear.如果我再次重新运行测试,它会失败,因为本地存储正在存储用户的同意,因此请求同意的 IFrame 不会出现。

If I manually clear the truste.consent.willistowerswatson.com key from local storage and re-run my test, it will pass.如果我从本地存储中手动清除truste.consent.willistowerswatson.com密钥并重新运行我的测试,它将通过。

I need to somehow automatically clear this value.我需要以某种方式自动清除此值。

I tried to specify the key value with the below code, but it isn't clearing the value:我试图用下面的代码指定key ,但它没有清除值:

beforeEach(() => {
    cy.clearLocalStorage('truste.consent.willistowerswatson.com')
})

it('First test', () => {
    cy.visit('http://www.willistowerswatson.com/ICT');
    cy.iframe('[id^=pop-frame]').find('.call').should('have.text', 'Agree and Proceed').click()
})

Can someone explain how I can clear this value from local storage, so my test can re-run without any issue?有人可以解释我如何从本地存储中清除此值,以便我的测试可以重新运行而不会出现任何问题吗?

You can remove this specific key by running:您可以通过运行以下命令删除此特定密钥:

localStorage.removeItem(‘truste.consent.willistowerswatson.com‘);

Alternatively, if desired, you can delete the entire localStorage as follows:或者,如果需要,您可以删除整个 localStorage,如下所示:

localStorage.clear();

You also find more info in the MDN Web Docs here: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage您还可以在此处的 MDN Web 文档中找到更多信息: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM