Andrew Cavanagh

Developer.  Project Architect.  Huge Nerd.

(not necessarily in that order)

We have a situation where we need to run a large number of behat tests that require javascript and therefor need a browser. For a variety of reasons we don’t want to use a service like Saucelabs and PhantomJs had some imperfect rendering issues that made for poor screenshots, making it unsuitable for our use case. To that end we’re setting up worker EC2 instances to run the tests. The basic, stripped down version of how to accomplish this on AWS stock ubuntu AMI:

  • install the necessary packages: apt-get install xvfb firefox openjdk-7-jre-headless
  • get the selenium server: wget http://selenium-release.storage.googleapis.com/2.48/selenium-server-standalone-2.48.2.jar. You’ll probably want to put it somewhere sensible. —NOTE: I found that there were issues when using an earlier version of selenium. Version matters here.
  • your behat.yml file should look something like this:
default:
  context:
    class: 'FeatureContext'
  paths:
    features: 'features'
    bootstrap: 'features/bootstrap'
  extensions:
    Behat\MinkExtension\Extension:
      javascript_session: selenium2
      goutte: ~
      selenium2:
        wd_host: http://localhost:4444/wd/hub

This is assuming that you’re doing this on a server that has your project on it, and that your project has behat installed, etc. At this point you should be able to run your tests on the headless server with minimal fuss using a command like this to start selenium in the background: DISPLAY=:1 xvfb-run -e /dev/stdout -a --server-args="-screen 0 1280x1024x8" java -jar ./selenium-server-standalone-2.48.2.jar &, and then running the tests as normal (ie, bin/behat --tags=@javascript).

This allows for running browser tests in CI, which is great. In our situation it lets us script out all of the set up and put the EC2 instances that are doing the work behind some autoscaling. Since we have lots of users sometimes running loads of tests concurrently, this is fairly important. It also allows for much better browser interaction and higher quality screenshots than using something like PhantomJs, which is critical for our use case. Note that these instructions are generic and minimalistic - anyone following them will very likely need to tweak and adapt the code samples to work for their particular situations. For instance, when setting it up in my local dev environment (I use homestead), I also needed to apt-get install libgconf-2-4.

Extra Credit: if you need to kill the selenium process, use lsof -i -n -P | grep 4444 and kill the process it says is on :4444.


comments powered by Disqus