How to wait url changing in feature test using capybara

May 08, 2015

Reading time ~1 minute

Outline

For example, You make SAP(single page application) using angularjs then the application change path from /login to /profile. If application change path after ajax request how to wait path changing.

Solution

You need include this helper.

module WaitHelper

  def wait_url(url)
    start = Time.now.tv_sec
    until is_not_over_limit(url, Capybara.default_wait_time, start); end
  end

  private
    def is_not_over_limit(url, limit_sec, start)
      matched = current_path === url
      if (Time.now.tv_sec - start) >= limit_sec then
        raise Exception.new("WaitHelperError: wait timeout")
      end
      matched
    end

end

then You can use like this.

describe "form submit" do

  context "input values are valid" do
    it "send data to server using ajax then path is changed" do
      visit "/login"      

      fill_in("email", with: "test@example.com")
      fill_in("password", with: "aaaaaa")
      click_button("login") # send ajax request    

      wait_url("/profile")
      expect(page).to have_title("profile page")
    end
  end

end

add ticket number to git commit automatically

Most of ticket tracker like Github, pivotal tracker have function to connect your commit to ticket(story).But every time when you commit ...… Continue reading

I will build frontend and backend separately

Published on September 28, 2016

order of angular controller's initialisation

Published on October 05, 2015