When it comes to testing a web application, nothing beats testing than using a real browser. In my previous post, I’ve used a headless browser emulator goutte
as my web driver for behat/mink test cases. Let’s dive into testing using real browser.
UPDATE: I wanted to run most of my full tests on my VPS to save my machine some processing power. For Selenium driven tests, I’ve configured Firefox to run headless without full X. See bottom for more details.
Requirements
Make sure we already have a behat/mink stack setup on our machine. Follow my previous post about setting up behat/mink/goutte for web acceptance testing. If we have done that already, let’s proceed to the next step, setting up Selenium 2.
Selenium 2
Before using Selenium, make sure we have Java software installed. I used AlienBob’s OpenDJK for my Slackware Linux. Next, we download Selenium 2 (formerly Selenium RC Server). You can find the download link on Selenium HQ website. Current version is 2.31.0.
cd /opt/bdd wget http://selenium.googlecode.com/files/selenium-server-standalone-2.31.0.jar
Now that we have Selenium 2 jar file, we need to run it indefinitely – as long as we need it when running automated browser tests. By the way, I intend to use only Firefox so assume that we will only use Firefox. Of course there are options to use other browsers. Run it via java.
java -jar /opt/bdd/selenium-server-standalone-2.31.0.jar
Selenium 2 and Behat
Let’s use our example used in my previous post. We need to change some of our initialization files and config files to tell behat to use Selenium 2 as web driver. Below is our new behat.yml
file content.
default: extensions: Behat\MinkExtension\Extension: base_url: http://www.lysender.com goutte: ~ default_session: selenium2 browser_name: 'firefox' selenium2: capabilities: { "browser": "firefox", "version": "20.0"}
And we also need to make some adjustments to our features/bootstrap/FeatureContext.php
file to load Selenium 2 specific libraries.
<?php require_once '/opt/bdd/vendor/autoload.php'; use Behat\Behat\Context\ClosuredContextInterface, Behat\Behat\Context\TranslatedContextInterface, Behat\Behat\Context\BehatContext, Behat\Behat\Exception\PendingException; use Behat\Gherkin\Node\PyStringNode, Behat\Gherkin\Node\TableNode; use Behat\Mink\Mink, Behat\Mink\Session, Behat\Mink\Driver\Selenium2Driver, Behat\MinkExtension\Context\MinkContext; use Selenium\Client as SeleniumClient; // // Require 3rd-party libraries here: // // require_once 'PHPUnit/Autoload.php'; // require_once 'PHPUnit/Framework/Assert/Functions.php'; // /** * Features context. */ class FeatureContext extends MinkContext { /** * Initializes context. * Every scenario gets it's own context object. * * @param array $parameters context parameters (set them up through behat.yml) */ public function __construct(array $parameters) { // Initialize your context here } }
Now, let’s run again our test.
cd ~/projects/test-behat behat
When it run, it still follows the same feature/scenarios/steps stated in our header.feature
but the difference is that it uses a real browser (Firefox) as the web driver via Selenium. Below is the sample result.
lysender@darkstar-lenovo-32:~/projects/test-behat$ behat Feature: Site basic header Scenario: Site header should be present Given I am on "/" Then I should see "Lysender" in the ".head-block h1 a" element And I should see "Web developer by day, product manager by night" in the ".head-block p" element 1 scenario (1 passed) 3 steps (3 passed) 0m11.282s lysender@darkstar-lenovo-32:~/projects/test-behat$
Running Selenium and Firefox headless – without X
To run your Selenium driven tests on a headless VPS, try this tip from this site: a little madness blog. For Slackware box, follow the steps below.
- Make sure you have
Xvfb
installed. You may install it viaslackpkg
- Install Firefox.
- Run (as root) the X virtual framebuffer on a special display number – see below for details
- Set your default DISPLAY number (as ordinary user) as specified above – see below
Start your X virtual framebuffer as root by typing:
Xvfb :99 -ac
Where 99
is your custom display number. Then using a different session (as ordinary user), you set your default display number into 99
so your X dependent applications would render to that virtual display instead of an actual hardware display.
export DISPLAY=:99
Then finally run Firefox.
firefox
In case there are dependencies for either Firefox or Xvfb, you will have to install them and retry again. For convenience, you can add the export command on your bash_profile
so that you don’t have to run it every time.
That’s it for now. Enjoy and share.
got stuck in this part because of a lower selenium version.
https://code.google.com/p/selenium/issues/detail?id=6721
higher version fixed the problem for me.