Php 使用Selenium确定是否选中具有特定类的复选框

Php 使用Selenium确定是否选中具有特定类的复选框,php,phpunit,selenium-rc,xpath,Php,Phpunit,Selenium Rc,Xpath,我将Selenium与PHPUnit结合使用,试图判断是否选中了一组特定类的复选框,我遇到了一些麻烦 我的代码: $count = $this->getXpathCount('//form//input[@type="checkbox" and @class="required"]'); for ($i = 1; $i <= $count; $i++) { $this->assertTrue($this->isChecked(sprintf('xpath=//fo

我将Selenium与PHPUnit结合使用,试图判断是否选中了一组特定类的复选框,我遇到了一些麻烦

我的代码:

$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)));
}
$count=$this->getXpathCount('//form//input[@type=“checkbox”和@class=“required”]');
对于($i=1;$i assertTrue($i->isChecked(sprintf('xpath=//form//input[@type=“checkbox”和@class=“required”][%d]”,$i));
}
不幸的是,我似乎不能在同一个标记上使用两次方括号,但我确实需要确保选中所有具有类“required”的复选框


有什么建议吗?

我不知道Selenium,但是一个
DOMXPath->evaluate
会理解这个语法并返回一个浮点值(不是int,而是hey),也许它适合你:

count(//form//input[@type="checkbox" and @class="required" and not(@checked)])
或者简单地说:

$this->assertTrue($this->getXpathCount('//form//input[@type="checkbox" and @class="required" and not(@checked)]')==0);

我不知道Selenium,但是
DOMXPath->evaluate
会理解这个语法并返回一个float(不是int,而是hey),也许它适合您:

count(//form//input[@type="checkbox" and @class="required" and not(@checked)])
或者简单地说:

$this->assertTrue($this->getXpathCount('//form//input[@type="checkbox" and @class="required" and not(@checked)]')==0);

除非我有误解,否则使用方括号两次应该不会有任何问题。我能够让您的代码在使用PHPUnit和Selenium时顺利运行,而不会对下面的html产生任何问题:

<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>

复选框





您是否正在运行最新版本的PHPUnit和Selenium rc?

除非我有误解,否则您在使用方括号两次时不会遇到任何问题。我能够让您的代码在使用PHPUnit和Selenium对以下html时顺利运行:

<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>

复选框





您正在运行PHPUnit和Selenium rc的最新版本吗?

$checkbox3=$this->byClassName('required');

$this->assertTrue($checkbox3->selected());

$checkbox3=$this->byClassName('required');

$this->assertTrue($checkbox3->selected());

旁注:你确定你的
input[@class=“required”]
s没有任何其他类名吗?a
contains()
可能更健壮。旁注:你确定你的
input[@class=“required”]
s没有任何其他类名吗可能更健壮。