Javascript TestCafe ClientFunction类型错误,因为文档未定义
我可以成功执行以下操作。单击:Javascript TestCafe ClientFunction类型错误,因为文档未定义,javascript,testing,automated-tests,e2e-testing,testcafe,Javascript,Testing,Automated Tests,E2e Testing,Testcafe,我可以成功执行以下操作。单击: const clickMockContinueButton = ClientFunction(() => document.getElementsByName("paymentGatewayIframeReturnSubmit")[0].click()) 通过以下方式调用: await clickMockContinueButton(); 但即使在成功单击后,我也会得到一个类型错误: An error occurred in ClientFunction
const clickMockContinueButton = ClientFunction(() => document.getElementsByName("paymentGatewayIframeReturnSubmit")[0].click())
通过以下方式调用:
await clickMockContinueButton();
但即使在成功单击后,我也会得到一个类型错误:
An error occurred in ClientFunction code:
TypeError: document.getElementsByName(...)[0] is undefined
我做错了什么
*使用变通方法进行编辑*
当我使用TC.click(选择器)或document.getElementsByName().click()时,我的模拟页面中似乎有一个错误,因为该操作执行了两次,因此第二次单击尝试会抛出一个错误,因为该按钮不再存在
因此,我决定继续使用一个简单的解决方法:
async function handleMockContinueButton() {
var focus = ClientFunction(() => {
document.getElementsByName("paymentGatewayIframeReturnSubmit")[0].focus();
});
await focus();
await t.pressKey("enter");
};
我无法用您的信息重现“TypeError
”问题。Mytest.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)
我建议您在测试中使用。我无法在您的信息中重现“类型错误”
”问题。Mytest.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)
我建议您在测试中使用。自从我们引入此模拟修复程序以来,出现了此问题:。无论我使用的是TC t.click()还是document.getElementsByName(“元素”)[0]。都将复制按钮上的所有单击操作。该按钮始终单击一次,然后第二次尝试单击会抛出错误,因为该按钮不再存在,即指定的选择器与DOM树中的任何元素都不匹配。。。使用TC t.click(),我不知道为什么会在out HTML模拟页面中发生这种情况,也不知道它是否与TestCafe完全无关!我想现在我需要弄清楚如何在这个场景中抑制错误。catch(e)错误日志:{code:'E4',isTestCafeError:true,callsite:callsitecord{filename:'/my account payment cards.js',lineNum:66,callsiteFrameIdx:3,stackFrames:[[callsite],[callsite],[callsite],[CallSite],[CallSite{},[CallSite],[CallSite],[CallSite],[CallSite],[CallSite],[CallSite],[CallSite]],isV8Frames:true},errMsg:'TypeError:document.getElementsByName(…)[0]未定义',实例化CallSiteName:'ClientFunction',isRejectedDriverTask:true}如果试图获取
中的元素,则需要使用TestCafe操作将测试的浏览上下文从主窗口更改为此
。否则,getElementsByName
方法将返回空的NodeList
。请检查您是否使用了正确的浏览上下文。自从我们引入此模拟修复程序以来,出现了此问题:。无论我使用的是TC t.click()还是document.getElementsByName(“元素”)[0]。都将复制按钮上的所有单击操作。该按钮始终单击一次,然后第二次尝试单击会抛出错误,因为该按钮不再存在,即指定的选择器与DOM树中的任何元素都不匹配。。。使用TC t.click(),我不知道为什么会在out HTML模拟页面中发生这种情况,也不知道它是否与TestCafe完全无关!我想现在我需要弄清楚如何在这个场景中抑制错误。catch(e)错误日志:{code:'E4',isTestCafeError:true,callsite:callsitecord{filename:'/my account payment cards.js',lineNum:66,callsiteFrameIdx:3,stackFrames:[[callsite],[callsite],[callsite],[CallSite],[CallSite{},[CallSite],[CallSite],[CallSite],[CallSite],[CallSite],[CallSite],[CallSite]],isV8Frames:true},errMsg:'TypeError:document.getElementsByName(…)[0]未定义',实例化CallSiteName:'ClientFunction',isRejectedDriverTask:true}如果试图获取
中的元素,则需要使用TestCafe操作将测试的浏览上下文从主窗口更改为此
。否则,getElementsByName
方法将返回空的NodeList
。请检查您是否使用了正确的浏览上下文。