PHP,使用 Behat 和 Mink(Selenium2 驱动程序)的 BDD:如何以编程方式在测试结束时关闭浏览器 window?
PHP, BDD with Behat and Mink (Selenium2 driver): How to close the browser window at the end of the test programatically?
我正在尝试在 PHP 和 Behat and Mink and I am using Selenium2 driver 中学习 BDD。
我的问题 一旦测试结束,如何以编程方式关闭 Selenium2 打开的每个浏览器 window/tab?由于每次测试后关闭这么多浏览器 windows/tabs 非常耗时。
我看到有一些选项允许在 Java 中执行相同的操作,Selenium2 的本地语言(我也在使用 .jar 文件),但在 PHP 中我找不到等效项。
注:
有关更多信息,我已经包含了我正在尝试的代码。
我正在尝试在 this page 上为 example.feature
写 FeatureContext.php
,即:
# features/search.feature
Feature: Search
In order to see a word definition
As a website user
I need to be able to search for a word
Scenario: Searching for a page that does exist
Given I am on "/wiki/Main_Page"
When I fill in "search" with "Behavior Driven Development"
And I press "searchButton"
Then I should see "agile software development"
Scenario: Searching for a page that does NOT exist
Given I am on "/wiki/Main_Page"
When I fill in "search" with "Glory Driven Development"
And I press "searchButton"
Then I should see "Search results"
我的FeatureContext.php
如下:
<?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;
/**
* 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()
{
$this->driver = new \Behat\Mink\Driver\Selenium2Driver('firefox');
$this->session = new \Behat\Mink\Session($this->driver);
$this->session->start();
}
/**
* @Given I am on :url
*/
public function iAmOn($url)
{
$this->session->visit('http://en.wikipedia.org'.$url);
}
/**
* @When I fill in :field with :text
*/
public function iFillInWith($field, $text)
{
$this
->session
->getPage()
->find('css', '[type=' . $field . ']')
->setValue($text);
}
/**
* @When I press :button
*/
public function iPress($button)
{
$this
->session
->getPage()
->find('css', '[id=' . $button . ']')
->press();
}
/**
* @Then I should see :text
*/
public function iShouldSee($text)
{
$title = $this
->session
->getPage()
->find('css', 'h1')
->getText();
if ($title !== $text) {
new Exception('Invalid page');
}
$this->driver->close();
}
}
尝试:
/**
* @AfterScenario
*/
public function tearDown()
{
$this->session->stop();
}
我正在尝试在 PHP 和 Behat and Mink and I am using Selenium2 driver 中学习 BDD。
我的问题 一旦测试结束,如何以编程方式关闭 Selenium2 打开的每个浏览器 window/tab?由于每次测试后关闭这么多浏览器 windows/tabs 非常耗时。
我看到有一些选项允许在 Java 中执行相同的操作,Selenium2 的本地语言(我也在使用 .jar 文件),但在 PHP 中我找不到等效项。
注:
有关更多信息,我已经包含了我正在尝试的代码。
我正在尝试在 this page 上为 example.feature
写 FeatureContext.php
,即:
# features/search.feature
Feature: Search
In order to see a word definition
As a website user
I need to be able to search for a word
Scenario: Searching for a page that does exist
Given I am on "/wiki/Main_Page"
When I fill in "search" with "Behavior Driven Development"
And I press "searchButton"
Then I should see "agile software development"
Scenario: Searching for a page that does NOT exist
Given I am on "/wiki/Main_Page"
When I fill in "search" with "Glory Driven Development"
And I press "searchButton"
Then I should see "Search results"
我的FeatureContext.php
如下:
<?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;
/**
* 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()
{
$this->driver = new \Behat\Mink\Driver\Selenium2Driver('firefox');
$this->session = new \Behat\Mink\Session($this->driver);
$this->session->start();
}
/**
* @Given I am on :url
*/
public function iAmOn($url)
{
$this->session->visit('http://en.wikipedia.org'.$url);
}
/**
* @When I fill in :field with :text
*/
public function iFillInWith($field, $text)
{
$this
->session
->getPage()
->find('css', '[type=' . $field . ']')
->setValue($text);
}
/**
* @When I press :button
*/
public function iPress($button)
{
$this
->session
->getPage()
->find('css', '[id=' . $button . ']')
->press();
}
/**
* @Then I should see :text
*/
public function iShouldSee($text)
{
$title = $this
->session
->getPage()
->find('css', 'h1')
->getText();
if ($title !== $text) {
new Exception('Invalid page');
}
$this->driver->close();
}
}
尝试:
/**
* @AfterScenario
*/
public function tearDown()
{
$this->session->stop();
}