javascript - TestCafe - 将选择器的结果存储在变量中

标签 javascript node.js automated-tests e2e-testing testcafe

所以为了测试,我的搜索结果根据我输入的关键字而不同,我想在输入关键字之前存储搜索结果的 Node 列表,然后将它们与添加关键字,但我无法让它工作。

我试过:

let results = await Selector('#example')

但是,这并没有给我返回一个 Node 列表。 我还尝试仅将 clientFunction 与 document.querySelectorAll() 一起使用,但 TestCafe 然后告诉我改用 Selector。

要做什么?是否有更好的方法来测试这个,我没有看到?

最佳答案

您可以提取所有需要的属性以供以后比较。

检查这个小例子:

index.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);
});

关于javascript - TestCafe - 将选择器的结果存储在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53501761/

相关文章:

javascript - 将 javascript mousedown/mouseup/mousemove/keypress 监听器移植到移动设备时应该注意什么?

javascript - Webtask.io 后端找不到模块

node.js - 在 Node 中创建 Mongoose 模型的新实例 - 将第一项添加到其中一个嵌入数组

testing - 如何在部署到 kubernetes 之前测试我的微服务?

javascript - 在 Jasmine 中测试成功后如何记录规范?

javascript - 用数组填充下拉列表

javascript - 如何在移动设备上使用setTimeout播放音频?

javascript - jQuery fadeTo() 用于颜色

javascript - Node.js 网络抓取,在 URL 数组上循环

android - 如何在 calabash-android 中将应用程序带到前台?