Creating generic DRY methods
At this point, our test is no longer recognizable compared to what it was at the beginning of the chapter. Before we wrap it up, let's talk about the generic action used all through our tests. Throughout the test code, we use some common methods to perform actions such as clicking or typing text into a text field. These chained methods look something like this:
What if we refactor these methods a little further and create some generic private methods that can be used in a much simpler way? Let's start with the most common method used, @selenium.find_element
, and create a generic find_element
method:
Our find_element
method now accepts an element
identifier and an optional strategy
parameter. If the strategy is not specified, it will default to the CSS selector.
Note
More information about element locator strategies can be found in Chapter 2, The Spaghetti Pattern.
Now let's add two new methods that use our local find_element
method to click and type...