8

How can I run a test "within PHP" instead of using the 'phpunit' command? Example:

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

Or do I have to write the test to a file, invoke phpunit via system()/exec() and parse the output? :(

2 Answers 2

4
+50

Just use the Driver that's included.

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

Then you need to set it up like 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']);

Then just:

$driver->open('/');
$driver->click("//a[@href='/contact/']");
Sign up to request clarification or add additional context in comments.

2 Comments

Either I don't get it, or you misunderstood me :) The purpose of this is to store testcases in a database and run them "on demand". I also want to email the results. Therefore I need a way to grab a test from the database (easy enough), run the testcase and grab the results (for db storage and email contents).
@arex1337 - What's the difference?! All you need to store in DB is the test procedures (mapping methods - open(), click() etc + their parameters), not Driver configuration. Just read the test procedures from Db and apply them once you have Driver operational as the answerer shows above... The guy just told you that you don't need to create/extend any classes, just use already existing library/class.
3

Here's an example from the 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);
?>

The code for SimpleTestListener is on the same page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.