PHPUnit + Selenium测试div是否有css类

Question

How do you check if a div has a particular css class or other attribute with PHPUnit and Selenium?

Background

I have been using PHPUnit and Selenium to perform functional testing on a number of sites. Test are currently extending PHPUnit_Extensions_SeleniumTestCase but I have no issue using PHPUnit_Extensions_Selenium2TestCase if it will provide a better solution.

Test Example

This is an example of a test, the real world usage is much more complicated with test classes abstracted out into several sub classes that my test cases extend.

In this example I would like to be able to click a button (some_button) and check if a div (some_div) has the css class 'active'.

class ExampleTest extends PHPUnit_Extensions_SeleniumTestCase {

protected $captureScreenshotOnFailure = true;

    public function setUp() {

        //Load some base application configuration and set $base_url

        $this->setBrowser('firefox');
        $this->setBrowserUrl($base_url);
    }

    public function testIfButtonChangesClass() {
        $this->open("/test_page");

        $this->select("id=some_combo", "value=2");
        $this->assertFalse($this->isVisible('id=some_button'));

        $this->click("id=some_button");

        //How do I test if some_div has the class active? it would be nice to do 
        //something like this?
        $this->assertTrue($this->hasClass('id=some_div','active'));

        }

    }


}

The easiest way to do it is simply to assert the existence of the required class. For example, suppose you have some div with an id:

<div id="mydiv">blah</div>

You have some Javascript that adds the class "someclass" from the div when you click a button:

$this->click("id=mybutton"); // this click should add the class

// now test to see if the class has been added
$this->assertElementPresent("css=div#mydiv.someclass");

div#mydiv is going to exist one way or another. div#mydiv.someclass will only exist if the class someclass has been added.

Note that my syntax for the assertion is a little different from yours - I'm using a one-step assertElementPresent method. The PHPUnit_Extensions_SeleniumTestCase class exposes a whole bunch of helpful assertions matching the ones in the Selenium documentation.

In Selenium 2 with PHPUnit_Extensions_Selenium2TestCase syntax you are more encouraged to use the PHPUnit standard asserts and only use selenium to access the page elements (rather than the mix of the two in v1).

You could do:

$element = $this->byId('some_div');
$this->assertContains('active', $element->attribute('class'));

or similar to Kryten's answer you could just try to "get" the element and let it fail or handle the exception to convert to a failure or even continue on:

try{
    $element = $this->byByCSSSelector('div#some_div.active');
} 
catch(PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {        
    $this->fail('Class "active" not found. '.$e->getMessage());
}

Personally I prefer the first method.