2

Each of my scenarios reads a sample in a file and copy them in a text field:

def sample(name)
  IO.read("spec/samples/#{name}.bib")
end

feature 'Import a record' do

  scenario 'from JabRef' do
    fill_in 'bibtex', :with => sample('jabref')
    in_dialog.click_button 'Import'
    ...
  end

end

This worked fine, until one of the sample had a tabulation in it: while a manual copy and paste worked, the test failed.

From other questions [1], I have understood that interpreting \t and \n as keyed inputs is supposed to be a "feature". Is there a way to deactivate this feature and just "paste" the content?

Community
  • 1
  • 1
Aurélien Bénel
  • 3,775
  • 24
  • 45
  • In the text field on the browser if you press your Tab key does it switch to another field? Inserting a literal tab is a known pain in text fields, does [this question](http://superuser.com/questions/67934/typing-the-tab-character-in-browser-text-boxes) help? – Nikolai B May 07 '14 at 07:35
  • @alex.frost My problem is that `fill_in` emulates typing rather than pasting. I would definitely prefer that users don't paste tabs in the field. However, some did and will probably do it again. I fixed the software to handle this, and now I would like to have a Capybara regression test for this. – Aurélien Bénel May 07 '14 at 08:54
  • 1
    Ok, its a long shot but maybe convert the "\t" to "\u0009" in ruby. That is unicode for the tab character. Also have you tried making it as simple as possible so: `fill_in 'bibtex', :with => "text with \u0009 tab in"` ? – Nikolai B May 07 '14 at 11:12
  • Thanks for your suggestion. I added `.gsub("\t", "\u0009")` after `IO.read`. My other samples are still OK, but the sample with the tab still fails. If I change it with `.gsub("\t", " ")`, the test passes but it's a totally different test. I'm afraid that `s.gsub("\t", "\u0009")` has the same exact value as `s`. – Aurélien Bénel May 07 '14 at 13:26
  • 1
    If you use Selenium as a capybara driver please try `find(locator).native.send_keys Selenium::Webdriver::Keys[:tab]` instead of `fill_in locator, with: "\t"` – Andrei Botalov May 15 '14 at 07:39
  • @AndreyBotalov Thanks for your suggestion but I use webkit driver... – Aurélien Bénel May 17 '14 at 14:33

1 Answers1

1

If all else fails, you could insert your text using Javascript:

page.execute_script '$("#bibtex").val("' << sample('jabref') << '")'

If you do this often enough, I'd extract this in a helper method (fill_in_plain or similar), maybe without using the help of jQuery (using plain old Javascript, i.e. document.getElementById et cetera).


Here's a proper helper, still using jQuery:

module CapybaraWebkitWorkarounds

  def fill_in_plain(selector, with: nil)
    value = with.gsub '"', "\\\"" # poor man's escaping
    page.execute_script %Q{ $("#{selector}").val("#{value}") }
  end

end

RSpec.configure do |config|
  # make it available in all feature specs
  config.include CapybaraWebkitWorkarounds, type: :feature
end

Then, in your feature spec, you'll do simply

feature 'Import a record' do

  scenario 'from JabRef' do
    fill_in_plain 'textarea[name="bibtex"]', with: sample('jabref')
    in_dialog.click_button 'Import'
    ...
  end

end

Please note, that the fill_in_plain helper now only understands jQuery selector (i.e. CSS selectors) strings as its first argument.

DMKE
  • 4,553
  • 1
  • 31
  • 50