Session 在behat和mink中的单个功能文件中维护场景之间的会话
我在这方面做了很多研发,但我找不到解决方案 我需要在单个功能文件中维护不同场景之间的登录会话。 我已经做了一个函数Session 在behat和mink中的单个功能文件中维护场景之间的会话,session,selenium-webdriver,behat,mink,Session,Selenium Webdriver,Behat,Mink,我在这方面做了很多研发,但我找不到解决方案 我需要在单个功能文件中维护不同场景之间的登录会话。 我已经做了一个函数我已经登录了并且我已经在后台写了。因此,在每个场景开始时,都会进行登录。但我想要的是在所有场景中维护一个登录会话。 有人能建议吗 示例代码为: Feature: To test the output Background: Given I am logged in @javascript Scenario: To test the positive input Wh
我已经登录了并且我已经在后台写了。因此,在每个场景开始时,都会进行登录。但我想要的是在所有场景中维护一个登录会话。
有人能建议吗
示例代码为:
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分钟。我想要的是,在单个功能文件中的每个场景之后,测试将以相同的登录会话继续进行。这无法完成。行为场景是故意独立的。否则,您将面临从一个场景泄漏到另一个场景的风险
你没有从正确的方向解决问题。从长远来看,牺牲场景分离来提高速度会伤害你
假设登录作为功能之一进行了测试,在需要登录的其他场景中,您不必使用实际的登录表单。考虑按程序进行
此外,您似乎正在使用Behat进行功能测试,而它是用于验证业务期望的构建。你可以直接考虑使用,这会给你更多的能量。 < P> <强>可以做到!我刚刚找到了一个解决方案-您需要创建一个AbstractWebDriver类来维护webDriver的静态实例
功能上下文
<?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";
}
}
如果其中一个场景失败,该场景的行为是什么
“正常”(如果每个场景都有自己的会话),每个场景都将被执行,即使一个场景正在下降。
但现在,它将停止每一个场景后,一个下降
即使你没有太多的场景,纠正你的场景并重新启动你所有的场景也会很麻烦。。。一段时间后,您将有许多场景肯定会自动执行,想象一下,如果一个测试失败,之后的所有其他场景都会被阻止。您将必须重新执行所有,以验证未执行的操作是否失败
一切都可以在Php中完成,您只需思考(或多或少根据问题而定)。但最大的问题是“后果如何?”
如果开发人员选择不这样做,这一定是一个原因。因此,请注意后面会附加什么。您是否只想在每个场景中(如果还没有)懒洋洋地“登录”,或者您的场景之间是否存在依赖关系?您能否发布说明或重现问题所需的最少代码量?此外,您是否使用After
钩子在场景之间关闭浏览器?@PaulMorie:我只是想节省测试时间,功能文件中的所有场景都与特定功能相关,可能有负输入正输入等。因此,对于每种不同的情况,我每次都会登录,哪个降低了性能。@是的,先生,代码已更新。这些场景是特定于会话的吗?
<?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 -----------------";
}
}