【问题标题】:TestCafe persist data when clicking点击时TestCafe持久化数据
【发布时间】:2019-05-30 13:25:37
【问题描述】:

在 TestCafe 中,单击锚标记时,我会重定向到新页面和一个基本上将单击事件记录到对象的事件。

我想在TestCafe的对象中测试click事件的值,但是因为发生了重定向,所以对象丢失了。

我可以手动执行此操作,因为我可以在单击链接时按住 shift 并为重定向页面打开一个新窗口,但将原始页面保留在原始窗口中,然后在控制台中查看对象.

试图弄清楚是否有“模拟”点击的方法,但不进行重定向。或者,能够以某种方式在点击后立即断言,但在重定向之前?

小例子:

await t
.click('a') // here it already redirects so I'll lose the object
.expect(object).contains('value')

【问题讨论】:

    标签: javascript automated-tests e2e-testing web-testing testcafe


    【解决方案1】:

    以下测试展示了如何禁用和启用链接导航:

    import { Selector, ClientFunction } from 'testcafe';
    
    fixture `Navigation`
        .page `example.com`;
    
    const enableNavigationControl = ClientFunction(selector => {
        const element = selector();
    
        element.addEventListener('click', event => window.disableNavigation && event.preventDefault());
    });
    
    const disableNavigation = ClientFunction(() => window.disableNavigation = true);
    const enableNavigation  = ClientFunction(() => window.disableNavigation = false);
    
    test('navigation', async t => {
        const link = Selector('a');
    
        await enableNavigationControl(link);
        await disableNavigation();
    
        await t.click(link);
    
        // Perform assertions...
    
        await enableNavigation();
    
        await t.click(link);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      相关资源
      最近更新 更多