我有一个div,有两个类,如When I click element with class "button"
当我点击这个课程时,我将重定向到一个页面。我的问题是如何在behat / mink中执行代码
我试过这个
/**
* @When /^I click element with class "([^"]*)"$/
*/
public function iClickElementWithClass($class) {
$locator = ".$class";
$element = $this->getSession()->getPage()->find('css', $locator);
if (null === $element) {
throw new \InvalidArgumentException(sprintf('Could not evaluate CSS selector: "%s"', $locator));
}
$element->click();
$this->getSession()->wait(1000);
}
但是它不起作用。
$ bin/behat features/cap_dutch_booking.feature
@javascript
Feature: Contact form Navigation and Submission
In order to check booking form
As an anonymous user
I should to be able to submit the booking form
Scenario: Submission of booking form with proper data # features\cap_dutch_booking.feature:8
Given I am on homepage # FeatureContext::iAmOnHomepage()
When I click "Opleidingen" # FeatureContext::assertClick()
And I click "BIM" # FeatureContext::assertClick()
And I click "BISL® Foundation" # FeatureContext::assertClick()
When I click element with id "edit-submit--3" # FeatureContext::iClickElementWithId()
And I should be on "/trainingen/bisl-foundation" # FeatureContext::assertPageAddress()
When I click element with class "button" # FeatureContext::iClickElementWithClass()
And I should be on "/cart" # FeatureContext::assertPageAddress()
Current page is "/trainingen/bisl-foundation", but "/cart" expected.
1 scenario (1 failed)
8 steps (7 passed, 1 failed)
请找到错误消息(不导航到下一页“/ cart”)。
UINavigationController
答案 0 :(得分:1)
在您的示例中,您可以使用以下选择器:.class1.class2
尝试删除$ locator ="。$ class&#34 ;;并使用.class1.class2作为参数。 你也应该尝试点击链接而不是div。
/**
* @Then /^I click "(?P<selector>[^"]*)"$/
*/
public function iClick($locator){
$element = $this->getSession()->getPage()->find('css', $locator);
if($element === null){
throw new Exception("Element $locator not found");
}
$element->click();
}
在FeatureContext中或在保留常规方法的上下文中添加此方法,它应该是扩展MinkContext的上下文。
尝试以下其中一种选择器格式:
a[href*=cart]
or
.class1.class2 a[href*=cart]
&#13;
在测试中使用它:
点击&#34; [href * = cart]&#34; 要么 我点击&#34; .class1.class2 a [href * = cart]&#34;
答案 1 :(得分:0)
这就是我使用多个选择器的方法。
When I click element with xpath "//div[contains(@class,'button') and contains(@class,'btn2')]/a"
/** Click on the element with the provided xpath query
*
* @Given /^I click on the element with xpath "([^"]*)"$/
* @Given /^I click on element with xpath "([^"]*)"$/
* @Given /^I click element with xpath "([^"]*)"$/
*/
public function iClickOnTheElementWithXPath($xpath)
{
$session = $this->getMainContext()->getSession(); // get the mink session
$element = $session->getPage()->find(
'xpath',
$session->getSelectorsHandler()->selectorToXpath('xpath', $xpath)
);
// errors must not pass silently
if (null === $element)
{
throw new \InvalidArgumentException(sprintf('Could not evaluate XPath: "%s"', $xpath));
}
// ok, let's click on it
$element->click();
}