Escaping Ruby Strings For Eval In Selenium Tests

If you are setting up a string in Ruby to then be used in an eval statement in a Selenium test you’ll need some escaping fu to be able to pull it off.

Quotes in strings that are then going to be put in other Ruby strings need to be tripple escaped. If the string is going directly to the eval in Selenium then only a single escape is needed.

first = "first quote: \\\"......"
second = "#{first} second quote: \\\".........."
jsfunction = "window.alert(\"#{second}\")"
puts jsfunction

it "should alert string" do
  page.js_eval(jsfunction)
end

selenuim ruby string escaping for eval

Published by

MikeGrace

I am a Geek! I hope you enjoyed this recent post.