TestCafe ClientFunction TypeError error as document is undefined

妖精的绣舞 提交于 2019-12-11 13:41:52

问题


I can successfully execute the following .click:

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

by calling it with:

await clickMockContinueButton();

But even after the successful .click I get a TypeError:

An error occurred in ClientFunction code:

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

What am I doing wrong here?

* EDIT with workaround *

Seems there is a bug in my mocked page when I use either TC .click(Selector) or document.getElementsByName().click(), in that the action is being performed twice, therefore the 2nd click attempt throws an error because the button doesn't exist anymore.

So I decided to move on and use a simple workaround as:

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

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

回答1:


I can't reproduce the "TypeError" issue with your information. My 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 version: 1.3.3

Command: testcafe chrome test.js

Result:

Running tests in:
- Chrome 75.0.3770 / Windows 10.0.0

My fixture
√ test


1 passed (3s)

I suggest you use t.click( selector [, options] ) in your test.



来源:https://stackoverflow.com/questions/57327950/testcafe-clientfunction-typeerror-error-as-document-is-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!