【问题标题】:TestCafe how to reload actual pageTestCafe如何重新加载实际页面
【发布时间】:2019-10-03 01:15:53
【问题描述】:

有没有办法重新加载我在 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 来实际模拟重新加载测试页面?

【问题讨论】:

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


    【解决方案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)
    

    【讨论】:

    • 我们遇到了一个特定的错误,即在重新加载页面时,页面上会抛出错误。但是当我尝试通过 testcafe 重新加载页面时,我无法重现该错误。我猜当 testcafe 重新加载时,它会重新加载它用来获取我测试页面的 testcafe 页面。所以实际上,它就像我测试页面的另一个导航事件,而不是重新加载事件。有没有办法真正重新加载测试页面?
    • @ruazn2 使用或不使用 testcafe 重新加载页面应该没有区别。您能否提供更多信息:测试页面 URL、错误消息、命令行参数(例如 --skip-js-errors 参数可能是问题的根源)?
    【解决方案2】:

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

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

    然后在你的测试中,用myPage.refresh()之类的东西调用它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多