开发者

Selenium: How to assert all checkboxes checked?

Decimal totalCheckboxes = selenium.GetXpathCount("//input[@type='checkbox']");
  for (int i = 1; i < totalCheckboxes + 1; i++) 
        {
            // Assert attempt 1
            //Assert.IsTrue(selenium.IsChecked("/descendant-or-self::input[@type='checkbox'][" + i + "]"));

            // Assert attempt 2
            //Assert.IsTrue(selenium.IsChecked("//input[@type='checkbox'][" + i + "]")); 
        }

I need to assert multiple checkboxes are checked. The number of ch开发者_运维技巧eckboxes are not always fixed because they depend on my search criteria. Also, the checkboxes have different id and name. For example, for the first checkbox, id = "ctl07_ctl01_ctl01_cbxRepeater_e5962e80-ca07-42e3-908f-1217ef5787d4" name = "ctl07$ctl01$ctl01$cbxRepeater_e5962e80-ca07-42e3-908f-1217ef5787d4"

and for the second checkbox, id="ctl07_ctl01_ctl03_cbxRepeater_c094f428-7ead-4ded-a11b-5824be49a95b" name="ctl07$ctl01$ctl03$cbxRepeater_c094f428-7ead-4ded-a11b-5824be49a95b"

and so on for the following checkboxes.

I have tried several things to add an assertion to assert the checkboxes are checked (Assert attempt 1 and Assert attempt 2 above), but when I run the test, it still fails at that point. Ther error I get:

Selenium.SeleniumException: ERROR: Element /descendant-or-self::input[@type='checkbox'][1] not found
Selenium.SeleniumException: ERROR: Element //input[@type='checkbox'][2] not found

Any help on this would be highly appreciated. Thanks in advance!


Try :

Assert.IsTrue(selenium.IsChecked("xpath=(//input[@type='checkbox'])[" + i + "]")); 

Note the addition of ().

The () says evaluate me first (as you would expect). This allows you to do (...)[1] to find the first element of the xpath evaluated in the ().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜