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

4
在我的Cypress测试场景开始时,我需要点击下面IFrame中的同意并继续按钮: enter image description here 问题在于,在第一次测试运行期间,点击此按钮后,用户的同意以本地存储的形式存储,如下所示: storage 如果我再次运行测试,它将失败,因为本地存储正在存储用户的同意,因此不会出现要求同意的IFrame。
如果我手动清除本地存储中的truste.consent.willistowerswatson.com键并重新运行测试,则测试将通过。
我需要以某种方式自动清除此值。
我尝试使用以下代码指定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()
})

有人能解释一下我如何清除本地存储中的值,这样我的测试就可以重新运行而没有任何问题吗?
1个回答

2

您可以通过运行以下命令来删除此特定密钥:

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

或者,如果需要,您可以按如下方式删除整个localStorage:

localStorage.clear();

你还可以在MDN Web文档中找到更多信息,链接如下:https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接