Recipe 3Run Slow Setup/Teardown Code with Global Hooks

Problem

You need to do something that takes a while before your first test, such as launching a browser or waiting for a desktop application to load. You’re familiar with Cucumber’s Before hook, which runs once per scenario. But you want something that runs just once overall so that your setup code doesn’t slow down your test too much.

Ingredients

  • Cucumber’s built-in env.rb file for setup code

  • Ruby’s built-in at_exit hook for teardown code[22]

  • The Selenium WebDriver browser automation library[23]

  • The Firefox web browser[24]

Solution

This recipe starts with a simple web testing project. Before we make our improvements, the code to start and stop the web browser executes inside regular ...

Get Cucumber Recipes now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.