Skip to content

Using Queen with Selenium

ozanturgut edited this page Jan 28, 2013 · 8 revisions

Before you begin, run npm install -g queen to ensure you have the latest version of Queen.

Queen can use a Selenium Server to automatically spawn a set of browsers and keep watch on them, restarting them automatically if they become unresponsive. This tutorial walks you through the steps nescessary.

Set up a Selenium Server

If you already have a server setup, you can skip this section.

  1. Download selenium-server-standalone-2.28.0.jar

This is the Selenium server software, we'll use it both for setting up the Hub and Node.

  1. Start the Hub by executing: java -jar selenium-server-standalone-2.28.0.jar -role hub

This will start the Hub on port 4444. Make sure the Selenium jar is in the current folder for this command to work.

  1. Start the Node by executing: java -jar selenium-server-standalone-2.28.0.jar -role node

Because we're using the default configuration, the node will be able to find and connect to the hub automatically.

Create the queenConfig.js file

We'll configure Queen to connect to the Hub we started through the use of a queenConfig.js file. This file does pretty much the same thing that the command-line options allow you to do, with the additional benefit of defining populators, such as Selenium.

Copy this to a file named queenConfig.js:

// queenConfig.js

module.exports = {
  // By default Queen captures browsers on port 80, but this requires
  // admin privilages on many operating systems, so for this example we'll
  // configure queen to use port 9000
  capture: ":9000",

  // This configuration option takes an object, or an array of objects
  // if we were using more than one type of populator (ex. Sauce)
  populator: [
    // Note that we're in an array, we could provide more populator configs
    // if we wanted
    {
      type: "selenium",

      // If your Selenium Server is hosted else where, change this value accordingly
      config: { host: "localhost:4444" },

      // This tells Queen to request a Firefox browser from the Hub once
      // we connect to it. You may specify any webdriver capability in
      // http://code.google.com/p/selenium/wiki/DesiredCapabilities
      clients: [
        { browserName: "firefox" }
      ]
    }
  ]
};

Start Queen

You can now start Queen by executing queen when in the directory the queenConfig.js file.

Once started, Queen will automatically connect to the hub and request that it spawn a Firefox browser and point it at Queen's capture URL. Once captured, Queen will ensure that the browser remains in a healthy state. If the browser becomes unresponsive, Queen will ask the Hub to start a new one and kill the old one. You can see this in action by closing the Firefox window -- within a few seconds another one will pop up.

Run your scripts

You can now use Queen as you would normally. If we wanted to run a test script, we could do so by executing: queen -r [QUEEN_REMOTE_SERVER_HOST] http://queenjs.com/server-example.js where [QUEEN_REMOTE_SERVER_HOST] is the address queen is listening to for remote connections (it's outputted to the log when queen starts as [Queen Remote Server] Accepting connections on [QUEEN_REMOTE_SERVER_HOST].

Next Steps

We've just scratched the surface of using Selenium with Queen. Here are some other things you can do:

  • The objects in the clients array can have any of the options defined http://code.google.com/p/selenium/wiki/DesiredCapabilities
  • You can define as many clients in the clients array as the Selenium Server can support
  • You can add additional populator configs to the populator array, including multiple selenium servers. Queen will deal with each of them independently.
  • Find out more about the configuration options for Selenium at the Grid2 documentation wiki.

Clone this wiki locally