简体   繁体   English

可以用Behat + Mink测试CSS样式表吗?

[英]Is it possible tests CSS stylesheets with Behat + Mink?

Is there some way how to test css parameters (coming from css stylesheet) of some element? 有什么方法可以测试某些元素的CSS参数(来自CSS样式表)? Eg color of paragraph. 例如段落颜色。 Or background-color of div etc... 或div等的背景色...

I know that selenium2 test framework can do this, but in Behat + Mink with selenium2 driver I cannot find a way how to do it. 我知道selenium2测试框架可以做到这一点,但是在带有selenium2驱动程序的Behat + Mink中,我找不到解决方法。

Thanks in advance. 提前致谢。

Yes, you would need to use Selenium2 driver along with Behat and Mink + custom JS calls to verify specific styles. 是的,您将需要使用Selenium2驱动程序以及Behat和Mink +自定义JS调用来验证特定样式。 The below example (Behat 3) uses jQuery to assert that the div.myClass color property is as expected. 下面的示例(Behat 3)使用jQuery断言div.myClass color属性符合预期。 It sends script to Selenium, which evaluates it and returns the result, which you can process in PHP as usual. 它将脚本发送到Selenium,Selenium对其进行评估并返回结果,您可以像往常一样在PHP中进行处理。

<?php

class MyContext extends RawMinkContext
{

    /**
     * @Then /^The div.myClass color should be black$/
     */
    public function assertCssValue()
    {

        // JS script that makes the CSS assertion in the browser.

        $script = <<<JS
            (function(){
                return $('div.myClass').css('color') === 'rgb(0, 0, 0)';
            })();
JS;

        if (!$this->getSession()->evaluateScript($script)) {
            throw new Exception();
        }
    }
}

You can easily extend this into a more generic instance that will do all different kinds of assertions, but most logic will be in JS. 您可以轻松地将其扩展为一个更通用的实例,该实例将执行所有不同种类的断言,但是大多数逻辑将在JS中进行。

Besides there are other CSS testing frameworks / alternatives like PhantomCSS , which in theory should work with PhantomJS via Selenium Driver and Mink. 除此之外,还有其他CSS测试框架/替代品,例如PhantomCSS ,从理论上讲应该通过Selenium Driver和Mink与PhantomJS一起使用。 Google "css testing framework" for that… Google的“ css测试框架” ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM