【问题标题】:Run a PHPUnit Selenium test case programatically ("within PHP")以编程方式运行 PHPUnit Selenium 测试用例(“在 PHP 内”)
【发布时间】:2011-06-30 04:27:51
【问题描述】:

如何在“PHP 内”运行测试而不是使用“phpunit”命令?示例:

<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
class MySeleniumTest extends PHPUnit_Extensions_SeleniumTestCase {

    protected function setUp() {
        $this->setBrowser("*firefox");
        $this->setBrowserUrl("http://example.com/");
    }

    public function testMyTestCase() {
        $this->open("/");
        $this->click("//a[@href='/contact/']");
    }

}

$test = new MySeleniumTest();
//I want to run the test and get information about the results so I can store them in the database, send an email etc.
?>

或者我是否必须将测试写入文件,通过 system()/exec() 调用 phpunit 并解析输出? :(

【问题讨论】:

    标签: php selenium phpunit


    【解决方案1】:

    只需使用随附的Driver

    require_once 'PHPUnit/Extensions/SeleniumTestCase/Driver.php';
    //You may need to load a few other libraries.  Try it.
    

    那你需要像SeleniumTestCase does这样设置:

    $driver = new PHPUnit_Extensions_SeleniumTestCase_Driver;
    $driver->setName($browser['name']);
    $driver->setBrowser($browser['browser']);
    $driver->setHost($browser['host']);
    $driver->setPort($browser['port']);
    $driver->setTimeout($browser['timeout']);
    $driver->setHttpTimeout($browser['httpTimeout']);
    

    那么就:

    $driver->open('/');
    $driver->click("//a[@href='/contact/']");
    

    【讨论】:

    • 要么我不明白,要么你误解了我 :) 这样做的目的是将测试用例存储在数据库中并“按需”运行它们。我也想通过电子邮件发送结果。因此我需要一种从数据库中获取测试的方法(足够简单),运行测试用例并获取结果(用于数据库存储和电子邮件内容)。
    • @arex1337 - 有什么区别?!您需要在 DB 中存储的只是测试程序(映射方法 - open()、click() 等 + 它们的参数),而不是驱动程序配置。只需阅读 Db 中的测试程序,并在驱动程序运行后应用它们,如上面的回答者所示......那个人刚刚告诉你,你不需要创建/扩展任何类,只需使用已经存在的库/类。
    【解决方案2】:

    这是来自phpunit docs 的示例:

    <?php
    require_once 'PHPUnit/Framework.php';
    
    require_once 'ArrayTest.php';
    require_once 'SimpleTestListener.php';
    
    // Create a test suite that contains the tests
    // from the ArrayTest class.
    $suite = new PHPUnit_Framework_TestSuite('ArrayTest');
    
    // Create a test result and attach a SimpleTestListener
    // object as an observer to it.
    $result = new PHPUnit_Framework_TestResult;
    $result->addListener(new SimpleTestListener);
    
    // Run the tests.
    $suite->run($result);
    ?>
    

    SimpleTestListener 的代码在同一页上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-12
      • 2021-08-08
      • 2011-09-21
      • 2018-05-06
      • 1970-01-01
      • 2013-08-17
      • 2020-11-01
      • 2012-04-24
      相关资源
      最近更新 更多