Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to wait url changing in feature test using capybara #783

Open
tomoyukikashiro opened this issue Aug 1, 2022 · 0 comments
Open

How to wait url changing in feature test using capybara #783

tomoyukikashiro opened this issue Aug 1, 2022 · 0 comments

Comments

@tomoyukikashiro
Copy link
Owner


date: 2015-05-08
title: How to wait url changing in feature test using capybara
slug: wait-url-changed-in-capybara
lang: en-US
tags: [test,capybara,rails]

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant