Rob*_*man 7 html testing click behat mink
我正在使用Behat来测试第三方网店.我在购物车中有一个项目要删除.确认弹出窗口显示询问我是否真的想要这样做.此对话框的结构如下所示:
<div>
<strong class="title">Remove item from shoppingcart</strong>
<p>Are you sure you want to delete this product?</p>
<div class="button-container">
<span class="button" data-confirm="true">Yes</span>
<span class="button alt right" data-mfp-close-link="true">No</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我能够使用xpath选择跨度,并使用以下代码:
public function iConfirmTheWindow()
{
$session = $this->getSession();
$element = $session->getPage()->find(
'xpath',
$session->getSelectorsHandler()->selectorToXpath('css', 'span.button')
);
if (null === $element) {
throw new \InvalidArgumentException(sprintf('Could not find confirmation window'));
}
$element->click();
}
Run Code Online (Sandbox Code Playgroud)
选择工作,但Behat似乎无法点击跨度.
supports clicking on links and submit or reset buttons only. But "span" provided
Run Code Online (Sandbox Code Playgroud)
我需要单击此项,如何重写我的功能以便可以单击它?
Wil*_*ers 12
@bentcoder的答案没有任何不同.它使用不同的选择器来查找元素,但Minkcontext click功能不支持单击span元素.
我发现这很奇怪,因为使用jQuery你可以添加button类和span元素,并有你的按钮.
上下文代码:
/**
* @Given I click the :arg1 element
*/
public function iClickTheElement($selector)
{
$page = $this->getSession()->getPage();
$element = $page->find('css', $selector);
if (empty($element)) {
throw new Exception("No html element found for the selector ('$selector')");
}
$element->click();
}
Run Code Online (Sandbox Code Playgroud)
CLI输出:
And I click the "#new_account" element # tests/behat/features/account.feature:14
Behat\Mink\Driver\GoutteDriver supports clicking on links and submit or reset buttons only. But "span" provided (Behat\Mink\Exception\UnsupportedDriverActionException)
Run Code Online (Sandbox Code Playgroud)
UPDATE
我忘了添加@javascript到我的场景..
我使用的代码片段是这样的:
/**
* @Then /^I click on "([^"]*)"$/
*/
public function iClickOn($element)
{
$page = $this->getSession()->getPage();
$findName = $page->find("css", $element);
if (!$findName) {
throw new Exception($element . " could not be found");
} else {
$findName->click();
}
}
Run Code Online (Sandbox Code Playgroud)
举个例子,我会在我的场景中写出这样的东西:
Feature: Test Click
@javascript
Scenario: Clicking on spans
Given I go to "http://docs.behat.org/en/v2.5/"
And wait "3000"
When I click on "span:contains('behat.yml')"
And wait "3000"
Then I should be on "http://docs.behat.org/en/v2.5/guides/7.config.html"
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助.