Suraj
Suraj

Reputation: 287

How to access multiple class of a div in behat?

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)

Upvotes: 1

Views: 1053

Answers (2)

Jose&#39; Vargas
Jose&#39; Vargas

Reputation: 1219

This is how I handle using multiple selectors.

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();

}

Upvotes: 0

lauda
lauda

Reputation: 4173

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]"

Upvotes: 1

Related Questions