This had me stumped for a little while, every time I went to test a website that used a language like German or Greek and a foreign character cropped up then my automated tests generally broke with the following error:
invalid multibyte char (US-ASCII) (SyntaxError)
It turns out that all you need to do is add one simple line to the beginning of any file that contains code that tests foreign characters and you’re good to go! The line in question is:
# encoding: utf-8
This forces the use of the UTF-8 format. Although it seems slightly counter-intuitive as a hash symbol normally comments out a line in Ruby but anyway, here’s an example of it in action. This piece of code will open a browser and navigate to a test page with a piece of German text on it. Try it out. You can also try deleting the first line to see it fail.
# encoding: utf-8
require "watir-webdriver"
b = Watir::Browser.new :chrome
b.goto 'http://www.lazyautomation.co.uk/lazy3.html'
if b.text.include? "Ich mit Ruby und Watir sie sind sehr gut gefällt! Web Automation Felsen!"
puts 'PASS - Found German Text'
else
puts 'FAIL - Did not find German text'
end
Leave a Reply