gpt4 book ai didi

javascript - 如何清除 Cypress 测试中的本地存储?

转载 作者:行者123 更新时间:2023-12-05 04:36:42 25 4
gpt4 key购买 nike

在我的 Cypress 测试场景开始时,我需要单击以下 IFrame 上的同意并继续按钮:

enter image description here

问题是,在我的第一次测试运行期间单击此按钮后,用户的同意存储在本地存储中,如您所见:

storage

如果我再次重新运行我的测试,它将失败,因为本地存储正在存储用户的同意,因此不会出现请求同意的 IFrame。

如果我从本地存储中手动清除 truste.consent.willistowerswatson.com key 并重新运行我的测试,它将通过。

我需要以某种方式自动清除这个值。

我试图用下面的代码指定 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()
})

有人可以解释我如何从本地存储中清除这个值,以便我的测试可以毫无问题地重新运行吗?

最佳答案

您可以通过运行删除此特定 key :

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

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

localStorage.clear();

您还可以在此处的 MDN Web 文档中找到更多信息:https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

关于javascript - 如何清除 Cypress 测试中的本地存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70777091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com