【问题标题】:How to click unrendered virtualized element in TestCafe如何在 TestCafe 中单击未渲染的虚拟化元素
【发布时间】:2020-04-09 05:21:55
【问题描述】:
我正在使用react-virtualized 来选择一个冗长的(1000 多个)项目列表。我正在尝试设置端到端测试,该测试需要单击当前未呈现的元素之一。
通常,我会简单地使用类似的东西:
await t.click(
ReactSelector('ListPage')
.findReact('ListItem')
.nth(873) // or .withText(...) or .withProps(...)
)
但是因为只呈现了ListItems 的一小部分,TestCafe 无法找到所需的元素。
我一直在尝试弄清楚如何使用 TestCafe 的 ClientFunction 滚动列表容器,以便呈现所需的 ListItem。
但是,我遇到了一些问题:
- 有没有办法将
Selector 共享到ClientFunction 并修改DOM 元素的scrollTop?还是我必须直接通过 DOM 重新查询元素?
- 由于
ListItems的高度不同,滚动位置不是简单计算索引x项的高度。如何在此功能中不断更新/滚动,直到所需的 Selector 可见?
【问题讨论】:
标签:
reactjs
typescript
automated-tests
testcafe
react-virtualized
【解决方案1】:
有没有办法将 Selector 共享到 ClientFunction 中并修改 DOM 元素的 scrollTop?
有一种方法可以将Selector 放入Client Function。请参考 TestCafe 文档中的this 示例。
如何在这个函数中不断更新/滚动直到所需的选择器可见?
您可以使用 TestCafe exist 属性来检查元素是否被渲染。以下示例演示了该方法:
import { Selector } from 'testcafe';
fixture`Getting Started`.page('https://bvaughn.github.io/react-virtualized/#/components/List')
test('Test 1', async t => {
const dynamicRowHeightsInput = Selector('._1oXCrgdVudv-QMFo7eQCLb');
const listItem = Selector('._113CIjCFcgg_BK6pEtLzCZ');
const targetItem = listItem.withExactText('Tisha Wurster');
await t.click(dynamicRowHeightsInput);
while (!await targetItem.exists) {
const currentLastRenderdItemIndex = await listItem.count -1;
const currentLastRenderdItemText = await listItem.nth(currentLastRenderdItemIndex).textContent;
const currentLastRenderdItem = await listItem.withExactText(currentLastRenderdItemText);
await t.hover(currentLastRenderdItem);
}
await t
.hover(targetItem)
.click(targetItem);
await t.debug();
});
为了滚动列表容器,我使用了hover 操作,最后呈现的listItem 作为目标元素。