2010-09-29 38 views
1

我正在使用Selenium和PHPUnit來嘗試判斷是否有一組具有特定類的複選框被選中,我遇到了一些麻煩。使用Selenium來確定是否選中具有特定類的複選框

我的代碼:

$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))); 
} 

不幸的是,它似乎並不像我可以在同一個標​​籤使用方括號兩次,但我確實需要確保有「需要」之類的所有複選框檢查。

有什麼建議嗎?

回答

1

我不知道硒,但DOMXPath->evaluate會明白這句法&返回浮點(不是整數,但嘿),也許這對你的作品:

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); 
+0

附註:您確定您的輸入[@ class =「required」]沒有任何其他類名嗎? 'contains()'可能更健壯。 – Wrikken 2010-09-29 23:40:29

1

除非我誤解,否則使用方括號兩次不應有任何問題。我能得到你的代碼,而無需使用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版本?

-1

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

相關問題