1717
1818package org .openqa .selenium .htmlunit ;
1919
20+ import java .math .BigInteger ;
21+ import java .util .HashMap ;
22+ import java .util .Map ;
23+
2024import org .junit .Test ;
2125import org .junit .runner .RunWith ;
26+ import org .openqa .selenium .By ;
2227import org .openqa .selenium .JavascriptExecutor ;
2328import org .openqa .selenium .WebDriver ;
29+ import org .openqa .selenium .WebElement ;
2430import org .openqa .selenium .htmlunit .junit .BrowserRunner ;
31+ import org .openqa .selenium .htmlunit .junit .BrowserRunner .Alerts ;
32+ import org .openqa .selenium .htmlunit .junit .BrowserRunner .HtmlUnitNYI ;
2533
2634/**
2735 * General tests for the HtmlUnitDriver.
@@ -33,9 +41,151 @@ public class HtmlUnitDriver2Test extends WebDriverTestCase {
3341
3442 @ Test
3543 public void executeScriptWithoutPage () {
36- final WebDriver driver = getWebDriver ();
37- final String text = (String ) ((JavascriptExecutor ) driver ).executeScript ("return 'test';" );
44+ final WebDriver webDriver = getWebDriver ();
45+ final String text = (String ) ((JavascriptExecutor ) webDriver ).executeScript ("return 'test';" );
3846
3947 assertEquals ("test" , text );
4048 }
49+
50+ @ Test
51+ @ Alerts ({"1| [0]HtmlUnit string" , "1| [0] string" })
52+ public void executeScriptParamString () throws Exception {
53+ executeScriptParam (getExpectedAlerts ()[0 ], "HtmlUnit" );
54+ executeScriptParam (getExpectedAlerts ()[1 ], "" );
55+ }
56+
57+ @ Test
58+ @ Alerts ({"1| [0]true boolean" , "1| [0]false boolean" , "1| [0]true boolean" })
59+ public void executeScriptParamBoolean () throws Exception {
60+ executeScriptParam (getExpectedAlerts ()[0 ], true );
61+ executeScriptParam (getExpectedAlerts ()[1 ], false );
62+ executeScriptParam (getExpectedAlerts ()[2 ], Boolean .TRUE );
63+ }
64+
65+ @ Test
66+ @ Alerts ({"1| [0]4711 number" ,
67+ "1| [0]-1234567890155 number" ,
68+ "1| [0]0 number" ,
69+ "1| [0]3.141592653589793 number" ,
70+ "1| [0]4444 number" })
71+ @ HtmlUnitNYI (
72+ CHROME = {"1| [0]4711 number" ,
73+ "1| [0]-1234567890155 number" ,
74+ "1| [0]0 number" ,
75+ "1| [0]3.141592653589793 number" ,
76+ "1| [0]4444 bigint" },
77+ EDGE = {"1| [0]4711 number" ,
78+ "1| [0]-1234567890155 number" ,
79+ "1| [0]0 number" ,
80+ "1| [0]3.141592653589793 number" ,
81+ "1| [0]4444 bigint" },
82+ FF = {"1| [0]4711 number" ,
83+ "1| [0]-1234567890155 number" ,
84+ "1| [0]0 number" ,
85+ "1| [0]3.141592653589793 number" ,
86+ "1| [0]4444 bigint" },
87+ FF_ESR = {"1| [0]4711 number" ,
88+ "1| [0]-1234567890155 number" ,
89+ "1| [0]0 number" ,
90+ "1| [0]3.141592653589793 number" ,
91+ "1| [0]4444 bigint" })
92+ public void executeScriptParamNumber () throws Exception {
93+ executeScriptParam (getExpectedAlerts ()[0 ], 4711 );
94+ executeScriptParam (getExpectedAlerts ()[1 ], -1234567890155L );
95+ executeScriptParam (getExpectedAlerts ()[2 ], 0f );
96+ executeScriptParam (getExpectedAlerts ()[3 ], Math .PI );
97+ executeScriptParam (getExpectedAlerts ()[4 ], BigInteger .valueOf (4444 ));
98+ }
99+
100+ @ Test
101+ @ Alerts ({"2| [0]Html string [1]Unit string" , "0|" })
102+ public void executeScriptParamSimpleArray () throws Exception {
103+ executeScriptParam (getExpectedAlerts ()[0 ], new String [] {"Html" , "Unit" });
104+ executeScriptParam (getExpectedAlerts ()[1 ], new String [] {});
105+ }
106+
107+ @ Test
108+ @ Alerts ({"1| [0]1,-2 array" ,
109+ "1| [0]1,-2 array" ,
110+ "1| [0]1,-2 array" ,
111+ "1| [0]1,-2 array" ,
112+ "1| [0]false array" })
113+ public void executeScriptParamPrimitiveArray () throws Exception {
114+ executeScriptParam (getExpectedAlerts ()[0 ], new int [] {1 , -2 });
115+ executeScriptParam (getExpectedAlerts ()[1 ], new long [] {1L , -2L });
116+ executeScriptParam (getExpectedAlerts ()[2 ], new float [] {1f , -2f });
117+ executeScriptParam (getExpectedAlerts ()[3 ], new double [] {1d , -2d });
118+ executeScriptParam (getExpectedAlerts ()[4 ], new boolean [] {false });
119+ }
120+
121+ @ Test
122+ @ Alerts ({"3| [0]Html string [1]17 number [2]true boolean" ,
123+ "3| [0]Html string [1]42,7 array [2]true boolean" })
124+ public void executeScriptParamMixedArray () throws Exception {
125+ executeScriptParam (getExpectedAlerts ()[0 ], new Object [] {"Html" , 17 , true });
126+ executeScriptParam (getExpectedAlerts ()[1 ], new Object [] {"Html" , new int [] {42 , 7 }, true });
127+ }
128+
129+ @ Test
130+ @ Alerts ({"1| [0][object Object] object ()" ,
131+ "1| [0][object Object] object (Html->Unit )" ,
132+ "1| [0][object Object] object (Zahl->17 )" })
133+ public void executeScriptParamSimpleMap () throws Exception {
134+ Map <String , Object > param = new HashMap <>();
135+
136+ executeScriptParam (getExpectedAlerts ()[0 ], param );
137+
138+ param .put ("Html" , "Unit" );
139+ executeScriptParam (getExpectedAlerts ()[1 ], param );
140+
141+ param = new HashMap <>();
142+ param .put ("Zahl" , 17 );
143+ executeScriptParam (getExpectedAlerts ()[2 ], param );
144+ }
145+
146+ private void executeScriptParam (final Object expected , final Object ... params ) throws Exception {
147+ final String html = "<html><head><title>Tester</title></head></html>" ;
148+ final WebDriver webDriver = loadPage2 (html );
149+
150+ final String js = "let result = '';\n "
151+ + "result += arguments.length;\n "
152+ + "result += '|';\n "
153+ + "for (let i = 0; i < arguments.length; i++) {\n "
154+ + " result += ' [' + i + ']' + arguments[i];"
155+ + " if (Array.isArray(arguments[i])) {\n "
156+ + " result += ' array';\n "
157+ + " } else if (Object.getPrototypeOf(arguments[i]) === Map.prototype) {\n "
158+ + " result += ' map';\n "
159+ + " } else {\n "
160+ + " result += ' ' + typeof arguments[i];\n "
161+ + " if ('object' === typeof arguments[i]) {\n "
162+ + " result += ' (';\n "
163+ + " let props = Object.getOwnPropertyNames(arguments[i]);\n "
164+ + " for (let p = 0; p < props.length; p++) {\n "
165+ + " result += props[p];\n "
166+ + " result += '->';\n "
167+ + " result += arguments[i][props[p]] + ' ';\n "
168+ + " }\n "
169+ + " result += ')';\n "
170+ + " }\n "
171+ + " }\n "
172+ + "}\n "
173+ + "return result;\n " ;
174+
175+ final String text = (String ) ((JavascriptExecutor ) webDriver ).executeScript (js , params );
176+ assertEquals (expected , text );
177+ }
178+
179+ @ Test
180+ public void executeScriptParamWebElement () throws Exception {
181+ final String html = "<html><head><title>Tester</title></head>\n "
182+ + "<body><div id='myDivId'>diff</div></html>" ;
183+ final WebDriver webDriver = loadPage2 (html );
184+
185+ final WebElement webElement = webDriver .findElement (By .id ("myDivId" ));
186+
187+ final String js = "return arguments[0].outerHTML;" ;
188+ final String text = (String ) ((JavascriptExecutor ) webDriver ).executeScript (js , webElement );
189+ assertEquals ("<div id=\" myDivId\" >diff</div>" , text );
190+ }
41191}
0 commit comments