javascript - TestCafe - 将 Selector 的结果存储在变量中
问题描述
所以为了测试,我的搜索结果根据我输入的关键字而有所不同,我想在输入关键字之前存储 searchResults 的节点列表,然后将它们与添加关键字后得到的 searchResults 的节点列表进行比较,但是我无法让它工作。
我试过了:
let results = await Selector('#example')
但是,这并没有给我一个节点列表。我还尝试只使用带有 a 的 clientFunction,document.querySelectorAll()
但 TestCafe 然后告诉我使用 Selector 代替。
该怎么办?有没有更好的方法来测试这个,我看不到?
解决方案
您可以提取所需的所有属性以供以后比较。
检查这个小例子:
索引.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function removeSpanId3 () {
const span = document.getElementById('id3');
document.querySelector('div').removeChild(span);
}
</script>
<button id="removeSpan" onclick="removeSpanId3()">Remove span</button>
<div>
<span id="id1">
test1
</span>
<span id="id2">
test12
</span>
<span id="id3">
test123
</span>
<span id="id4">
none
</span>
</div>
测试.js:
import { Selector } from 'testcafe';
fixture `test`
.page('http://localhost:8080');
test('Test1', async t => {
const results = await Selector('span');
const resultsCount1 = await Selector('span').count;
const result1 = [];
const result2 = [];
for (let i = 0; i < resultsCount1; i++) {
const text = await results.nth(i).innerText;
result1.push(text);
}
// Remove span
await t.click(Selector('button').withText('Remove span'));
const resultsCount2 = await Selector('span').count;
for (let i = 0; i < resultsCount2; i++) {
const text = await results.nth(i).innerText;
result2.push(text);
}
await t
.expect(result1.length).eql(result2.length + 1);
});
推荐阅读
- css - 在第一次加载时,FireFox 使 css 变得混乱
- python - 导入问题python模块
- mysql - 外键创建生成的 Mariadb 索引不“工作”
- crystal-reports - Crystal Reports SUM 公式帮助。不要对具有特定 ID 的值求和
- iis - 在 IIS 中复制 mod-proxy 行为
- c++ - 通过继承和函数指针绕过保护
- opengl - 关于OpenGL图像格式,GL驱动在将像素传输到GPU时是做什么的
- javascript - 将 props 从另一个组件传递给一个组件,React Router v4
- amazon-web-services - 为 aws 函数配置输出队列
- javascript - 在这个函数中为什么首先打印“ho”而不是“hello”