4

これについて多くの研究開発を行ってきましたが、解決策を見つけることができません。

1 つの機能ファイルで、さまざまなシナリオ間でログイン セッションを維持する必要があります。関数I am logged inを作成し、バックグラウンドで書き込みました。そのため、すべてのシナリオの開始時にログインが発生します。しかし、私が望むのは、シナリオ全体で単一のログイン セッションを維持することです。誰でも提案できますか?

コードの例は次のとおりです。

Feature: To test the output

Background:
  Given I am logged in 

@javascript
 Scenario: To test the positive input
   When I fill in "test" with "aab"
   And I press "add"
   Then I should see "welcome"

@javascript
  Scenario:To test the negative inputs
    When I fill in "test" with "@#$@!!111"
    And I press "add"
    Then I should see "Sorry,invalid input please try again"

他の人が私のコードをレビューすると、ポジティブなテスト ケースとネガティブなテスト ケースについて知るようになります。しかし、シナリオがリロードされるたびに、1 つの機能に 50 のシナリオがあるとしたらどうなるでしょうか。より大きなプロジェクトの場合。ログインするすべてのシナリオで見栄えがよくなく、合計で 15 分余分に無駄にします。私が望むのは、単一の機能ファイルのすべてのシナリオの後、同じログイン セッションでテストが続行されることです。

4

3 に答える 3

4

それはできません。Behat シナリオは、意図的に独立しています。そうしないと、あるシナリオから別のシナリオに状態がリークする危険があります。

あなたは正しい方向から問題に近づいていません。速度の向上のためにシナリオの分離を犠牲にすることは、長期的にはあなたを傷つけます.

ログインが機能の 1 つとしてテストされていると仮定すると、ログインが必要な他のシナリオでは、実際のログイン フォームを使用する必要はありません。プログラムでそれを行うことを考えてください。

また、ビジネスの期待を検証するために構築されている一方で、機能テストに Behat を使用しているようです。Minkを直接使用することを検討すると、より強力になります。

于 2013-10-10T09:58:48.500 に答える
1

できる!解決策を見つけたところです。webDriver の静的インスタンスを維持する AbstractWebDriver クラスを作成する必要があります。

FeatureContext

<?php
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends AbstractWebDriver
{
    /**
     * 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()
    {
        $capabilities = DesiredCapabilities::safari();
        if(!AbstractWebDriver::$webDriver) {
            AbstractWebDriver::$webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
        }
        $this->baseUrl = "http://test.test.com";
    }
}

AbstractWebDriver

<?php

use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

/**
 * Defines application features from the specific context.
 */
abstract class AbstractWebDriver extends \PHPUnit\Framework\TestCase implements Context, SnippetAcceptingContext
{
    /**
     * @var \RemoteWebDriver
     */
    protected static $webDriver;
    protected $baseUrl;

protected function getDriver()
{
    if($this->webDriver==Null)
    echo "----------------- Instatiate New Driver -----------------";
    $capabilities = DesiredCapabilities::safari();
    self::$webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);


    echo "----------------- Return Current Driver -----------------";
}



}

1 つの機能ファイルに対して、webDriver の 1 つのインスタンスで複数のシナリオを実行できるようになりました!

于 2018-04-04T22:57:14.937 に答える