PHPUnit Automated Testing
See our PHPUnit example repository for a simple example on how to run PHPUnit tests in parallel on TestingBot.
Let's start with making sure PHP is available on your system.
For Windows:- You can download PHP for Windows from PHP For Windows.
- Run the installer and follow the setup wizard to install PHP.
sudo apt-get install curl libcurl4 libcurl4-openssl-dev php
brew install php
Installation
- Make sure you have Composer installed.
-
Install PHPUnit and the WebDriver client by running these commands in your terminal:
Copycomposer require --dev phpunit/phpunit composer require php-webdriver/webdriver
PHPUnit Example
Paste this example code in a file called WebDriverTest.php
<?php
require_once('vendor/autoload.php');
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;
class WebDriverTest extends TestCase
{
protected RemoteWebDriver $driver;
protected string $apiKey = 'API_KEY';
protected string $apiSecret = 'API_SECRET';
protected function setUp(): void
{
parent::setUp();
$caps = DesiredCapabilities::chrome();
$caps->setCapability('platformName', 'WIN11');
$caps->setCapability('browserVersion', 'latest');
$caps->setCapability('tb:options', [
'key' => $this->apiKey,
'secret' => $this->apiSecret,
'name' => $this->getName()
]);
$this->driver = RemoteWebDriver::create(
'https://hub.testingbot.com/wd/hub',
$caps,
120000
);
}
protected function tearDown(): void
{
$this->sendTestStatusToTestingBot();
$this->driver->quit();
parent::tearDown();
}
public function testTitle(): void
{
$this->driver->get('https://www.google.com/');
$this->assertEquals('Google', $this->driver->getTitle());
}
protected function sendTestStatusToTestingBot(): void
{
$sessionID = $this->driver->getSessionID();
$success = !$this->hasFailed();
$postData = http_build_query([
'test[success]' => $success ? 1 : 0,
'test[name]' => $this->getName()
]);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.testingbot.com/v1/tests/{$sessionID}");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, "{$this->apiKey}:{$this->apiSecret}");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_exec($curl);
curl_close($curl);
}
}
To run the test, please enter this command:
./vendor/bin/phpunit WebDriverTest.php
Configuring capabilities
To run your existing tests on TestingBot, your tests will need to be configured to use the TestingBot remote machines. If the test was running on your local machine or network, you can simply change your existing test like this:
Before:$caps = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create("http://localhost:4444/wd/hub", $caps, 120000);
$caps = DesiredCapabilities::chrome();
$caps->setCapability('platformName', 'WIN11');
$caps->setCapability('browserVersion', 'latest');
$caps->setCapability('tb:options', [
'key' => 'API_KEY',
'secret' => 'API_SECRET'
]);
$driver = RemoteWebDriver::create("https://hub.testingbot.com/wd/hub", $caps, 120000);
Specify Browsers & Devices
To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.
$driver = RemoteWebDriver::create("https://hub.testingbot.com/wd/hub", $caps, 120000);
To see how to do this, please select a combination of browser, version and platform in the drop-down menus below.
Testing Internal Websites
We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation for more information about this easy to use tunneling solution.
The example below shows how to easily run a PHP WebDriver test with our Tunnel:
1. Download our tunnel and start the tunnel:
java -jar testingbot-tunnel.jar key secret
2. Adjust your test: instead of pointing to 'hub.testingbot.com/wd/hub' like the example above - change it to point to your tunnel's IP address.
Assuming you run the tunnel on the same machine you run your tests, change to 'localhost:4445/wd/hub'. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.
This way your test will go securely through the tunnel to TestingBot and back:
<?php
require_once('vendor/autoload.php');
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
class WebDriverTunnelTest extends TestCase
{
protected RemoteWebDriver $driver;
protected function setUp(): void
{
parent::setUp();
$caps = DesiredCapabilities::firefox();
$caps->setCapability('platformName', 'WIN11');
$caps->setCapability('browserVersion', 'latest');
$caps->setCapability('tb:options', [
'key' => 'API_KEY',
'secret' => 'API_SECRET',
'name' => $this->getName()
]);
// Point to the tunnel running on localhost:4445
$this->driver = RemoteWebDriver::create(
'http://localhost:4445/wd/hub',
$caps,
120000
);
}
public function testTitle(): void
{
$this->driver->get('https://www.google.com/');
$this->assertEquals('Google', $this->driver->getTitle());
}
}
Run tests in Parallel
Parallel Testing means running the same test, or multiple tests, simultaneously. This greatly reduces your total testing time.
You can run the same tests on all different browser configurations or run different tests all on the same browser configuration.
TestingBot has a large grid of machines and browsers, which means you can use our service to do efficient parallel testing. It is one of the key features we provide to greatly cut down on your total testing time.
To run tests in parallel, we recommend using Paratest, which makes it very easy to run multiple PHPUnit tests simultaneously.
vendor/bin/paratest -p 8 -f --phpunit=vendor/bin/phpunit WebDriverTest.php
The command above will run your tests in 8 separate processes (8 tests at once).
Queuing
Every plan we provide comes with a limit of parallel tests.
If you exceed the number of parallel tests assigned to your account, TestingBot will queue the additional tests (for up to 6 minutes) and run the tests as soon as slots become available.
Mark tests as passed/failed
As TestingBot has no way to determine whether your test passed or failed (it is determined by your business logic), we offer a way to send the test status back to TestingBot. This is useful if you want to see if a test succeeded or failed from the TestingBot member area.
You can use our PHP API client to report back test results.
$api = new TestingBot\TestingBotAPI($apiKey, $apiSecret);
$api->updateJob($driver->getSessionID(), ['name' => 'mytest', 'success' => true]);
Other PHP Framework examples
-
Behat
Behat is a BDD framework which runs on PHP.
Mink is used for its browser emulation and works nicely together with Behat. -
Codeception
Codeception is a BDD-styled PHP testing framework.
This testing framework offers good Selenium support. -
Laravel Dusk
Laravel Dusk provides an easy-to-use browser automation testing framework.
-
PHPUnit
PHPUnit is the most popular unit testing framework for PHP.
It comes with good Selenium WebDriver support and is easy to set up. -
SimpleTest
SimpleTest is a framework for unit testing, web site testing and mock objects for PHP.