最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Turbolinks 5CapybaraRails - Stack Overflow

programmeradmin2浏览0评论

I am working on upgrading my Rails Application to use Turbolinks 5 to take advantage of the Native Wrappers. I have all of my javascript updated and everything is working as expected in the browser and the Android App. I am having trouble with some of my capybara test when using js true. I am using the latest capybara (2.11.0), poltergeist (1.12.0) and phantomjs (2.1) at the time of writing this post. I have tried the capybara-webkit driver as well with no success.

Where I appear to be having problems is that in turbolinks 5 all forms should by submitted remotely and turbolinks knows how to handle the request. I find that with Capbara my form does not redirect after submission, it stays on the same page. I have done alot of Googling and trial and error and have some up short. Any help would be greatly appreciated. If more information is needed let me know.

  scenario 'user has dispatch permission and valid attributes', js: true do
    sign_in('jdoe')
    page.visit path
    page.fill_in 'contact_text_input', with: 'Jimmy'
    page.select 'Medium - 3 Hours', from: 'Priority'
    page.fill_in 'Created By', with: 'John Doe'
    page.click_button 'Create This'
    save_and_open_page
    expect(page.current_path).to eq("/dispatch")
    expect(page).to have_content('This was created successfully')
  end

  def sign_in(username)
    page.visit '/'
    page.fill_in 'Username', with: username
    page.fill_in 'Password', with: 'test'
    page.click_button 'Log In'
  end

I am working on upgrading my Rails Application to use Turbolinks 5 to take advantage of the Native Wrappers. I have all of my javascript updated and everything is working as expected in the browser and the Android App. I am having trouble with some of my capybara test when using js true. I am using the latest capybara (2.11.0), poltergeist (1.12.0) and phantomjs (2.1) at the time of writing this post. I have tried the capybara-webkit driver as well with no success.

Where I appear to be having problems is that in turbolinks 5 all forms should by submitted remotely and turbolinks knows how to handle the request. I find that with Capbara my form does not redirect after submission, it stays on the same page. I have done alot of Googling and trial and error and have some up short. Any help would be greatly appreciated. If more information is needed let me know.

  scenario 'user has dispatch permission and valid attributes', js: true do
    sign_in('jdoe')
    page.visit path
    page.fill_in 'contact_text_input', with: 'Jimmy'
    page.select 'Medium - 3 Hours', from: 'Priority'
    page.fill_in 'Created By', with: 'John Doe'
    page.click_button 'Create This'
    save_and_open_page
    expect(page.current_path).to eq("/dispatch")
    expect(page).to have_content('This was created successfully')
  end

  def sign_in(username)
    page.visit '/'
    page.fill_in 'Username', with: username
    page.fill_in 'Password', with: 'test'
    page.click_button 'Log In'
  end
Share Improve this question asked Jan 16, 2017 at 19:25 Ryan CondronRyan Condron 4291 gold badge4 silver badges14 bronze badges 6
  • Are you running Poltergeist with js_errors: true to make sure you don't have any JS errors, or use of unsupported JS functions/features (Poltergeist 2.1.1 does not support ES2015 - the new 2.5 beta should though). Also note that use of let in your JS will silently fail in Poltergeist 2.1.1 – Thomas Walpole Commented Jan 16, 2017 at 20:29
  • Also - I would expect your test to fail due to sign_in not waiting for the sign in to plete. This means the page.visit path in your test may be called before the auth cookies have been set and therefore not load up the page you think it is. You should have something that makes sure sign in has pleted like expect(page).to have_text('You have logged in!') or whatever shows on the page to indicate successful log in. – Thomas Walpole Commented Jan 16, 2017 at 20:36
  • Than You for all the pointers, the sign in problem is one that I am having, so I will be sure to try that. I am trying to install the beta of phantomjs, but the sign in issue may be what I am experiencing – Ryan Condron Commented Jan 16, 2017 at 21:05
  • Sorry -- I meant PhantomJS 2.1.1 - not poltergeist - and the PhantomJS 2.5 beta. It is very much a beta right now though and has a few issues of its own – Thomas Walpole Commented Jan 16, 2017 at 21:08
  • I went back to 2.1.1 and the suggestion with my sign_in function solved most of my test failures. I have a few left mainly due to still implementing turbolinks. Thank you for all the help. – Ryan Condron Commented Jan 16, 2017 at 21:33
 |  Show 1 more ment

3 Answers 3

Reset to default 4

You can track Turbolinks AJAX request by the progress bar like:

def wait_for_turbolinks
  has_css?('.turbolinks-progress-bar', visible: true)
  has_no_css?('.turbolinks-progress-bar')
end

Nice solution ErgoLau. I did some minor changes to fix max time waiting.

def wait_for_turbolinks timeout = nil
  if has_css?('.turbolinks-progress-bar', visible: true, wait: (0.25).seconds)
    has_no_css?('.turbolinks-progress-bar', wait: timeout.presence || 5.seconds)
  end
end

I had this issue. My fix was (like Thomas Walpole suggests in a ment) to add an expect(page).to have_text("Some text from the page you should be redirected to after login") after submitting the login form.

In our case, we have a log_in_buyer_using_the_form test helper, so I modified it to take an argument: log_in_buyer_using_the_form(wait_for_content: item.title)

My assumption here is that we need to 1. wait for the form's Ajax submission to happen and 2. then wait for Turbolinks to redirect according to the Ajax response. By having the test wait for some content to appear, we know it is past that stage.

Something more elegant that I haven't tried, but that should work in theory, would be to set up an event listener using page.execute_script that loops until some event like turbolinks:load has run.

发布评论

评论列表(0)

  1. 暂无评论