Как я могу запустить тест «внутри 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. ?>
Или мне нужно написать тест в файл, вызвать phpunit через system () / exec () и проанализировать вывод? 🙁
Просто используйте Драйвер, который включен.
require_once 'PHPUnit/Extensions/SeleniumTestCase/Driver.php'; //You may need to load a few other libraries. Try it.
Затем вам нужно настроить его, как это делает SeleniumTestCase :
$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 = 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 = 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 = 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 = 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 = 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 = 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/']");
функции$driver->open('/'); $driver->click("//a[@href='/contact/']");
Вот пример из документов phpunit :
<?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); ?>
-<?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 находится на одной странице.