【问题标题】:TestCafe ClientFunction TypeError error as document is undefinedTestCafe ClientFunction TypeError 错误,因为文档未定义
【发布时间】:2019-08-02 13:57:13
【问题描述】:

我可以成功执行下面的.click:

const clickMockContinueButton = ClientFunction(() => document.getElementsByName("paymentGatewayIframeReturnSubmit")[0].click())

通过调用它:

await clickMockContinueButton();

但即使在成功后 .click 我得到一个 TypeError:

An error occurred in ClientFunction code:

TypeError: document.getElementsByName(...)[0] is undefined

我在这里做错了什么?

* 使用解决方法编辑 *

当我使用 TC .click(Selector) 或 document.getElementsByName().click() 时,我的模拟页面中似乎有一个错误,因为该操作被执行了两次,因此第二次单击尝试会引发错误因为按钮不存在了。

所以我决定继续并使用一个简单的解决方法:

async function handleMockContinueButton() {
    var focus = ClientFunction(() => {
        document.getElementsByName("paymentGatewayIframeReturnSubmit")[0].focus();
    });

    await focus();
    await t.pressKey("enter");
};

【问题讨论】:

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


    【解决方案1】:

    我无法使用您的信息重现“TypeError”问题。我的test.js

    import { Selector, ClientFunction } from 'testcafe';
    
    fixture `My fixture`
        .page `https://google.com/`;
    
    const clickInput = ClientFunction(() => document.getElementsByName('q')[0].click())
    
    test('test', async t => {
        await clickInput(); 
    });
    

    TestCafe 版本:1.3.3

    命令:testcafe chrome test.js

    结果:

    Running tests in:
    - Chrome 75.0.3770 / Windows 10.0.0
    
    My fixture
    √ test
    
    
    1 passed (3s)
    

    我建议你在测试中使用t.click( selector [, options] )

    【讨论】:

    • 自从我们引入了这个模拟修复程序后,就会出现这个问题:github.com/DevExpress/testcafe-hammerhead/issues/2012。无论我使用 TC t.click() 还是 document.getElementsByName("element")[0].click(),按钮上的所有点击操作都会被复制。该按钮总是单击一次,然后第二次尝试单击会引发错误,因为该按钮不再存在,即指定的选择器与 DOM 树中的任何元素都不匹配...使用 TC t.click() 我已经不知道为什么会在 HTML 模拟页面中发生这种情况,或者它是否与 TestCafe 完全无关!
    • 我认为现在我需要弄清楚如何在这种情况下抑制错误。
    • catch(e) 错误日志:{ code: 'E4', isTestCafeError: true, callsite: CallsiteRecord { filename: '/my-account-payment-cards.js', lineNum: 66, callsiteFrameIdx : 3, stackFrames: [[CallSite], [CallSite], [CallSite], [CallSite], CallSite {}, [CallSite], [CallSite], [CallSite], [CallSite] ], isV8Frames: true }, errMsg: 'TypeError: document.getElementsByName(...)[0] is undefined', instantiationCallsiteName: 'ClientFunction', isRejectedDriverTask: true }
    • 如果您尝试获取位于<iframe> 中的元素,则需要使用switchToIframe TestCafe 操作将测试的浏览上下文从主窗口更改为<iframe>。否则,getElementsByName 方法将返回空的NodeList。请检查您是否使用了正确的浏览上下文。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 2022-10-17
    • 2023-02-02
    相关资源
    最近更新 更多