e2e-testing - TestCafe页面初始化前如何设置本地存储

标签 e2e-testing testcafe

在 Testcafe 初始化页面之前,我需要设置本地存储以绕过登录屏幕。我尝试注入(inject)自定义脚本,但这不起作用。

有人可以帮我怎么做吗?

最佳答案

要在 TestCafe session 中设置本地存储变量,您可以使用 ClientFunction's .您可以在 beforeEach Hook 中使用您的 key 设置本地存储变量,以便在您的每个测试之前调用它。实现此目的的代码可能如下所示:

const setLocalStorageItem = ClientFunction((key: string, value: string) => window.localStorage.setItem(key, value));

fixture(`My tests`)
  .page("https://www.my-test-url.com")
  .beforeEach(async (t) => {
    await setLocalStorageItem("myLocalStorageKey", "myValue");
  });

test('Some test', async (t) => {
  // This assertion should be fine for every test
  await t.expect(await getLocalStorageItem("myLocalStorageKey")).eql("myValue");

  // more test code should follow here
})

随着 TestCafe 清除 localStorage and sessionStorage after each test ,如果您想在每个测试中访问相同的本地存储键/值对(或者由于某种原因需要为每个测试设置它),则需要这样做。

如果您只想为特定测试设置本地存储条目,您可以进行如下操作:

test('Some other test', async (t) => {

  // my test code
}).before(async (t) => {
  await setLocalStorageItem("testKey", "testValue");
});

关于e2e-testing - TestCafe页面初始化前如何设置本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66908694/

相关文章:

javascript - 如何将参数传递给 --world-parameters 或 npm run 命令以供包中的脚本使用

node.js - 是否可以使用 Testcafe 检查 E2E 测试中预期的颜色代码?

javascript - 按名称的特定部分搜索元素

javascript - Protractor 测试: options to force refresh when URL hash changes

javascript - 是否可以在 Express.js 中运行 TestCafe 测试?

docker - 如何从远程 selenium 网格上的 docker 运行 testcafe

testing - 如何设置 TestCafe 以在 CLI 中指定的环境中运行?

testing - 如何获取 testCafe 退出代码

angularjs - Protractor E2E 使用 ng-file-upload 测试多个文件上传

typescript - Testcafe Docker - 错误 : TypeScript compilation failed. 找不到名称 'require'