簡體   English   中英

PHPUnit和Selenium-從另一個類運行測試

[英]PHPUnit and Selenium - run tests from another class

我正在使用PHPUnit和Selenium來測試我的Web應用程序。

目前,我有2個測試類-UserTestPermissionsTest

  • 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..."

謝謝!

在我看來,您似乎缺少測試實現與其邏輯的分離-我不是在談論PHP問題,而是在談論通用測試模型。它將允許您在各種測試案例中重用測試組件。

您可以在此處查看有關PHP中的Page Objects的材料或一般的硒Wiki

解決方法如下:

//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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM