我有一个像这样<div class="class1 class2"> <a href="/cart">TEXT</a>的两个类的div。
 当我单击此类时,我将重定向到一页。我的问题是如何在behat / mink中执行代码。
我尝试过
 
When I click element with class "button",但不起作用。

           /**
           * @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)

最佳答案

在您的示例中,您可以使用以下选择器:.class1.class2

尝试删除$ locator =“。$ class”;并使用.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]





在测试中像这样使用它:

我点击“ a [href * =购物车]”
要么
我单击“ .class1.class2 a [href * =购物车]”

关于php - 如何在Behat中访问div的多个类?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38103141/

10-09 21:57