Testing PHP aplication is not the native task for Selenium, but thanks to facebook api developers we have a tool to make it. Facebook PHP WebDriver alows to use Selenium and even combine it with PHPUnit.
You should first read Automatic tests with Selenium – part #1.
Let’s drive
Like in the previous article, we need a Selenium server, but this time for PHP. We can get it from here. Download the newest one standalone java version and run it (the most recent at the time of writing this article is 3.3.1)
sudo wget http://selenium-release.storage.googleapis.com/3.3/selenium-server-standalone-3.3.1.jar java -jar selenium-server-standalone-3.3.1.jar
It should be running all the time, as a single process. You will also need a PHPUnit installed on your WebSever.
So we need to create some environment for testing. We will use composer. If you don’t have it installed, do it now. Create a new directory test in your localhost web directory.
In a new terminal window type
cd /path/to/test composer init
you will see a composer wizard. When it ask you for a package to search, type facebook/webdriver, like below
Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (<vendor>/<name>) [root/webdriver]: Description []: Author [picios <example@example.com>, n to skip]: Minimum Stability []: Package Type (e.g. library, project, metapackage, composer-plugin) []: License []: Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? yes Search for a package: facebook/webdriver Enter the version constraint to require (or leave blank to use the latest version): facebook/webdriver
Hit empty ENTER in another Search for a package prompt and confirm generating of a new composer.json file. Now type
composer update
Composer will take care to download the newest version of the package.
Let’s test
We need something to test, so let’s create a new html file tested_page.html in our test directory.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>The tested page</title> <meta name="description" content="The tested page"> <meta name="author" content="Me"> <!-- stylesheets assets here --> </head> <body> <h1>It works!</h1> <!-- scripts assets here --> </body> </html>
And now create a new test php file MyPageSeleniumTest.php in the same directory.
<?php require 'vendor/autoload.php'; use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\WebDriverBy; class MyPageSeleniumTest extends \PHPUnit_Framework_TestCase { public function testPage() { $driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', DesiredCapabilities::chrome()); $driver->get('http://localhost/test/tested_page.html'); $heading = $driver->findElement(WebDriverBy::tagName("h1")); $this->assertContains('It works', $heading->getText()); } }
Save it. While your Selenium server is running, type in terminal
phpunit --verbose MyPageSeleniumTest
If everything goes ok, you will see a new Chrome window with a ‘It works!’ sign. Moreover you will get your test result from PHPUnit.
OK (1 test, 1 assertion)
MeShellAngelo says:
I read both of your Selenium articles. They are pretty fast but clear. Only you should’ve mentioned, that your OS is Ubuntu. Some beginners might be confused.
Picios says:
Hi MeShellAngelo. You’re right. I should’ve mentioned it. Sorry.