views:

26

answers:

2

I'm using Selenium with PHPUnit to try to tell whether a bunch of checkboxes with a specific class are checked and I'm having a bit of trouble.

My code:

$count = $this->getXpathCount('//form//input[@type="checkbox" and @class="required"]');
for ($i = 1; $i <= $count; $i++) {
    $this->assertTrue($this->isChecked(sprintf('xpath=//form//input[@type="checkbox" and @class="required"][%d]', $i)));
}

Unfortunately, it doesn't seem like I can use square brackets twice on the same tag, but I do need to make sure all checkboxes that have the class "required" are checked.

Any Suggestions?

A: 

I don't know about Selenium, but a DOMXPath->evaluate would understand this syntax & return a float (not int, but hey), maybe it works for you:

count(//form//input[@type="checkbox" and @class="required" and not(@checked)])

Or perhaps a simple:

$this->assertTrue($this->getXpathCount('//form//input[@type="checkbox" and @class="required" and not(@checked)]')==0);
Wrikken
On a side note: are you sure your `input[@class="required"]` s don't have any other classnames? A `contains()` could be more robust.
Wrikken
A: 

Unless I'm misunderstanding, you shouldn't have any problems using square brackets twice. I was able to get your code to work without trouble using PHPUnit and Selenium against the html below:

<html>
  <head>
    <title>Check Boxes</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <form action="self" method="post">
        <div>
            <input type="checkbox" class="required"/>
            <br />
            <input type="checkbox" class="required"/>
            <br />
            <input type="checkbox" class="required"/>
            <br />
            <input type="submit" value="Submit" />
            <br />
        </div>
    </form>
  </body>
</html>

Are you running the most recent version of PHPUnit and Selenium-rc?

David Kanenwisher