如何访问多个类的div在behat? [英] How to access multiple class of a div in behat?
问题描述
我有一个div,有两个类,像这样
< div class =class1 class2>
。
< a href =/ cart> TEXT< / a>
当我点击这个类,我会重定向到一个页面。我的问题是如何执行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 CSS selector:%s',$ locator));
}
$ element-> click );
$ this-> getSession() - > wait(1000);
}
b $ b
请找到错误信息(不转到下一页/ cart)。
$ bin / behat特性/ cap_dutch_booking.feature
@javascript
特性:联系表导航和提交
为了检查预订表单
作为匿名用户
我应该能够提交预订表
场景:使用适当的数据提交预订表#features\cap_dutch_booking.feature:8
假定我在主页#FeatureContext :: iAmOnHomepage()
当我点击Opleidingen#FeatureContext :: assertClick()
我点击BIM#FeatureContext :: assertClick()
我点击BISL┬«Foundation#FeatureContext :: assertClick ()
当我点击id为edit-submit - 3的元素#FeatureContext :: iClickElementWithId()
我应该在/ trainingen / bisl-foundation#FeatureContext :: assertPageAddress )
当我点击类按钮的元素#FeatureContext :: iClickElementWithClass()
我应该在/ cart#FeatureContext :: assertPageAddress()
当前页是 trainingen / bisl-foundation,但是/ cart预期。
1个场景(1个失败)
8个步骤(7个通过,1个失败)
<在您的示例中,您可以使用如下选择器:.class1.class2
尝试删除$ locator = 。$ class;并使用.class1.class2作为参数。
也应该尝试点击链接而不是div。
/ **
* @Then / ^ I click(?P< selector> [^] *)$ /
* /
public function iClick($ locator){
$ element = > getSession() - > getPage() - > find('css',$ locator);
if($ element === null){
throw new Exception Element $ locator not found);
}
$ element-> click();
}
在FeatureContext中或在保存常规方法的上下文中添加此方法,它应该是扩展MinkContext的上下文。
尝试以下选择器格式之一:
href * = cart] or.class1.class2 a [href * = cart]
$ b b
在测试中使用它:
我点击a [href * = cart]
或
我点击 .class1.class2 a [href * = cart]
I have a div with two classes like this <div class="class1 class2">
<a href="/cart">TEXT</a>
.
When i click on this class, i will redirect to one page. My question is how to execute the code in behat/mink.
I tried by this
When I click element with class "button"
but its not working.
/**
* @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);
}
Please find the error message(not navigating to the next page "/cart").
$ 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)
In your example you can use selectors like: .class1.class2
Try to remove $locator = ".$class"; and use .class1.class2 as parameter. Also you should try to click on the link and not the 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();
}
add this method in FeatureContext or in the context where you keep your general methods, it should be the context that extends MinkContext.
And try one of these selector formats:
a[href*=cart]
or
.class1.class2 a[href*=cart]
Use it like this in your test:
I click "a[href*=cart]" or I click ".class1.class2 a[href*=cart]"
这篇关于如何访问多个类的div在behat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!