开始测试与Drupal 7,Behat 3,Selenium2的Javascript交互

时间:2016-06-20 23:15:58

标签: selenium selenium-webdriver drupal-7 behat mink

我一直坚持如何配置我的Drupal 7网站以使用Behat来测试Javascript交互。

这是我开始的设置。如果有人可以帮我修改配置以支持测试Javascript,我将非常感激。

Vagrant 1.8.1正在运行:   - 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

我使用以下命令启动了Selenium:

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

看起来Selenium正在运行: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

上下文/ 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()
    {
    }
}

特征/ 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"

当我运行test.feature behat 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      # 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()

1 个答案:

答案 0 :(得分:1)

由于您正在测试Drupal 7站点,我建议您使用Behat提供的Drupal Extension。看起来您已经在作曲家文件中需要必要的扩展名。所以在这种情况下不需要更新作曲家。

但是,您可以在这里使用'drupal'驱动程序或'drush'驱动程序而不是'blackbox',因为它们功能更强大。如果您有本地网站设置,请继续使用'Drupal'驱动程序。 'Drupal'驱动程序是最强大的API驱动程序。

请按照以下步骤运行第一个方案:

  1. 修改您的behat.yml文件,如下所示:
  2. 的Drupal \ DrupalExtension:

    api_driver:'drupal'

        drupal:
          drupal_root: /path/to/docroot/
    

    2。修改FeatureContext文件,类文件应扩展DrupalContext,以便您可以使用Drupal扩展提供的所有现成步骤定义。

    class FeatureContext extends DrupalContext实现了SnippetAcceptingContext,Context {

    1. 在命令提示符下,在Behat父目录中,键入命令:
    2. bin / behat --dl

      1. 不要忘记使用 @api
      2. 标记您的方案或整个功能文件

        这将为您提供所有内置Drupal步骤定义的列表。让我知道它是怎么回事。