1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.ArrayList; 41 import java.util.Arrays; 42 import java.util.Collections; 43 import java.util.List; 44 45 import com.gargoylesoftware.htmlunit.BrowserVersion; 46 import com.gargoylesoftware.htmlunit.KeyValuePair; 47 import com.gargoylesoftware.htmlunit.MockWebConnection; 48 import com.gargoylesoftware.htmlunit.WebClient; 49 import com.gargoylesoftware.htmlunit.WebTestCase; 50 51 59 public class HtmlButtonTest extends WebTestCase { 60 65 public HtmlButtonTest( final String name ) { 66 super( name ); 67 } 68 69 70 73 public void testButtonClick_onClick() throws Exception { 74 final String htmlContent 75 = "<html><head><title>foo</title></head><body>" 76 + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>" 77 + " <button type='button' name='button' id='button' " 78 + "onClick='alert(\"foo\")'>Push me</button>" 79 + "</form></body></html>"; 80 final List collectedAlerts = new ArrayList(); 81 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 82 final HtmlButton button = ( HtmlButton )page.getHtmlElementById( "button" ); 83 84 final HtmlPage secondPage = (HtmlPage)button.click(); 85 86 final List expectedAlerts = Arrays.asList( new String[]{"foo"} ); 87 assertEquals( expectedAlerts, collectedAlerts ); 88 89 assertSame( page, secondPage ); 90 } 91 92 93 96 public void testSubmitClick_onClick() throws Exception { 97 final String htmlContent 98 = "<html><head><title>foo</title></head><body>" 99 + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>" 100 + " <button type='submit' name='button' id='button' " 101 + "onClick='alert(\"foo\")'>Push me</button>" 102 + "</form></body></html>"; 103 final List collectedAlerts = new ArrayList(); 104 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 105 final HtmlButton button = ( HtmlButton )page.getHtmlElementById( "button" ); 106 107 final HtmlPage secondPage = (HtmlPage)button.click(); 108 109 final List expectedAlerts = Arrays.asList( new String[]{"foo", "bar"} ); 110 assertEquals( expectedAlerts, collectedAlerts ); 111 112 assertNotSame( page, secondPage ); 113 } 114 115 116 119 public void testResetClick_onClick() throws Exception { 120 final String htmlContent 121 = "<html><head><title>foo</title></head><body>" 122 + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>" 123 + " <button type='reset' name='button' id='button' " 124 + "onClick='alert(\"foo\")'>Push me</button>" 125 + "</form></body></html>"; 126 final List collectedAlerts = new ArrayList(); 127 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 128 final HtmlButton button = ( HtmlButton )page.getHtmlElementById( "button" ); 129 130 final HtmlPage secondPage = (HtmlPage)button.click(); 131 132 final List expectedAlerts = Arrays.asList( new String[]{"foo", "reset"} ); 133 assertEquals( expectedAlerts, collectedAlerts ); 134 135 assertSame( page, secondPage ); 136 } 137 138 139 142 public void testReset() throws Exception { 143 final String htmlContent 144 = "<html><head><title>foo</title></head><body>" 145 + "<form id='form1'>" 146 + "<input type='text' name='textfield1' id='textfield1' value='foo'/>" 147 + "<input type='password' name='password1' id='password1' value='foo'/>" 148 + "<input type='hidden' name='hidden1' id='hidden1' value='foo'/>" 149 + "<input type='radio' name='radioButton' value='foo' checked/>" 150 + "<input type='radio' name='radioButton' value='bar'/>" 151 + "<input type='checkbox' name='checkBox' value='check'/>" 152 + "<select id='select1'>" 153 + " <option id='option1' selected value='1'>Option1</option>" 154 + " <option id='option2' value='2'>Option2</option>" 155 + "</select>" 156 + "<textarea id='textarea1'>Foobar</textarea>" 157 + "<isindex prompt='Enter some text' id='isindex1'>" 158 + "<button type='reset' id='resetButton' value='pushme'/>" 159 + "</form></body></html>"; 160 final HtmlPage page = loadPage(htmlContent); 161 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 162 final HtmlButton resetInput = (HtmlButton)page.getHtmlElementById( "resetButton" ); 163 164 form.setCheckedRadioButton("radioButton", "bar"); 166 ((HtmlCheckBoxInput)form.getInputByName("checkBox")).setChecked(true); 167 ((HtmlOption)page.getHtmlElementById("option1")).setSelected(false); 168 ((HtmlOption)page.getHtmlElementById("option2")).setSelected(true); 169 ((HtmlTextArea)page.getHtmlElementById("textarea1")).setText("Flintstone"); 170 ((HtmlTextInput)page.getHtmlElementById("textfield1")).setValueAttribute("Flintstone"); 171 ((HtmlHiddenInput)page.getHtmlElementById("hidden1")).setValueAttribute("Flintstone"); 172 ((HtmlPasswordInput)page.getHtmlElementById("password1")).setValueAttribute("Flintstone"); 173 ((HtmlIsIndex)page.getHtmlElementById("isindex1")).setValue("Flintstone"); 174 175 assertEquals( "bar", form.getCheckedRadioButton("radioButton").getValueAttribute()); 177 assertTrue( ((HtmlCheckBoxInput)form.getInputByName("checkBox")).isChecked() ); 178 assertFalse(((HtmlOption)page.getHtmlElementById("option1")).isSelected()); 179 assertTrue(((HtmlOption)page.getHtmlElementById("option2")).isSelected()); 180 assertEquals( "Flintstone", ((HtmlTextArea)page.getHtmlElementById("textarea1")).getText()); 181 assertEquals( "Flintstone", ((HtmlTextInput)page.getHtmlElementById("textfield1")).getValueAttribute()); 182 assertEquals( "Flintstone", ((HtmlHiddenInput)page.getHtmlElementById("hidden1")).getValueAttribute()); 183 assertEquals( "Flintstone", ((HtmlIsIndex)page.getHtmlElementById("isindex1")).getValue()); 184 185 final HtmlPage secondPage = (HtmlPage)resetInput.click(); 186 assertSame( page, secondPage ); 187 188 assertEquals( "foo", form.getCheckedRadioButton("radioButton").getValueAttribute()); 190 assertFalse( ((HtmlCheckBoxInput)form.getInputByName("checkBox")).isChecked() ); 191 assertTrue(((HtmlOption)page.getHtmlElementById("option1")).isSelected()); 192 assertFalse(((HtmlOption)page.getHtmlElementById("option2")).isSelected()); 193 assertEquals( "Foobar", ((HtmlTextArea)page.getHtmlElementById("textarea1")).getText()); 194 assertEquals( "foo", ((HtmlTextInput)page.getHtmlElementById("textfield1")).getValueAttribute()); 195 assertEquals( "foo", ((HtmlHiddenInput)page.getHtmlElementById("hidden1")).getValueAttribute()); 196 assertEquals( "foo", ((HtmlPasswordInput)page.getHtmlElementById("password1")).getValueAttribute()); 197 assertEquals( "", ((HtmlIsIndex)page.getHtmlElementById("isindex1")).getValue()); 198 } 199 202 public void testButtonTypeSubmit() throws Exception { 203 final String htmlContent 204 = "<html><head><title>foo</title></head><body>" 205 + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>" 206 + " <button type='submit' name='button' id='button' value='foo'" 207 + " >Push me</button>" 208 + "</form></body></html>"; 209 final List collectedAlerts = new ArrayList(); 210 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 211 final HtmlButton button = ( HtmlButton )page.getHtmlElementById( "button" ); 212 213 final HtmlPage secondPage = (HtmlPage)button.click(); 214 215 final List expectedAlerts = Arrays.asList( new String[]{"bar"} ); 216 assertEquals( expectedAlerts, collectedAlerts ); 217 218 assertNotSame( page, secondPage ); 219 final List expectedParameters = Arrays.asList( new Object[]{ 220 new KeyValuePair("button", "foo") 221 } ); 222 final List collectedParameters = getMockConnection(secondPage).getLastParameters(); 223 224 assertEquals( expectedParameters, collectedParameters ); 225 } 226 227 228 232 public void testDefaultButtonType_StandardsCompliantBrowser() throws Exception { 233 doTestDefaultButtonType(BrowserVersion.MOZILLA_1_0, "submit"); 234 } 235 236 240 public void testDefaultButtonType_InternetExplorer() throws Exception { 241 doTestDefaultButtonType(BrowserVersion.INTERNET_EXPLORER_6_0, "button"); 242 } 243 244 private void doTestDefaultButtonType(final BrowserVersion browserVersion, 245 final String expectedType) throws Exception { 246 final String firstContent 247 = "<html><head><title>First</title></head><body>" 248 + "<form id='form1' action='http://second'>" 249 + " <button name='button' id='button' value='pushme'>PushMe</button>" 250 + "</form></body></html>"; 251 final String secondContent 252 = "<html><head><title>Second</title></head><body'></body></html>"; 253 final WebClient client = new WebClient(browserVersion); 254 255 final MockWebConnection webConnection = new MockWebConnection( client ); 256 webConnection.setResponse( 257 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 258 webConnection.setResponse( 259 URL_SECOND,secondContent,200,"OK","text/html",Collections.EMPTY_LIST ); 260 client.setWebConnection( webConnection ); 261 262 final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST); 263 final HtmlButton button = ( HtmlButton )page.getHtmlElementById( "button" ); 264 assertEquals( expectedType, button.getTypeAttribute() ); 265 266 final HtmlPage page2 = (HtmlPage)button.click(); 267 final List expectedParameters; 268 final String expectedSecondPageTitle; 269 if( expectedType.equals("submit") ) { 270 expectedParameters = Collections.singletonList( new KeyValuePair("button", "pushme")); 271 expectedSecondPageTitle = "Second"; 272 } 273 else { 274 expectedParameters = Collections.EMPTY_LIST; 275 expectedSecondPageTitle = "First"; 276 } 277 assertEquals( expectedParameters, webConnection.getLastParameters() ); 278 assertEquals( expectedSecondPageTitle, page2.getTitleText() ); 279 } 280 } 281 | Popular Tags |