Write tastier tests.
dill to your
Gemfile. Then add
require 'dill/cucumber' to
- We prefix the file name with
00_to ensure it's loaded as soon as possible during cucumber boot.
See the docs for master.
The widgets API is an implementation of the Page Object pattern, with a twist.
Your First Widget
Given the following HTML:
<body> <span id="my-widget">My Widget</span> </body>
You can define a widget as follows:
class MyWidget < Dill::Widget root '#my-widget' end
So, to create a widget, simply extend
Dill::Widget (or one of its subclasses)
and define the widget's root element.
As long as you include
Dill::DSL (done automatically if you require
dill/cucumber), you can then get a reference to the widget using
my_widget = widget(:my_widget)
A test is typically comprised of 3 phases: Arrange (Given), Act (When) and Assert (Then). Widgets care about Act and Assert, so they let you
- do things, and
- verify things
much like Capybara, but in a more object-oriented manner. So, for example, where, in Capybara, you would test if a node contains some text like this:
expect(page).to have_selector('#my-widget', text: 'My Widget')
you can do the samething using Dill, like this:
expect(widget(:my_widget)).to eq 'My Widget'
This works even if the widget isn't visible yet because, when called,
Widget#== assumes that it's expected to return
true. If it can't return
true, then it will wait a certain amount of time until it can, or, finally, it
will give up and return
false. The same thing happens with all other operators
!~. So remember
this: some widget methods will block until they return true, or some
pre-defined time passes.
You can take advantage of the OO-nature of the widget API to customize how a
widget presents itself to the world. Just override the widget's
class MyWidget < Dill::Widget root '#my-widget' def value # unsurprisingly, #text returns the widget's text text + ", eh?" end end # use it: expect(widget(:my_widget)).to eq 'My Widget, eh?' expect(widget(:my_widget)).to include 'My'
See the documentation for Dill::Widget for information on how to use basic widgets.
The top level document