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.CollectingAlertHandler; 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 57 public class HtmlSubmitInputTest extends WebTestCase { 58 63 public HtmlSubmitInputTest( final String name ) { 64 super( name ); 65 } 66 67 68 71 public void testSubmit() throws Exception { 72 final String htmlContent 73 = "<html><head><title>foo</title></head><body>" 74 + "<form id='form1'>" 75 + "<input type='submit' name='aButton' value='foo'/>" 76 + "<input type='suBMit' name='button' value='foo'/>" 77 + "<input type='submit' name='anotherButton' value='foo'/>" 78 + "</form></body></html>"; 79 final HtmlPage page = loadPage(htmlContent); 80 final MockWebConnection webConnection = getMockConnection(page); 81 82 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 83 84 final HtmlSubmitInput submitInput = (HtmlSubmitInput)form.getInputByName("button"); 85 final HtmlPage secondPage = (HtmlPage)submitInput.click(); 86 assertEquals("foo", secondPage.getTitleText()); 87 88 assertEquals( 89 Collections.singletonList(new KeyValuePair("button", "foo")), 90 webConnection.getLastParameters() ); 91 } 92 93 94 97 public void testClick_onClick() throws Exception { 98 final String htmlContent 99 = "<html><head><title>foo</title></head><body>" 100 + "<form id='form1' onSubmit='alert(\"bar\")'>" 101 + " <input type='submit' name='button' value='foo' onClick='alert(\"foo\")'/>" 102 + "</form></body></html>"; 103 final List collectedAlerts = new ArrayList(); 104 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 105 106 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 107 final HtmlSubmitInput submitInput = (HtmlSubmitInput)form.getInputByName("button"); 108 109 submitInput.click(); 110 111 final List expectedAlerts = Arrays.asList( new String[]{"foo","bar"} ); 112 assertEquals( expectedAlerts, collectedAlerts ); 113 } 114 115 116 119 public void testClick_onClick_JavascriptReturnsTrue() throws Exception { 120 121 final String firstContent 122 = "<html><head><title>First</title></head><body>" 123 + "<form name='form1' method='get' action='http://second'>" 124 + "<input name='button' type='submit' value='PushMe' id='button1'" 125 + "onclick='return true'/></form>" 126 + "</body></html>"; 127 final String secondContent = "<html><head><title>Second</title></head><body></body></html>"; 128 129 final WebClient client = new WebClient(); 130 final List collectedAlerts = new ArrayList(); 131 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 132 133 final MockWebConnection webConnection = new MockWebConnection( client ); 134 webConnection.setResponse(URL_FIRST, firstContent); 135 webConnection.setResponse(URL_SECOND, secondContent); 136 137 client.setWebConnection( webConnection ); 138 139 final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST); 140 final HtmlSubmitInput input = (HtmlSubmitInput)firstPage.getHtmlElementById("button1"); 141 final HtmlPage secondPage = (HtmlPage)input.click(); 142 assertEquals("Second", secondPage.getTitleText()); 143 } 144 } 145 | Popular Tags |