testcafe如何重新加载实际页面

时间:2019-05-15 20:20:40

标签: automated-tests refresh reload e2e-testing testcafe

是否可以重新加载我在TestCafe中访问的实际测试页面,而不是重新加载运行TestCafe的网站。我尝试使用:

await t.eval(() => location.reload(true));

但这只是重新加载Testcafe使用的服务器页面。因此,例如,如果我测试www.google.com,则启动TestCafe后浏览器中的URL将类似于http://172.16.0.152:58486/Qf6eMxOvA/https:/www.google.com/ 当我执行上面的代码时,该站点将重新加载。有没有一种方法可以强制仅重新加载www.google.com来模拟重新加载测试页?

2 个答案:

答案 0 :(得分:1)

await t.eval(() => location.reload(true));

是的,您应该使用上面提供的代码重新加载测试页。

请签出following example。该示例按预期方式工作:重新加载页面后,我们会检查本地存储值。

test.js:

import { ClientFunction } from 'testcafe';

fixture `fixture`
  .page `http://devexpress.github.io/testcafe/example`;

const getLocalStorageItem = ClientFunction(key => localStorage.getItem(key));

test('local storage', async t => {
  await t.eval(() => localStorage.setItem('key', 'value'));
  await t.expect(getLocalStorageItem('key')).eql('value');
  await t.wait(2000);
  await t.eval(() => location.reload(true));
  await t.wait(2000);
  await t.expect(getLocalStorageItem('key')).eql('value');
});

结果:

Running tests in:
- Chrome 74.0.3729 / Windows 10.0.0

fixture
√ local storage


1 passed (9s)

答案 1 :(得分:0)

尽量避免使用 evalwait。请改用 Testcafe's ClientFunction。我在我的基页对象中使用了这样的东西:

  async refresh () {
    await ClientFunction(() => {
      document.location.reload();
    })();
  }

然后在您的测试中,使用类似 myPage.refresh()