Behat工作正常,但PhantomJS在尝试点击时在下拉菜单上失败

时间:2014-09-30 08:36:52

标签: selenium phantomjs behat mink

下面的Gherkin可以正常使用但是当我使用PhantomJS时,我在When I follow "Profile"行下面会出错:

注意:该元素是bootstrap的下拉菜单,因此在单击后切换。

错误

Exception thrown by (//html/.//a[./@href][(((./@id = 'Profile' or contains(normalize-space(string(.)), 'Profile')) or contains(./@title, 'Profile') or contains(./@rel, 'Profile')) or .//img[contains(./@alt, 'Profile')])] | .//*[./@role = 'link'][((./@id = 'Profile' or contains(./@value, 'Profile')) or contains(./@title, 'Profile') or contains(normalize-space(string(.)), 'Profile'))])[1]
{"errorMessage":"Element is not currently visible and may not be manipulated","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"27","Content-Type":"application/json; charset=utf-8","Host":"localhost:5140","User-Agent":"Apache-HttpClient/4.3.4 (java 1.5)"},"httpVersion":"1.1","method":"POST","post":"{\"id\":\":wdc:1412065547042\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/5b7c0830-487b-11e4-adc0-a162f25e4a98/element/%3Awdc%3A1412065547042/click"}}

小黄瓜:

When I follow "Profile"
And I follow "Edit"

HTML:

<ul class="nav navbar-nav navbar-right">
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Profile <span class="caret"></span></a>
        <ul class="dropdown-menu" role="menu">
            <li><a href="#">Show</a></li>
            <li><a href="#">Edit</a></li>
        </ul>
    </li>
</ul>

1 个答案:

答案 0 :(得分:0)

我已经发现了原因并通过如下所示的小修复解决了。

使用Bootstrap时,菜单会被压扁,只显示一个图标代表相同的菜单,这是响应式设计的正常行为。一切都很好,但是当PhantomJS运行时,它找不到它正在寻找的文本。例如我个案中的“个人资料”菜单。

菜单的原始状态,总是正常工作:

enter image description here

压扁版本后导致PhantomJS失败:

enter image description here

<强> SOLUTION:

只需将窗口大小设置为更大的值,以便在测试时不会压缩菜单。您可以根据需要将屏幕尺寸从1024X768更改为其他内容。

#symfony/src/Site/CommonBundle/Features/Context/FeatureContext.php

/**
 * @BeforeStep
 */
public function beforeStep()
{
    $this->getSession()->resizeWindow(1024, 768, 'current');
}