Adding UTF-8 characters to text fields

This document shows how to add UTF-8 characters to text fields using watir.

The key to making this work in old versions of Watir, is setting the WINOLE.codepage to UTF-8

require "win32ole"
WIN32OLE.codepage = WIN32OLE::CP_UTF8 

This is the defeault since version 1.6.5. If you want to revert to the old behaviour, use

WIN32OLE.codepage = WIN32OLE::CP_ACP

If you are going to try to use the sample below make sure that you encode the .rb file that you are using in UTF-8.  We are using eclipse and it is just a setting in one of the options,  the same goes for text editors like PSPad and notepad change the file to UTF-8 and save it before working with the UTF-8 characters.  The font of the file can also prevent you from viewing the correct characters.  I usually use Arial Unicode to be on the safe side.

require 'win32ole'
require 'watir'

WIN32OLE.codepage = WIN32OLE::CP_UTF8

ENV["watir_browser"]="ie"

browser = Watir::Browser.new
browser.goto "www.google.com"

browser.text_field(:name,'q').value = "考"
browser.text_field(:name,'q').value = "\xE8\x80\x83"

You can see in the code that data is entered into the 'q' field twice.  That is because the second time is the UTF-8 hex version of the same character.

One more thing to note is that we use excel data to drive our test cases.  You have to have the code page set to UTF-8 in order to read these characters in UTF-8

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.