在behat和貂皮中的单个功能文件中维护场景之间的会话

问题描述 投票:4回答:3

我为此做了很多研发,但找不到解决方案。

我需要在一个功能文件中维护不同方案之间的登录会话。我做了一个函数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分钟。我想要的是在每种情况下在单个功能文件中完成之后,测试以相同的登录会话继续进行。

session selenium-webdriver behat mink
3个回答
4
投票

无法完成。 Behat方案是有意独立的。否则,您可能会从一种情况到另一种情况进行状态泄漏

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

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

[此外,您似乎正在使用Behat进行功能测试,而它是为验证业务期望而构建的。您可以考虑直接使用Mink,这将为您提供更多功能。


1
投票

它可以完成!我刚刚找到了一个解决方案-您需要创建一个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实例上运行多种方案!


0
投票

如果其中一种情况失败,这是什么行为?

“通常”(如果每个方案都有自己的会话),则每个方案都将执行,即使其中一个正在下降。但是现在,它会在每种情况下都停止了吗?

即使您没有很多方案,也可能不得不更正您的方案并重新启动所有方案...。过了一段时间,您将有很多方案,您肯定会自动执行,想想如果进行一次测试失败,之后的所有其他对象均被阻止。您将必须重新执行所有操作以验证未执行的操作是否没有失败!

一切都可以用Php完成,您只需要考虑一下(根据问题而不同)。但是最大的问题是“ 会有什么后果?”。

如果开发人员选择不这样做,那一定是有原因的。因此,请注意之后会追加什么。

© www.soinside.com 2019 - 2024. All rights reserved.