Permalink
Browse files

Fix UTF-8 issue in watir-1.6

This issue might have been fixed in the future version of watir. However, I believe this diff makes sense because:
a) it allows us to remove our custom hack
b) it helps the few people on 1.6
c) it helps make sure this doesn't break in future versions

Note: i'm not totally sure how other watir versions deal with unittests. On one hand they should all live in one place. On the other hand, if
      things like syntax are not compatible, somebody will have to manage the multiple copies :(
  • Loading branch information...
1 parent ad53f2f commit 5c00c9cbe6904fda4fab87e77cf0276a06da0dff Alok Menghrajani committed Apr 7, 2011
View
216 commonwatir/unittests/html/base64.js
@@ -0,0 +1,216 @@
+/*
+Copyright (c) 2008 Fred Palmer fred.palmer_at_gmail.com
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+function StringBuffer()
+{
+ this.buffer = [];
+}
+
+StringBuffer.prototype.append = function append(string)
+{
+ this.buffer.push(string);
+ return this;
+};
+
+StringBuffer.prototype.toString = function toString()
+{
+ return this.buffer.join("");
+};
+
+var Base64 =
+{
+ codex : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
+
+ encode : function (input)
+ {
+ var output = new StringBuffer();
+
+ var enumerator = new Utf8EncodeEnumerator(input);
+ while (enumerator.moveNext())
+ {
+ var chr1 = enumerator.current;
+
+ enumerator.moveNext();
+ var chr2 = enumerator.current;
+
+ enumerator.moveNext();
+ var chr3 = enumerator.current;
+
+ var enc1 = chr1 >> 2;
+ var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
+ var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
+ var enc4 = chr3 & 63;
+
+ if (isNaN(chr2))
+ {
+ enc3 = enc4 = 64;
+ }
+ else if (isNaN(chr3))
+ {
+ enc4 = 64;
+ }
+
+ output.append(this.codex.charAt(enc1) + this.codex.charAt(enc2) + this.codex.charAt(enc3) + this.codex.charAt(enc4));
+ }
+
+ return output.toString();
+ },
+
+ decode : function (input)
+ {
+ var output = new StringBuffer();
+
+ var enumerator = new Base64DecodeEnumerator(input);
+ while (enumerator.moveNext())
+ {
+ var charCode = enumerator.current;
+
+ if (charCode < 128)
+ output.append(String.fromCharCode(charCode));
+ else if ((charCode > 191) && (charCode < 224))
+ {
+ enumerator.moveNext();
+ var charCode2 = enumerator.current;
+
+ output.append(String.fromCharCode(((charCode & 31) << 6) | (charCode2 & 63)));
+ }
+ else
+ {
+ enumerator.moveNext();
+ var charCode2 = enumerator.current;
+
+ enumerator.moveNext();
+ var charCode3 = enumerator.current;
+
+ output.append(String.fromCharCode(((charCode & 15) << 12) | ((charCode2 & 63) << 6) | (charCode3 & 63)));
+ }
+ }
+
+ return output.toString();
+ }
+}
+
+
+function Utf8EncodeEnumerator(input)
+{
+ this._input = input;
+ this._index = -1;
+ this._buffer = [];
+}
+
+Utf8EncodeEnumerator.prototype =
+{
+ current: Number.NaN,
+
+ moveNext: function()
+ {
+ if (this._buffer.length > 0)
+ {
+ this.current = this._buffer.shift();
+ return true;
+ }
+ else if (this._index >= (this._input.length - 1))
+ {
+ this.current = Number.NaN;
+ return false;
+ }
+ else
+ {
+ var charCode = this._input.charCodeAt(++this._index);
+
+ // "\r\n" -> "\n"
+ //
+ if ((charCode == 13) && (this._input.charCodeAt(this._index + 1) == 10))
+ {
+ charCode = 10;
+ this._index += 2;
+ }
+
+ if (charCode < 128)
+ {
+ this.current = charCode;
+ }
+ else if ((charCode > 127) && (charCode < 2048))
+ {
+ this.current = (charCode >> 6) | 192;
+ this._buffer.push((charCode & 63) | 128);
+ }
+ else
+ {
+ this.current = (charCode >> 12) | 224;
+ this._buffer.push(((charCode >> 6) & 63) | 128);
+ this._buffer.push((charCode & 63) | 128);
+ }
+
+ return true;
+ }
+ }
+}
+
+function Base64DecodeEnumerator(input)
+{
+ this._input = input;
+ this._index = -1;
+ this._buffer = [];
+}
+
+Base64DecodeEnumerator.prototype =
+{
+ current: 64,
+
+ moveNext: function()
+ {
+ if (this._buffer.length > 0)
+ {
+ this.current = this._buffer.shift();
+ return true;
+ }
+ else if (this._index >= (this._input.length - 1))
+ {
+ this.current = 64;
+ return false;
+ }
+ else
+ {
+ var enc1 = Base64.codex.indexOf(this._input.charAt(++this._index));
+ var enc2 = Base64.codex.indexOf(this._input.charAt(++this._index));
+ var enc3 = Base64.codex.indexOf(this._input.charAt(++this._index));
+ var enc4 = Base64.codex.indexOf(this._input.charAt(++this._index));
+
+ var chr1 = (enc1 << 2) | (enc2 >> 4);
+ var chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
+ var chr3 = ((enc3 & 3) << 6) | enc4;
+
+ this.current = chr1;
+
+ if (enc3 != 64)
+ this._buffer.push(chr2);
+
+ if (enc4 != 64)
+ this._buffer.push(chr3);
+
+ return true;
+ }
+ }
+};
View
11 commonwatir/unittests/html/utf8.html
@@ -7,6 +7,15 @@
</head>
<body>
+ <script src="./base64.js"></script>
+ <script>
+ function base64_encode() {
+ document.getElementById("output").value = Base64.encode(document.getElementById("input").value);
+ }
+ </script>
<div id="utf8_string">æøå</div>
+ <input id="input" type="text">
+ <input id="output" type="text">
+ <input type="submit" id="button" onClick="base64_encode(); return false"/>
</body>
-</html>
+</html>
View
18 commonwatir/unittests/utf8_test.rb
@@ -2,13 +2,14 @@
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..') unless $SETUP_LOADED
require 'unittests/setup'
+require 'base64'
class TC_Utf8 < Test::Unit::TestCase
location __FILE__
def setup
uses_page "utf8.html"
- end
+ end
def test_is_correct_encoding
txt = browser.div(:id, 'utf8_string').text
@@ -19,6 +20,17 @@ def test_is_correct_encoding
end
end
-end
-
+ def test_utf8_text_field_set
+ utf8_str = 'w6ljb2xlIMOgIGxvbA=='
+ browser.text_field(:id, 'input').set Base64.decode64(utf8_str)
+ browser.button(:id, 'button').click
+ assert_equal(utf8_str, browser.text_field(:id, 'output').value)
+ end
+ def test_utf8_text_field_value
+ utf8_str = 'w6ljb2xlIMOgIGxvbA=='
+ browser.text_field(:id, 'input').value = Base64.decode64(utf8_str)
+ browser.button(:id, 'button').click
+ assert_equal(utf8_str, browser.text_field(:id, 'output').value)
+ end
+end
View
6 firewatir/lib/firewatir/element.rb
@@ -1316,6 +1316,12 @@ def method_missing(methId, *args)
args[0].gsub!("\\", "\\"*4)
args[0].gsub!("\"", "\\\"")
args[0].gsub!("\n","\\n")
+
+ if (args[0].class == String)
+ args[0] = args[0].to_json()
+ args[0] = args[0][1, args[0].length - 2]
+ end
+
jssh_command = "#{element_object}.#{methodName}\"#{args[0]}\""
else
jssh_command = "#{element_object}.#{methodName}#{args[0]}"
View
6 firewatir/lib/firewatir/elements/text_field.rb
@@ -186,6 +186,12 @@ def set( setThis )
# - value - The string to enter into the text field
#
def doKeyPress( value )
+ if RUBY_VERSION =~ /^1\.8/
+ # before iterating over each character, we'll convert value to utf-8
+ require "encoding/character/utf-8"
+ value = u"" + value
+ end
+
begin
max = maxlength
if (max > 0 && value.length > max)

0 comments on commit 5c00c9c

Please sign in to comment.