2

我需要一个关于点击 testCafe 的非常简单的帮助。

我有一个简单的元素 10

  • 里面。我必须单击第一个或最后一个或从索引 0 到索引 10 的随机 li。

    我怎样才能做到这一点?

  • 4

    1 回答 1

    2

    使用nth(index)方法通过索引选择元素:

    function getRandomInt(min, max) {
       return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    
    const ul = Selector('ul');
    const li = ul.find('li');
    
    const liCount = await li.count;
    const index = getRandomInt(0, liCount);
    
    await t.click(li.nth(index));
    
    于 2018-01-13T22:41:51.090 回答