javascript - 如何在 Test Cafe 中使用 Javascript 将表作为数组返回
问题描述
我正在尝试验证整个表格元素并希望将表格作为数组返回,然后想要访问每个元素..请帮助。
解决方案
请看以下示例,该示例演示了如何使用 TestCafe 检查表状态:
import { Selector, ClientFunction } from 'testcafe';
fixture `fixture`
.page `https://jsfiddle.net/gv69jqrx/1/`;
test('Obtain array', async t => {
await t
.switchToIframe(Selector('#editor').find('[name="result"]'));
const getCountries = ClientFunction(() => {
const countries = [];
const columnKeys = ['country', 'capital', 'population', 'language'];
const rows = document.getElementsByTagName('tr');
for (let i = 0; i < rows.length; i++) {
const cells = rows[i].getElementsByTagName('td');
const country = {};
if (!cells.length) continue;
for (let j = 0; j < cells.length; j++)
country[columnKeys[j]] = cells[j].textContent;
countries.push(country);
}
return countries;
});
await t
.expect(getCountries()).eql([
{
country: 'USA',
capital: 'Washington, D.C.',
population: '309 million',
language: 'English'
},
{
country: 'Sweden',
capital: 'Stockholm',
population: '9 million',
language: 'Swedish'
}
]);
});
如果您的情况不同,请更详细地描述它。测试代码和测试页面示例将非常有帮助。
推荐阅读
- python - 如何使用 Python 3 下载和静默安装具有给定 URL 的 .exe 文件
- azure - 递归列出 Azure 资源组中的所有资源标记
- reactjs - 在嵌套的 setTimeout 回调中更新 React 状态
- python - 如何从仅包含某些值的数据框中提取行
- python - PyTorch:将除前k之外的向量的所有元素归零?
- android - 如何在android上的edittext上设置最大长度消息
- java - 如何在不使用 Java 库中的 CyclicBarrier 的情况下制作自己的 CyclicBarrier
- c# - SqlDataReader 只能检查(Null 或非 null 值)之一
- firebase - 获取下拉菜单选择的值并将其发送到 Firebase ( Flutter )
- php - 基于国家代码的 WooCommerce 动态产品类别信息