Automating your browser testing is very easy with using PHP and Selenium.  Below is a diagram of how it all works and the steps on how to achieve controlling your Browser with PHP!

Automating Browser Testing with PHP and Selenium
Automating Browser Testing with PHP and Selenium

Steps to install the basic components

  1. Install PHP on your machine download the PHP executable and set its path up in your environment (not sure how to do that?).
  2. Install Composer which will be used to download the source code for the facebook web driver
  3. Download the Selenium Standalone Server here http://www.seleniumhq.org/download/ (Direct link: selenium-server-standalone-2.53.1.jar)
  4. Run the selenium server by double clicking the jar file or invoking it from the command line..  You could also run this in your php script if you put the jar file in the same directory as the php script: exec(“java -jar “.__DIR__.”\selenium-server-standalone-2.53.1.jar”);
  5. Create a composer.json file https://gist.github.com/phpdave/580e5666c1d78d12e66e
  6. Run composer install using the composer.json file – This will download the facebook web driver
  7. Create an example PHP script on your computer.  https://github.com/facebook/php-webdriver/blob/community/example.php
  8. Run the script on the command line C:\myscripts\> php example.php
  9. This should start your browser up and start running the example script code.  You could then use the web driver to screen scrape, enter in various input and take various actions that are needed to test your application out.  You’ll also have the power of PHP to do various things such as save the results of your test to a file and all this is done in PHP!  So you don’t have to learn another language.  You’ll also want to get comfortable with firefox’s developers tools so you can inspect an element and get a unique selector for that element so you know how to interact with that element.

Note:

  • Supported Platforms: Windows, OSX, Linux.  Not supported on the IBMi.  This makes sense since browsers run on a users machine not on the i.  Therefore to automate you may have to setup a Linux VM on a server and have it called to test.  Or you can run the code on your local machine http://www.seleniumhq.org/about/platforms.jsp

Referencee
www.seleniumhq.org/download/

Adding PHP formatter to Selenium IDE firefox plugin to allow copying and pasting commands in PHP format

  1. Install the plugin here: https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-php-formatters/
  2. In Selenium IDE go to Options->Clipboard Formatting-> PHP (PHPUnit)

This will give you output like this:

$this->open("/Page1/");
$this->type("name=field2", "99999");
$this->click("name=Submit");

which you’ll need to modify into this format if your using the PHP FB webdriver:

$driver->get("/Page1/");
$driver->findElement(WebDriverBy::name('field2'))->sendKeys("99999");
$driver->findElement(WebDriverBy::name('submit'))->click();

Adding a user extension to selenium IDE to add a new function

  1. Modify the user-extension.js that the selenium ide is using and add a function like this that will do an random email:
Selenium.prototype.doTypeRandomEmail = function(locator) {
    /**
    * Sets the value of an input field to a random email id,
    * as though you typed it in.
    *
    * @param locator an <a href="#locators">element locator</a>
    */

    // All locator-strategies are automatically handled by "findElement"
    var element = this.page().findElement(locator);

    /* The following block generates a random email string */
    var allowedChars = "abcdefghiklmnopqrstuvwxyz";
    var stringLength = 8;
    var randomstring = '';

    for (var i=0; i<stringLength; i++) {
        var rnum = Math.floor(Math.random() * allowedChars.length);
        randomstring += allowedChars.substring(rnum,rnum+1);
    }

    // Append a domain name
    randomstring += "@example.com"

    // Replace the element text with the new text
    this.browserbot.replaceText(element, randomstring);
};
  1. Then you can just call the function like this:
$this->typeRandomEmail("id=email");

Setting it up on an Amazon Linux EC2 instance

Follow this example to setup PHP on EC2 if you don’t already have a linux box to play with

Open the ports 7055 and 4444 on your EC2 server instance’s security group inbound rules

SSH into your Linux box run:

#Setup FireFox on Amazon Linux
curl -X GET -o RPM-GPG-KEY-lambda-epll https://lambda-linux.io/RPM-GPG-KEY-lambda-epll
sudo rpm –import RPM-GPG-KEY-lambda-epll
curl -X GET -o epll-release-2015.09-1.1.ll1.noarch.rpm https://lambda-linux.io/epll-release-2015.09-1.1.ll1.noarch.rpm
sudo yum -y install epll-release-2015.09-1.1.ll1.noarch.rpm
sudo yum –enablerepo=epll install firefox-compat
wget -O firefox-latest.tar.bz2 "https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=en-US"
bzcat firefox-latest.tar.bz2 | tar xvf –
#setup Xvfb – X virtual framebuffer is a display server implementing the X11 display server protocol
Xvfb :1 -screen 0 1280x768x24 &
export DISPLAY=:1
#Setup and run Selenium. Note append the version number to selenium
mkdir download
cd download
wget -O selenium 'http://goo.gl/qTy1IB'
java -jar "selenium" -Dwebdriver.firefox.bin="/home/ec2-user/download/firefox/firefox" &
#Run PHP example
mkdir projects
cd projects
mkdir seleniumphptest
cd seleniumphptest/
curl -sS https://getcomposer.org/installer | php
php composer.phar require facebook/webdriver
wget "https://raw.githubusercontent.com/facebook/php-webdriver/community/example.php"
php example.php
#Array
#(
# [0] => Array
# (
# [name] => cookie_name
# [domain] => docs.seleniumhq.org
# [secure] =>
# [path] => /
# [value] => cookie_value
# [class] => org.openqa.selenium.Cookie
# [httpOnly] =>
# [hCode] => -1723267194
# )
#
#)
#The title is 'About Selenium'
#The current URI is 'http://docs.seleniumhq.org/about/&#39;

References: https://lambda-linux.io/blog/2015/01/28/announcing-firefox-browser-support-for-amazon-linux/