Question

I'm writing acceptance tests for a web application using Behat/Mink.

I want to know best practice for testing strings in the GUI. For example: say I want to test that a link exists on the homepage to the login screen, by looking for a link with a text, such as "Log In".

I want to make it robust; at the moment, for example, I am testing for the presence of the link by hard coding the text of the link into the scenario:

Feature: Website Nagivation
  To use the website
  As a website visitor
  The nagivation needs to work

Scenario: Check that the login link exists on the homepage
  Given I am on "/"
  Then I should see a link labelled "Log In"

Obviously this is really fragile. If the link's text is changed to "Sign In" then the test would break. (Among other problems, e.g. if the site is internationalised, the problem would be multiplied).

What strategies can I use for this? For example:

  • Should I keep the data DRY, so have the strings used to populate the GUI come from the same place as the strings used to test, and refer to the string by some unique identifier?
  • How could this be made most user-friendly for the writers/readers of the Gherkin/Behat acceptance tests?
Was it helpful?

Solution

Behat is at its best when used a Behaviour Driven Development tool. Try to think of your feature files as requirements, not tests. Change the requirements first, then alter your system to get the tests passing!

To answer specifically your question about languages, if you do have a multi-language site, try putting an extra step like 'Given I'm an Englishman' at the top of your Scenario.

Edit: Bare in mind that Behat suites can become unwieldy if they're too tightly coupled to the UI all the time though. Consider doing more 'service-level' testing and less 'UI' testing for larger systems. See: http://sam-burns.co.uk/108/testing-pyramid/

Licensed under: CC-BY-SA with attribution
scroll top