for-loopradio-buttontestcaferadio-groupradiobuttonlist

How to identify the checked radiobox from a radio-box group in Testcafe?


The webpage I am testing in Testcafe has got two groups of radio-boxes.

  1. first group always has two radio-boxes with Yes(always selected by default) and No, available based on inputs from previous screens. It can be absent based on some inputs.
  2. second group has a variable number of radio-boxes again depending on inputs from previous screens, with one default checked in any order.

Now I am trying to do the following but the code is not verifying it properly. 1.count the number of total radio-boxes combined from both groups 2. Iterate over this count to find the ticked radio-box + the test text associated with it. (for ex - ticked radio-box element from first group + 'Yes', or ticked radio-box element from second group + 'sample test text')

Can you please help debugging it ?

const radioBox = Selector('.radioBox')

const radioboxCount = await radioBox.count
console.log(await radioboxCount);

//verify if the first group of radio-box is present with 'Yes' selected by default.
for (let count = 0; count < await radioboxCount; count++) {
            console.log("Inside Loop");
            var radioBoxObject1 = Selector('.radioBox').nth(count).find('label').withText('Yes')
            var radioBoxObject2 = Selector('.radioBox').nth(count).find('input').withAttribute('checked')

            if((radioBoxObject1.exists) && (radioBoxObject2.exists)){
              console.log('service question has been asked correctly'+ count)
            }else {
              console.log('issue in service question answer')
            }

Actual Result

Since there are total 5 radio-boxes and I am checking for 'Yes' radio box, I expect the output to be: Landing Page 5 Inside Loop service question has been asked correctly0


Solution

  • Use await when you try to check exists outside the built-in expect. Otherwise, it returns Promise and Promise is true in the if-else condition.

    if((await radioBoxObject1.exists) && (await radioBoxObject2.exists)){