【问题标题】:Maintaining the session between scenarios in a single feature file in behat and mink在 behat 和 mink 的单个功能文件中维护场景之间的会话
【发布时间】:2013-09-27 12:38:36
【问题描述】:

我在这方面做了很多研发,但我无法找到解决方案。

我需要在单个功能文件中维护不同场景之间的登录会话。 我做了一个函数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"

现在,如果另一个人查看我的代码,他就会知道正面和负面的测试用例。但是每次重新加载场景时,如果我在一个功能中有 50 个场景怎么办。对于更大的项目。在我登录的每个场景中,它看起来并不好,我总共浪费了 15 分钟。我想要的是在单个功能文件中的每个场景之后,测试继续使用相同的登录会话。

【问题讨论】:

  • 您是否只想在每个场景中懒惰地“登录”,或者您的场景之间是否存在依赖关系?
  • 您能发布说明或重现问题所需的最少代码量吗?另外,您是否使用After 挂钩在场景之间关闭浏览器?
  • @PaulMorie :我只是想节省我的测试时间,并且功能文件中的所有场景都与可能具有负输入正输入等的特定功能相关,因此对于每种不同的情况我都会记录下来每次都会降低性能。
  • @orde 是的,先生,代码已更新。
  • 这些场景是特定于会话的吗?

标签: session selenium-webdriver behat mink


【解决方案1】:

无法做到。 Behat 场景是故意独立的。否则,您将面临状态泄漏从一种情况到另一种情况的风险。

您没有从正确的方向解决问题。从长远来看,为了提高速度而牺牲场景分离会对您造成伤害。

假设登录作为功能之一进行了测试,在其他需要登录的情况下,您不必使用实际的登录表单。考虑以编程方式进行。

此外,您似乎在使用 Behat 进行功能测试,而它是为验证业务预期而构建的。可以考虑直接使用Mink,这样会更有力量。

【讨论】:

    【解决方案2】:

    可以!我刚刚找到了一个解决方案 - 你需要创建一个 AbstractWebDriver 类来维护 webDriver 的静态实例。

    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 -----------------";
    }
    
    
    
    }
    

    对于一个功能文件,我现在可以在一个 webDriver 实例上运行多个场景!

    【讨论】:

      【解决方案3】:

      如果其中一个场景失败,会有什么行为?

      “正常”(如果每个场景都有自己的会话),每个场景都会被执行,甚至有一个正在下降。 但是现在,它会在一个下降之后停止每个场景?

      即使您没有很多场景,也必须纠正您的场景并重新启动您的所有场景可能会很麻烦......一段时间后,您将有很多场景您肯定会自动执行,想象一下如果 1测试失败,之后的所有其他人都被阻止。您将不得不重新执行所有以验证那些未执行的是否没有失败!

      一切都可以在Php中完成,您只需要思考(或多或少根据问题)。但最大的问题是“后果是什么?”。

      如果开发者选择不做,那一定是有原因的。所以要注意后面会附加什么。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多