2

I have JavaScript in my application that submits a form when the user hits Ctrl-S or Cmd-S. I want to write an automated test for this using RSpec, Capybara, and Capybara Webkit. I don't think I can just have Capybara execute JavaScript to trigger Ctrl-S/Cmd-S because that's not normally allowed with JavaScript in Chrome as a security concern. I see with Selenium there are page.driver.browser.action.key_down/key_up methods available. Is there anything similar with Capybara Webkit? If not, how can I send Ctrl-S and Cmd-S to the browser in my test?

Edit: I also can't get this to work using the regular Selenium driver with Firefox:

describe 'edit a template and hit Ctrl-S', js: true do
  render_views

  it 'saves the template' do
    visit my_path
    page.execute_script("$('#hidden_textarea').val('Fabulous new content')")
    builder = page.driver.browser.action
    builder.key_down(:control).send_keys('s').key_up(:control).perform
    expect(page).to have_text('Record was saved.')
    expect(page).to have_text('Fabulous new content')
  end
end

It looks like the builder.key_down(:control).send_keys('s').key_up(:control).perform isn't doing anything--the page loads in Firefox but just sits there. This is with Firefox 19 on OS X with selenium-webdriver 2.35.1.

Any suggestions on how to get this to work in either Firefox or Chrome, with either Selenium or Capybara Webkit?

Community
  • 1
  • 1
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
  • By the way, the JavaScript I'm trying to test does work. When Selenium opens Firefox, if I wait a second and hit Ctrl-S myself, the form submits as expected and the test passes. – Sarah Vessels Oct 10 '13 at 16:10

1 Answers1

0

I'm trying to do a similar thing in Chrome using Capybara and SitePrism. This actually works for me in Firefox though:

page.element.native.send_keys :command, 'a'

so I suggest trying this

builder.native.send_keys :control, 's'
Adam Fraser
  • 6,255
  • 10
  • 42
  • 54