You must enable Javascript on your browser for the site to work optimally and display sections completely.
Aug 11, 2017 5:30:36 AM mbarrus73

Problem

Have you ever needed to run a process through a browser every day? Holiday and weekends included. You didn't want to be tied to your computer to make this happen? If you could find a way to do work with the least amount of effort in the long term, would you spend a few days figuring out how to do it?

I had the same need. I spent a couple of days going through some trial an error to automate a browser without my intervention.

Read on to find out what I did and how I did it.

Proof of Concept

I need to prove that it could be done. As a developer, we have tools at our fingertips to allow for us to develop automated test to make sure that our code works. These tools include Codeception, Selenium, Fake on MacOS, Bamboo and even some web based testing solutions. One such solution is 24/7 by Zoho.

I knew the capabilities of the other test automation utilities except for Selenium. So, what did I choose? You got it! Selenium.

I was able to prove that I could automate a browser process through Selenium with the Firefox browser. Visually, I could see Firefox open, buttons clicked and text entered in.It was very satisfying.A job well done.

Prototype - Crash and Burn

Having successfully built a prototype, I decided to extend the process. Part of the overall requirement was to have this run on the server. That means that this process of opening a browser, page loads, button clicks, text entry all needs to be done without a visual browser. This is known as headless mode. Or in other words, run the process without visually opening a browser window.

To accomplish this, I chose to use PhantomJS.PhantomJS was attractive to me because of what I read about it from Selenium. The two together seemed like a good fit.

Boy, was I wrong. The same process that worked within Selenium and Firefox were radically different within Selenium and PhantomJS. Events were not getting fired. I don’t know how many hours I lost trying to get things to work. Why won’t that button click? It works in Firefox!!

Later that evening, I was doing some additional research and learned that PhantomJS is losing it support in favor of Google Chrome and ChromeDriver. What?I didn’t even know ChromeDriver existed.

Final Solution

Taking my new-found knowledge that Google has built a testing environment using Chrome, I jumped on it to see what it could do. Will it satisfy the requirements of the project? Mainly, can I run my browser automation without visually seeing the process in headless mode.

Complete success!! I could complete the process using ChromeDriver and PHP-Facebook\WebDriver.

The following is what I did to get everything to work. Please let me know if anyone of you have any questions.

Prerequisites

Make sure that the following are already installed for your operating system.

WebDriver

What is WebDriver? According to the W3C, WebDriver is a remote-control interface that enables introspection and control of user agents. It provides a platform- and language-neutral wire protocol as a way for out-of-process programs to remotely instruct the behavior of web browsers.

Facebook\WebDriver and ChromeDriver

We will be using the Chrome web browser in headless mode with the Facebook\WebDriver with PHP library and ChromeDriver.

Download the ChromeDriver executable for your operating system. This can be found at https://sites.google.com/a/chromium.org/chromedriver/. Unlike the Firefox WebDriver, the executable can be put anywhere.

Now, you can either create your own PHP project and get started by creating a composer.json file, install Facebook\WebDriver.Your composer.json file should have at least the following.

{
    "require"</strong>: {
        "facebook/webdriver": "^1.4"
    }
}

One you have a composer.json file created with the information required, run composer to update your project to include all dependencies.

composer update

You can take an easier route, however. I have created a prototype of this whole process. All you need to do is go and grab my webdriver example repository from git hub. Clone the git repository, run composer update and run the script.

php webdriver.php

That’s it folks.

Final Thoughts

There are two things that I had to do to get this working right.

  • Change putenv( ‘webdriver.chrome.driver=/opt/local/sbin/chromedriver’ ); to reference the path to where you installed your downloaded chromedriver executable.
  • Change the ‘const CONNECT_TIMEOUT_MS’ within vendor/facebook/webdriver/lib/Net/URLChecker.php to be 5000 instead of 500.

Happy Automation!!

comments powered by Disqus