Jennifer Dwinall
Jennifer Dwinall

Reputation: 1

Getting started testing Javascript interactions with Drupal 7, Behat 3, Selenium2

I have been stuck on how to configure my Drupal 7 site to work with Behat to test Javascript interactions.

This is the setup I am starting with. If someone could help me modify my configuration to support testing Javascript I would greatly appreciate it.

Vagrant 1.8.1 running: - Ubuntu 14.04.2 LTS - PHP 5.6.22-1+donate.sury.org~trusty+1 (cli) - Drupal 7.41 - Selenium: selenium-server-standalone-2.46.0.jar

I started Selenium using the following commands:

java -jar /opt/selenium/selenium-server-standalone.jar -role hub -port 4444
java -jar /opt/selenium/selenium-server-standalone.jar -role node -hub http://localhost:4444/grid/register

It looks like Selenium is running: lsof -i -n -P | grep 4444

php     2428 vagrant    6u  IPv6  22327      0t0  TCP [::1]:46489->[::1]:4444 (CLOSE_WAIT)

composer.json

{
    "require-dev": {
        "drush/drush": "7.1.0",
        "drupal/coder": "7.2.5",
        "phpunit/php-timer": "dev-master",
        "pear/Console_Color2": "0.1.2",

        "behat/behat": "3.1.0",
        "behat/mink": "1.7.1",
        "behat/mink-extension": "2.2",      
        "behat/mink-goutte-driver": "1.2.1",
        "behat/mink-selenium2-driver": "1.3.1",         
        "drupal/drupal-extension": "3.2.1"
    },
    "config": {
        "bin-dir": "bin/"
    } 
}

behat.yml

# behat.yml
default:
    autoload: [ %paths.base%/contexts ]
    extensions:
      Behat\MinkExtension:
        goutte: ~
        selenium2: ~
        base_url: http://test.dev
      Drupal\DrupalExtension:
        blackbox: ~
    suites:
        default:
            paths:    [ %paths.base%/features ]
            filters:
            contexts:
              - FeatureContext
              - Drupal\DrupalExtension\Context\DrupalContext
              - Drupal\DrupalExtension\Context\MinkContext
              - Drupal\DrupalExtension\Context\MessageContext
              - Drupal\DrupalExtension\Context\DrushContext

contexts/FeatureContext.php

<?php

use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

/**
 * Defines application features from the specific context.
 */
class FeatureContext implements Context, SnippetAcceptingContext
{
    /**
     * Initializes context.
     *
     * Every scenario gets its own context instance.
     * You can also pass arbitrary arguments to the
     * context constructor through behat.yml.
     */
    public function __construct()
    {
    }
}

features/test.feature

Feature: Test DrupalContext
  In order to prove the Drupal context using the blackbox driver is working properly
  As a developer
  I need to use the step definitions of this context

  @javascript
  Scenario: Test the ability to find a heading in a region
    Given I am on the homepage
    When I click "some link"
    Then I should see the heading "Some heading"

When I run test.feature behat features/test.feature I get the following output. I do not know how to get the stack trace to output. If you can tell me how I will add that information to my post.

Feature: Test DrupalContext
  In order to prove the Drupal context using the blackbox driver is working properly
  As a developer
  I need to use the step definitions of this context

  @javascript
  Scenario: Test the ability to find a heading in a region      # features/test.feature:7
    Given I am on the homepage                                  # Drupal\DrupalExtension\Context\MinkContext::iAmOnHomepage()
      Could not open connection (Behat\Mink\Exception\DriverException)
    When I click "some link"                                    # Drupal\DrupalExtension\Context\MinkContext::assertClick()
    Then I should see the heading "Some heading"                # Drupal\DrupalExtension\Context\MinkContext::assertHeading()

Upvotes: 0

Views: 280

Answers (1)

Shweta Sharma
Shweta Sharma

Reputation: 161

Since you are testing a Drupal 7 site, I would recommend you to use the Drupal Extension that is provided by Behat. Looks like you have already required the necessary extension in your composer file. So no need to update the composer in this case.

However, you can use the 'drupal' driver or 'drush' driver here instead of 'blackbox', since they are much more powerful. If you have a local setup of the site, go ahead with the 'Drupal' driver. 'Drupal' driver is the strongest of the available API drivers.

Please follow the steps below to get your first scenario running:

  1. Modify your behat.yml file as below:

Drupal\DrupalExtension:

api_driver: 'drupal'

    drupal:
      drupal_root: /path/to/docroot/

2. Modify your FeatureContext file, class file should extend DrupalContext, so that you can use all the ready-made step definitions that Drupal extension provides.

class FeatureContext extends DrupalContext implements SnippetAcceptingContext, Context {

  1. In the command prompt, in your Behat parent directory, type the command:

bin/behat --dl

  1. Don't forget to tag your scenarios or the entire feature file with @api

This would give you a list of all in-built Drupal based step definitions. Let me know how it goes.

Upvotes: 1

Related Questions