【问题标题】:PHPUnit and Selenium - run tests from another classPHPUnit 和 Selenium - 从另一个类运行测试
【发布时间】:2013-10-19 07:45:10
【问题描述】:

我正在使用 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 selenium phpunit selenium-webdriver


    【解决方案1】:

    在我看来,您似乎缺少将测试实现与其逻辑分离 - 我不是在谈论 PHP 问题,而是在谈论通用测试模型。它将允许您在各种测试用例中重用您的测试组件。

    你可以看看 关于 PHP here 或一般 selenium wiki 中的页面对象的材料。

    【讨论】:

      【解决方案2】:

      解决方法如下:

      //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();
      

      这很好用。我不明白为什么在这种情况下使用另一个测试类的功能是一个坏主意,因为如果我不这样做,我将不得不将该功能重写到我的新类中,这似乎不那么“纯粹”.. .

      【讨论】:

      • 你是如何加入课程的?它是由 PHPUnit 自动加载的还是需要手动指令?
      • 我使用自动加载来包含“UserTest”类。具体来说,我使用composer创建了一个自动加载文件,并将这个文件包含在PHPUnit的“bootstrap”文件中。
      • 谢谢,你知道类似的东西是否适用于 Selenium 1?我刚刚发现setUpSessionStrategyprepareSession 只是特定于Webdriver 的命令。
      • 抱歉,我不知道 Selenium 1,因为我从未使用过它 - 希望你能找到答案!
      • 我找到了必要的命令来模拟您在回答中所做的事情 - 感谢您的帮助!
      【解决方案3】:

      对于 Selenium 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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-10-19
        • 2011-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        • 2017-03-15
        • 2019-01-06
        相关资源
        最近更新 更多