douqiao6015 2014-07-23 23:50
浏览 40

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'));

        }

    }


}
  • 写回答

2条回答 默认 最新

  • doucou1892 2014-07-24 14:29
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大