Understanding Strategy

Understanding Strategy

Understanding Strategy

Let’s start by Understanding Strategy. But before we understand Strategy, let’s have a look at Capybara. It provides developers to simulate a user on a web page as well as make assertions based on the content and environment of the page. Moreover, it also offers an API to interact with the web page.

Not to mention, when trying to find an element either using the DSL or xpath/CSS selectors. However, it is common to have two or more matches which will cause Capybara to fail with an Ambiguous match error. Also, Capybara can perform partial matches, leading to unexpected results.

Using Capybara.match and the equivalent match option, you can control how Capybara behaves when multiple elements all match a query.

 Four different strategies built into Capybara:

  • first: Just picks the first element that matches.
  • one: Raises an error if more than one element matches.
  • smart: depends on the value for Capybara.exact. If set to true, it will behave like :one, raises an error if more than one element matches. Otherwise, it will first search for exact matches. If multiple matches are found, an ambiguous exception is raised. If none are found, it will search for inexact matches which allows partial matches and again raise an ambiguous exception when multiple inexact matches are found.
  • prefer_exact: Finds all matching elements, but will return only an exact match discarding other matches. If multiple matches are found, some of which are exact, and some of which are not, then the first exactly matching element is returned.

The default for Capybara.match is :smart. To emulate the behaviour in Capybara 2.0.x, set Capybara.match to :one. To emulate the behaviour in Capybara 1.x, set Capybara.match to :prefer_exact.

More importantly,  if you want to understand and have a clear view of the topic, visit the link mentioned below.

Make your resume stand out and become a Certified Capybara Testing Professional. Try free practice tests here!

A great career is just a certification away. So, practice and validate your skills to become Certified Capybara Testing Professional

Exactness
what is Scoping?

Get industry recognized certification – Contact us

keyboard_arrow_up