SW Designs

Technology and Photography

Behat Monolog Extension

Good application logging is invaluable. If offers insight into what your application is doing under normal conditions as well as somewhere to log information about errors. Combined with tools such as Graphite or Splunk, you can create real-time dashboards showing how your application is performing. Not only does this aid developers, but also operational teams to indentify abnormal patterns quickly.

These benefits also apply to automated test suites. They start off small and easy to understand. As they grow, testing more components and requiring more fixtures, they can become difficult to understand. Take an online shop for example, fixturing and testing the product catalogue is probably significantly more straightforward compared to completing a purchase.

To help those creating Behat test suties, I’ve create a Monolog extension for it. It works by injecting a log channel into each Behat Context, including Sub-Contexts. The name of the logger object is configurable, as are the handlers, although only the Stream hanlder (for writing to files) is supported for now. To get you up ‘n running quickly, there’s also an example project demostrating howto use it.

Below is the output when the stream hanlder is figured to write to php://stdout,

Any feedback, comments or suggestions are welcome.

Git Completion With Mac OS X

On every fresh OS X install, I always end-up googling how-to setup Git completion on the command line. Almost every article I read suggests cloning the Git source code to acquire the necessary bash script. That’s not necessary if you have the offical Git package installed as you’ll already have the file under /usr/local/git/.

To add it to your bash profile,

1
echo 'source /usr/local/git/contrib/completion/git-completion.bash' >> ~/.bash_profile

While you’re there you can load /usr/local/git/contrib/completion/git-promot.bash too. Instructions for setting PS1 are in the file.

Wireless Raspberry Pi

I run my Raspberry Pi as a headless server using ArchLinux. This post runs through how-to setup a wireless network interface using the Edimax EW-7811UN (£9.20 from Amazon) and have it connect automatically on boot, all without leaving the command line.

This post is just as much for my benefit as it is for others. It probably won’t be long before I break something and have to install the OS!

Lets get started,

  1. Ensure you have a recent release of Archlinux – I installed archlinux-hf-2012-09-18 which includes Linux 3.2 and the Edimax driver.

  2. Install the wireless management tools

1
sudo pacman -S wireless_tools iwconfig
  1. Create /etc/wpa_supplicant.conf and add your wifi settings,
1
2
3
4
5
6
7
8
9
10
ctrl_interface=/var/run/wpa_supplicant
network={
 ssid="your network name"
 scan_ssid=1
 proto=WPA2
 key_mgmt=WPA-PSK
 pairwise=CCMP
 group=TKIP
 psk=your passphase key
}
  1. Manually bring up your wireless interface to check your settings
1
sudo wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant.conf -d
  1. Then run /sbin/ipconfig wlan0 to check whether it’s connected, if is says ‘not associated’ it failed to connect. Once your connected, you and request an IP address from your router with,
1
sudo dhcpd wlan0

Run ipconfig again and you should see your IP address.

  1. Now we can move onto configuring your RPi to automatically connect on bootup. To do this, we’ll create a network profile using netcfg and configure systemd to start it during bootup.
1
sudo pacman -S netcfg
  1. Define a network profile, create /etc/network.d/wlan and enter the following details
1
2
3
4
5
6
7
8
CONNECTION='wireless'
DESCRIPTION='any description'
INTERFACE='wlan0'
SECURITY='wpa-config'
WPA_CONF='/etc/wpa_supplicant.conf' ; this is the file we created earlier
IP='dhcp'
WPA_DRIVER='wext'
NETCFG_DEBUG=yes
  1. Now register your network profile with netcfg by adding the above filename to the NETWORKS list /etc/conf.d/netcfg,
1
NETWORKS=(wlan)
  1. Finally configure systemd to start netcfg during bootup,
1
systemctl enable netcfg.service

International Space Station Flyby

Providing the night sky is clear, the International Space Station is visible from as early as 21:30 for the next couple of weeks. It only takes a couple of minutes to across the sky, so you need to be ready for it! For exact times, see the Heaven Above website.

If your astronavigation skills are anything like mine, I highly recommend Stellarium to help find your way around the night sky. It is available for Mac OS X, Linux and Windows.

Here are my better pics from Friday night,

Multiple Sessions With Behat, Mink and Selenium2

At work, we have a few scenarios where acceptance tests require two or more users to interact with each other on a website. An example of this is multiple users joining a poker table and playing against each other.

Mink doesn’t fully support this out-of-the-box, but with a bit of effort multi-session scenarios can be achieved, albeit with a few gotchas. For the purposes of this blog, I’ve created a sample scenario based around a Web Sockets demo app at html5labs.cloudapp.net.

Lets start with some suitable Gherkin. Note the step reuse for the ‘Given’ sections and that the user profile will be passed to the step as an argument.

ConcurrentUsers.featureGitHub
1
2
3
4
5
Scenario: Simple 1-way chat
 Given "User1" enters the chat room
 And "User2" enters the chat room
 When "User1" posts a message
 Then "User2" should see that message

And now for some code. There’s a lot going on below, first we retrieve the default Mink session (as configured in behat.yml) and clone it. At this point we have two session objects linked to the same Selenium2 session. To correct this and provide session isolation, we instantiate and set a fresh WebDriver object before finally registering the new session with Mink and restarting. Okay, this is a bit hacky, but stay with me.

Now we have multiple sessions registered with Mink and subsequent steps will be passed the appropriate session name as defined in the Gherkin. These steps can simply reference the required session with $this->getSession($user).

I mentioned some gotchas previously. They are,

  • This code is Selenium2/Web Driver specific, however, broadly speaking the same approach should work with other drivers too.
  • Mink auto-starts the default session when you first call getSession(). This means a total of 3 browser instances are used for this test.
  • I’ve duplicated some Selenium2 config in behat.yml for ease. I couldn’t figure how-to ‘ask’ Mink for another Selenium2 driver instance
  • The standard MinkContext steps can’t be used as they only work with the default Mink session. UPDATE: Switching the default session name within a step with $this->getMink()–>setDefaultSessionName($name) means the MinkContext continue to work. I’ve updated the code on GitHub to reflect this.
  • Probably more

Clearly this isn’t the cleanest implementation of multiple sessions, however it does prove the concept works while retaining the focus on describing behaviour in the Gherkin. I suspect the limitations of MinkContext and custom sessions can be resolved with a simple patch which I hope to raise over the next few days.

The full source code is available on GitHub. Any ideas/suggestions/improvements are welcome!