首页 > 解决方案 > Testcafe:如何将文本输入到光谱颜色选择器的隐藏输入中

问题描述

我正在尝试使用 将颜色代码发送到Spectrum 颜色选择器输入t.typeText(colorPickerInput),但此输入被隐藏并且 Testcafe 抛出错误。基于文档选项{ visibilityCheck: false }在以下情况下不起作用typeText()

<input id="ember949" class="spectrum-color-picker ember-view" style="display: none;">

有没有其他方法来处理这个?

标签: testingautomated-testsui-automationtestcafecolor-picker

解决方案


TestCafe 旨在模拟真实的用户行为,因此您不能使用“display: none”样式在输入元素中键入内容。但是,您可以使用ClientFunctions机制设置输入值,该机制允许您编写任何自定义 JS 代码。

请看下面的例子:

import { ClientFunction } from 'testcafe';

const setInputValue = ClientFunction(() => {
    document.querySelector('input.spectrum-color-picker').value = '';
});

fixture `fixture`
    .page `http://example.com`;

test('test', async t => {
    await setInputValue();
});

推荐阅读