Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Getting Started With Selenium Testing using PHP

We all know that Selenium can be used to perform automated tests. Some of us may even already have a perfectly working testing solution that uses it. However, if you're like me, then you just never really got round to it because getting the tool working and writing the tests was such a pain in the butt. This tutorial aims to address that by giving instructions on using Selenium from scratch, which includes downloading Selenium and the necessary browser driver before then writing and running a test that was written in PHP (which I believe is much easier than using Java).

Walkthrough

Below is a video of me performing this tutorial myself on a freshly installed Ubuntu 16.04 desktop.

Steps

Download the latest selenium standalone server from this page. At the time of writing this tutorial, it is version 3.8.1

wget https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar

For the next step, You may require installing some php extensions

sudo apt-get install php7.2-zip php7.2-curl -y

Install composer if you haven't got it already. Then use it tonstall the php webdriver from facebook.

composer require facebook/webdriver

Run the selenium server with passthrough disabled. You may need to install Java if you haven't got it already.

java -jar selenium-server-standalone-3.141.59.jar -enablePassThrough false

Get the latest chrome driver from here.

wget https://chromedriver.storage.googleapis.com/2.34/chromedriver_linux64.zip
unzip https://chromedriver.storage.googleapis.com/2.34/chromedriver_linux64.zip
sudo mv -i chromedriver /usr/bin/.

Get the gecko driver from the releases page for Firefox.

wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz
tar --extract --gzip --file gecko*
sudo mv -i geckodriver /usr/bin/.
<?php

require_once(__DIR__ . '/vendor/autoload.php');

$host = 'http://localhost:4444/wd/hub'; // this is the default
$USE_FIREFOX = true; // if false, will use chrome.

if ($USE_FIREFOX)
{
    $driver = Facebook\WebDriver\Remote\RemoteWebDriver::create(
        $host, 
        Facebook\WebDriver\Remote\DesiredCapabilities::firefox()
    );
}
else
{
    $driver = Facebook\WebDriver\Remote\RemoteWebDriver::create(
        $host, 
        Facebook\WebDriver\Remote\DesiredCapabilities::chrome()
    );
}

$driver->get("http://www.google.com");

# enter text into the search field
$driver->findElement(Facebook\WebDriver\WebDriverBy::id('lst-ib'))->click();
sleep(1);
$driver->findElement(Facebook\WebDriver\WebDriverBy::id('lst-ib'))->sendKeys('programster selenium');
sleep(1);

# Click the search button
$driver->findElement(Facebook\WebDriver\WebDriverBy::name('btnK'))->click();

References / Resources

Last updated: 26th February 2021
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch