[英]PHPUnit and Selenium - run tests from another class
我正在使用PHPUnit和Selenium來測試我的Web應用程序。
目前,我有2個測試類-UserTest和PermissionsTest 。
例如,我可能會關閉“創建用戶”權限,然后測試是否已禁用“創建用戶”按鈕。 但是,如果我重新打開“創建用戶”權限,則我想測試是否可以創建用戶。
能夠創建用戶的所有邏輯已經在UserTest類中了-那么有什么方法可以從PermissionsTest類的UserTest類運行測試?
目前,我正在嘗試以下代碼:
public function testUserPermission(){
$userTest = new UserTest();
if($this->hasPermission = true){
$userTest->testCanCreateUser();
}
}
但是,當我運行此測試時,出現錯誤"There is currently no active session to execute the 'execute' command. You're probably trying to set some option in setup() with an incorrect setter name..."
謝謝!
解決方法如下:
//instantiate and set up other test class
$userTest = new UserTest();
$userTest->setUpSessionStrategy($this::$browsers[0]);
$userTest->prepareSession();
//carry out a test from this class
$userTest->testCanCreateUser();
這很好。 我不明白為什么在這種情況下使用另一個測試類的功能不是一個好主意,因為如果不這樣做,我只需要將該功能重寫到我的新類中,這似乎不太“純粹”。 。
對於硒1(RC),
我改為進行了以下修改(以及應用“頁面對象”設計模式):
特定考試班
//instantiate and set up other test class
$userTest = new UserTest($this->getSessionId());
//carry out a test from this class
$userTest->createUser();
//asserts as normal
$userTest->assertTextPresent();
...
基礎頁面對象類
class PageObject extends PHPUnit_Extensions_SeleniumTestCase {
public function __construct($session_id) {
parent::__construct();
$this->setSessionId($session_id);
$this->setBrowserUrl(BASE_URL);
}
}
特定頁面對象類
class UserTest extends PageObject {
public function createUser() {
// Page action
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.