1

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?
1

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/

  • Great, thanks - both good advice. I'm new to acceptance tests and the concepts feel foreign. Any chance you have a link to somewhere which gives guidance on what sort of tests to write? E.g ideally a set of feature files for a small web app? I've found precious little in the Behat docs. I should look for a book that has a large chapter on acceptance tests! I'm using Growing OO s/w Guided by Tests (Freeman & Pryce) – CL22 Jul 27 '15 at 19:34
  • Not exactly the kind of examples you've asked for, but see edit. – Sam Burns Jul 27 '15 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.