以编程方式运行PHPUnit Selenium测试用例(“在PHP内”)

时间:2011-02-18 23:49:26

标签: php selenium phpunit

如何在“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并解析输出? :(

2 个答案:

答案 0 :(得分:4)

只需使用随附的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/']");

答案 1 :(得分:3)

以下是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的代码在同一页面上。