pawanamigo
pawanamigo

Reputation: 35

Avoiding await in for loop in testafe

I have a written a test that loops through drop down options and sets value based on options. During the process, I have to also evaluate if the index is indeed set for which my code is -

    test(`Select all available options`, async (browser) => {
      const audioDevicelist = Selector(
        '#__next > div > main > div.Home_toolbar__JY_RL > div.Home_deviceControls__nQw4V > select:nth-child(2)'
      );
      const setSelectedIndex = ClientFunction(
        (index) => {
          audioDevicelist().selectedIndex = index;
        },
        { dependencies: { audioDevicelist } }
      );
      await browser.wait(3000);
      for (let i = 0; i < 4; i++) {
        await setSelectedIndex(i);
        await browser.expect(audioDevicelist.selectedIndex).eql(i);
         
        }
      }
      
    });

This works, but I wanted to avoid the await inside a loop. So I tried -

if (audioDevicelist.selectedIndex === i) {
        let ops += 1;
      } else {
        console.log('Sorry');
      }

But somehow the if statement is not validating while below statement satisfies and passes the test.

await browser.expect(audioDevicelist.selectedIndex).eql(i);

Any pointers? I just wanted to remove await inside the for loop.

Upvotes: 0

Views: 60

Answers (1)

Alexey Popov
Alexey Popov

Reputation: 1047

You can't get any properties from the Selector result directly because the result of the Selector is a special asynchronous function and you should run it to get DOMNodeState. After that, you can get this property from the node state. But in any case, you should use await to get DOMNodeState.

Upvotes: 2

Related Questions