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)
3

There are 3 answers

0
Pippo On

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

0
streetlogics On

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
0
Sophie Déziel On

Remove the capital B of :Backspace

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