2014-02-27 48 views
3

我在网站上有一个可展开的div。我想用Behat/Mink创建一个测试用例来声明,当用户到达页面时,该框不会被扩展。Behat/Mink从CSS元素获取价值

<a class="expand" data-reactid=".r[37uxa].[1]" href="#">Click to expand</a> 
<div class="expandable" data-reactid=".r[37uxa].[2]" style="height: 0px;"> 

之后,当用户点击“点击展开”的风格=“高度”的值更改:

<a class="expand" data-reactid=".r[37uxa].[1]" href="#">Click to expand</a> 
<div class="expandable" data-reactid=".r[37uxa].[2]" style="height: 157px;"> 

这意味着它现在已经扩大。

我想知道是否有办法,或者如果我可以实现一个步骤定义来检查/获取样式属性的值。谢谢。

回答

6

您可以使用NodeElement::isVisible()来检查元素的可见性。

$session = $this->getSession(); // assume extends RawMinkContext 
$page = $session->getPage(); 

$expandable = $page->find('css', '.expandable'); 

if (null === $expandable) { 
    throw new \LogicException('Could not find the element'); 
} 

if ($expandable->isVisible()) { 
    throw new \LogicException('Element is visible...'); 
} 

或者检查自己的风格attribute

$style = $expandable->getAttribute('style'); 
2

我想你有$session$session = new \Behat\Mink\Session($driver);。您可以使用NodeElement::getAttribute('style')

$page = $session->getPage(); 
$el = $page->find('css', '.something'); 
echo $el->getAttribute('style'); 

如果无法访问或它不为你工作,你总是可以做

echo $session->evaluateScript(
    "(function(){ return document.getElementById('...').style.height; })()" 
); 
4

谢谢两位,我是能够解决工作,我需要的东西在你的帮助。下面是我用的方式:

/** 
* @Then /^The "([^"]*)" element should not be expanded$/ 
*/ 

public function theElementShouldNotBeExpanded($element) 
{ 
    $session = $this->getSession(); 
    $page = $session->getPage(); 

    $expandable = $page->find('css', '.expandable'); 
    $style = $expandable->getAttribute('style'); 

    if ($style === "height: 0px;") { 
     $message1 = sprintf('Is not expanded having the attribute: '.$style); 
     echo $message1; 
    } else { 
      $message2 = sprintf('The element is expanded having the attribute: '.$style); 
      throw new Exception($message2); 

      } 

}

/** 
    * @Then /^The "([^"]*)" element shold be expanded$/ 
    */ 
    public function theElementSholdBeExpanded($element) 
    { 
     $session = $this->getSession(); 
     $page = $session->getPage(); 
     $expandable = $page->find('css', '.expandable'); 
     $style = $expandable->getAttribute('style'); 

     if ($style === "height: 105px;") { 
      $message1 = sprintf('Is expanded having the attribute: '.$style); 
      echo $message1; 
     } else { 
       $message2 = sprintf('The element is not expanded having the attribute: '.$style); 
       throw new Exception($message2); 

       } 

    } 

步骤定义如下:

Given I am on "/" 
     Then I should see an ".promotion" element 
     Then The ".expandable" element should not be expanded 
     And I follow "Click to expand" 
     Then The ".expandable" element shold be expanded 
     And I follow "Click to hide" 

而结果:

Given I am on "/"   
Then I should see an ".promotion" element  
Then The ".expandable" element should not be expanded 
--Is not expanded having the attribute: height: 0px; 
And I follow "Click to expand" 
Then The ".expandable" element shold be expanded 
    --Is expanded having the attribute: height: 105px; 

谢谢祝你有个美好的一天!