3

I need a very simple help about click on testCafe.

I' ve got a simple element with 10

  • inside. I have to click the first one or the last one or a random li from index 0 to index 10.

    How can I do this?

  • Alex Skorkin
    • 4,264
    • 3
    • 25
    • 47
    user3528466
    • 186
    • 2
    • 17

    1 Answers1

    2

    Use the nth(index) method to select an element by its 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));
    
    Alexander Moskovkin
    • 1,861
    • 12
    • 13