Fail fast and move on

1 min


621
621 points
fail fast
fail fast

Fail fast and move on

I like Capybara because of its idea to test  web applications by simulating how a real user would interact with the app, and capybara syntax is pretty straight forward to understand but when you have a big project using the default customisation that capybara provides will not allow you to ‘fail fast and move on’.

You might say from where this whole idea of fail fast , it’s pretty simple  to me , I don’t want to wait in order to fail , I just want to fail and move on.

Given the following code :

 it "signs me in" do
    visit '/sessions/new'
    within("#session") do
      fill_in 'Email', with: 'user@example.com'
      fill_in 'Password', with: 'password'
    end
    click_button 'Sign in'
    expect(page).to have_content 'Success'
  end
end

When you reach the assertion and having the default timeout of 30 seconds , Capybara will wait 30 seconds to fail in case a actual results are not the same with expected results.

Honestly to me it doesn’t make sense to wait that much for something that is not on the page, so  I was thinking that we can have a helper method :

Create a capybara helper
   def is_content_present?(content)
      Capybara.default_max_wait_time = 0
        if page.has_content?(content)
          begin
            return true
          ensure
            Capybara.default_max_wait_time = 30
          end
        else
          return false
        end
    end
end

This gives use some flexibility because is returning a boolean value and using test unit assertions we can have something like this :

Assert using test unit assertions
require 'test/unit/assertions'
include Test::Unit::Assertions

# content should be present
assert_true(is_content_present?(content)
# content should not be present
assert_false(is_content_present?(content)

You can create helpers like the above one for presence of elements , for a locator having text and so on .

You might say but why I should add that much extra work just for this ?

Well it’s simple, Imagine you have couple or thousand of tests  that are running in the continuos integration , when you developers are pushing a dodgy branch and you have hundreds of failures each failure will take the time from Capybara default timeout so that means hundreds of minutes lost. This is completely against any CI principle , when you need to deliver your feedback as fast as possible.

A while ago I wrote an article about selenium performance tips  , you can consider trying any of those in your project to see if is working out.

Happy testing!

 


Like it? Share with your friends!

621
621 points

What's Your Reaction?

cute cute
0
cute
scary scary
0
scary
wtf wtf
0
wtf
geek geek
0
geek
WIN WIN
0
WIN
Love Love
0
Love
OMG OMG
0
OMG
Angry Angry
0
Angry
Lol Lol
0
Lol
Test engineer

0 Comments

Your email address will not be published. Required fields are marked *

Choose A Format
Trivia quiz
Story