Matt Gibson
Matt Gibson

Reputation: 14949

How can i send :Backspace to an element with Capybara and Poltergeist without it triggering the browser to navigate back one page?

I am trying to test a HTML editor in a Cucumber scenario, and I need to simulate the user pressing backspace. The code I am using is below, but it causes the browser to navigate back instead of delete a character.

How can I focus on the editor area so that the keypress does not do this?

  page.execute_script "$('.redactor-editor').first().focus();"
  all('.redactor-editor')[0].native.send_key(:Backspace)

Upvotes: 3

Views: 2119

Answers (3)

streetlogics
streetlogics

Reputation: 4730

If you "fill_in" the field just prior, it should still have focus. In my case I needed to trigger a backspace to "clear a field's value", because just setting it to empty wasn't triggering things as expected.

EX:

When("I clear the field {string}") do |field_name|
  fill_in field_name, with: ' '
  find("input[name=#{field_name}]").native.send_key(:backspace)
end

Upvotes: 1

Sophie Déziel
Sophie Déziel

Reputation: 428

Remove the capital B of :Backspace

page.execute_script "$('.redactor-editor').first().focus();"
all('.redactor-editor')[0].native.send_key(:backspace)

Upvotes: 3

Pippo
Pippo

Reputation: 925

How about if you click in the text box that contains the char you want to delete prior to sending the backspace action

Upvotes: 0

Related Questions