如何在Test Cafe中使用Javascript将表作为数组返回

时间:2019-07-20 00:33:39

标签: javascript arrays testcafe

我试图验证整个表元素,并希望将表作为数组返回,然后要访问每个元素。.pls帮助。

1 个答案:

答案 0 :(得分:1)

请查看下面的示例,该示例演示如何使用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'
            }
         ]);
});

如果情况不同,请更详细地描述。测试代码和测试页示例将非常有帮助。