[英]How do I run a PHPUnit Selenium test without having a new browser window run for each function?
我正在嘗試使用PHPUnit運行selenium測試用例。 我做的第一件事是嘗試登錄功能,這是完美的,但我想運行一個函數來檢查登錄后頁面上的信息,但它打開一個新的瀏覽器,而不是繼續在當前的瀏覽器窗口。 這是一個問題的原因是因為頁面設置為在窗口關閉時刪除登錄身份驗證,所以如果你使用$ this-> url()轉到頁面,它會給出我需要登錄的錯誤。 這是我的代碼,它啟動瀏覽器並運行測試登錄表單的功能,然后關閉瀏覽器,打開一個新的並運行鏈接檢查。 這當然會因為身份驗證錯誤而導致錯誤,因為窗口已關閉。 我可以在一個函數中運行所有測試,但這是非常草率的編碼,我想避免這種情況。 有誰知道如何解決這個問題?
<?php
class TestMyTest extends PHPUnit_Extensions_Selenium2TestCase {
public function setUp()
{
$this->setBrowser("firefox");
$this->setBrowserUrl("https://**************************");
}
public function testLoginForm()
{
$this->url("login.php");
$this->byLinkText('Forgot your password?');
$form = $this->byCssSelector('form');
$this->byName('username')->value('test');
$this->byName('password')->value('1234');
$form->submit();
}
public function testCheckForMainMenueLinks ()
{
$this->url("index.php");
$this->byLinkText('Home');
$this->byLinkText('Products');
$this->byLinkText('About us');
$this->byLinkText('Contact');
}
}
?>
要在Selenium2TestCase
共享瀏覽器會話,您必須在初始瀏覽器設置中設置sessionStrategy => 'shared'
:
public static $browsers = array(
array(
'...
'browserName' => 'iexplorer',
'sessionStrategy' => 'shared',
...
)
);
替代方案(默認)是'isolated'
。
Okej所以我想你可以直接從另一個函數調用這個函數,如下所示:
public function testOne
{
#code
$this->Two();
}
public function Two()
{
#code
$this->Three();
}
public function Three()
{
#code
}
等等,這將只運行下一個沒有新瀏覽器的功能,但是,如果它在任何測試中的任何地方失敗,整個測試都會停止,因此反饋不會像個別測試一樣好。
在一個函數中創建斷言,因為這是功能測試。 我也是phpunit和selenium的新手,但我成功測試了所有這樣的:
public function testAuth(){
$this->open('register.php&XDEBUG_SESSION_START=PHPSTORM');
$this->assertTextPresent('Register');
$this->type('name=email', "...");
$this->type('name=firstname', "...");
$this->type('name=lastname', "...");
$this->type('name=password', "...");
$this->type('name=verifyPassword', "...");
$this->click("reg-butt");
$this->waitForPageToLoad("5000");
$this->assertTextPresent('Profile');
$this->open('logout.php');
$this->assertTextPresent('text from redirect page');
$this->open('login.php');
.....
}
設置會話共享的一種優雅方式是使用PHPUnit的setUpBeforeClass()
方法:
public static function setUpBeforeClass()
{
self::shareSession(true);
}
您可以調用PHPUnit_Extensions_SeleniumTestCase :: shareSession(true)來啟用瀏覽器窗口重用。
在手冊中它說:
從Selenium 1.1.1開始,包含一個實驗性功能,允許用戶在測試之間共享會話。 唯一支持的情況是在使用單個瀏覽器時在所有測試之間共享會話。 在引導程序文件中調用PHPUnit_Extensions_SeleniumTestCase :: shareSession(true)以啟用會話共享。 如果沒有成功的測試(失敗或不完整),會話將被重置; 用戶可以通過重置cookie或從測試中的應用程序注銷(使用tearDown()方法)來避免測試之間的交互。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.