/*
* Copyright (c) 2002-2008 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gargoylesoftware.htmlunit.html;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.MockWebConnection;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebTestCase;
import com.gargoylesoftware.htmlunit.WebWindow;
/**
* Tests for {@link HtmlForm}.
*
* @version $Revision$
* @author Mike Bowler
* @author Jun Chen
* @author George Murnock
* @author Marc Guillemot
* @author Ahmed Ashour
* @author Philip Graf
*/
public class HtmlFormTest extends WebTestCase {
/**
* Tests the good case for setCheckedRatdioButton().
* @exception Exception If the test fails
*/
@Test
public void testSetSelectedRadioButton_ValueExists() throws Exception {
final String htmlContent
= "
foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final HtmlSubmitInput pushButton = (HtmlSubmitInput) form.getInputByName("button");
((HtmlRadioButtonInput) form.getFirstByXPath(
"//input[@type='radio' and @name='foo' and @value='2']")).setChecked(true);
assertFalse(((HtmlRadioButtonInput) page.getHtmlElementById("input1")).isChecked());
assertTrue(((HtmlRadioButtonInput) page.getHtmlElementById("input2")).isChecked());
// Test that only one value for the radio button is being passed back to the server
final HtmlPage secondPage = (HtmlPage) pushButton.click();
assertEquals("url", URL_GARGOYLE.toExternalForm() + "?foo=2&button=foo",
secondPage.getWebResponse().getUrl());
Assert.assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
}
/**
* Tests setCheckedRadioButton() with a value that doesn't exist.
* @exception Exception If the test fails
*/
@Test
public void testSetSelectedRadioButton_ValueDoesNotExist_DoNotForceSelection() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final HtmlInput pushButton = form.getInputByName("button");
assertNotNull(pushButton);
assertNull(form.getFirstByXPath("//input[@type='radio' and @name='foo' and @value='4']"));
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_String() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
// Regression test: this used to blow up
form.submit((HtmlSubmitInput) page.getHtmlElementById("submitButton"));
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_ExtraParameters() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button");
button.click();
final List expectedParameters = Arrays.asList(new NameValuePair[]{
new NameValuePair("textfield", "*"), new NameValuePair("button", "foo")
});
final List collectedParameters = webConnection.getLastParameters();
assertEquals(expectedParameters, collectedParameters);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_BadSubmitMethod() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
((ClickableElement) page.getHtmlElementById("button")).click();
assertSame(HttpMethod.GET, webConnection.getLastMethod());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_onSubmitHandler() throws Exception {
final String firstContent
= "First\n"
+ "\n"
+ "";
final String secondContent = "Second";
final WebClient client = new WebClient();
final List collectedAlerts = new ArrayList();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection(client);
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setDefaultResponse(secondContent);
client.setWebConnection(webConnection);
final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
final HtmlSubmitInput button = (HtmlSubmitInput) firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = (HtmlPage) button.click();
assertEquals("Second", secondPage.getTitleText());
assertEquals(new String[] {"clicked"}, collectedAlerts);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_onSubmitHandler_returnFalse() throws Exception {
final String firstContent
= "First\n"
+ "\n"
+ "";
final String secondContent = "Second";
final WebClient client = new WebClient();
final List collectedAlerts = new ArrayList();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection(client);
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);
client.setWebConnection(webConnection);
final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
final HtmlSubmitInput button = (HtmlSubmitInput) firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = (HtmlPage) button.click();
assertEquals(firstPage.getTitleText(), secondPage.getTitleText());
assertEquals(new String[] {"clicked"}, collectedAlerts);
}
/**
* Regression test for bug 1628521 (NullPointerException when submitting forms).
* @throws Exception if the test fails
*/
@Test
public void testSubmit_onSubmitHandler_fails() throws Exception {
final String firstContent
= "First\n"
+ "\n"
+ "";
final String secondContent = "Second";
final WebClient client = new WebClient();
final MockWebConnection webConnection = new MockWebConnection(client);
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setDefaultResponse(secondContent);
client.setWebConnection(webConnection);
final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
final HtmlSubmitInput button = (HtmlSubmitInput) firstPage.getHtmlElementById("button");
final HtmlPage secondPage = (HtmlPage) button.click();
assertEquals("Second", secondPage.getTitleText());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_onSubmitHandler_javascriptDisabled() throws Exception {
final String firstContent
= "First\n"
+ "\n"
+ "";
final String secondContent = "Second";
final WebClient client = new WebClient();
client.setJavaScriptEnabled(false);
final List collectedAlerts = new ArrayList();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection(client);
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setDefaultResponse(secondContent);
client.setWebConnection(webConnection);
final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
final HtmlSubmitInput button = (HtmlSubmitInput) firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = (HtmlPage) button.click();
assertEquals("First", firstPage.getTitleText());
assertEquals("Second", secondPage.getTitleText());
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_javascriptAction() throws Exception {
final String firstContent
= "First\n"
+ "\n"
+ "";
final String secondContent = "Second";
final WebClient client = new WebClient();
final List collectedAlerts = new ArrayList();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection(client);
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);
client.setWebConnection(webConnection);
final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
final HtmlSubmitInput button = (HtmlSubmitInput) firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = (HtmlPage) button.click();
assertEquals(firstPage.getTitleText(), secondPage.getTitleText());
assertEquals(new String[] {"clicked"}, collectedAlerts);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_javascriptAction_javascriptDisabled() throws Exception {
final String firstContent
= "First\n"
+ "\n"
+ "";
final WebClient client = new WebClient();
client.setJavaScriptEnabled(false);
final List collectedAlerts = new ArrayList();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection(client);
webConnection.setResponse(URL_FIRST, firstContent);
client.setWebConnection(webConnection);
final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
final HtmlSubmitInput button = (HtmlSubmitInput) firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = (HtmlPage) button.click();
assertSame(firstPage, secondPage);
}
/**
* Regression test for a bug that caused a NullPointer exception to be thrown during submit.
* @throws Exception if the test fails
*/
@Test
public void testSubmitRadioButton() throws Exception {
final String htmlContent
= "\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlSubmitInput loginButton
= (HtmlSubmitInput) page.getDocumentElement().getOneHtmlElementByAttribute("input", "value", "Login");
loginButton.click();
}
/**
* @throws Exception if the test fails
*/
@Test
public void testReset_onResetHandler() throws Exception {
final String html
= "First\n"
+ "\n"
+ "";
final List collectedAlerts = new ArrayList();
final HtmlPage firstPage = loadPage(html, collectedAlerts);
final HtmlResetInput button = (HtmlResetInput) firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = (HtmlPage) button.click();
assertSame(firstPage, secondPage);
final String[] expectedAlerts = {"clicked", "reset"};
assertEquals(expectedAlerts, collectedAlerts);
}
/**
*
Simulate a bug report where an anchor contained JavaScript that caused a form submit.
* According to the bug report, the form would be submitted even though the onsubmit
* handler would return false. This wasn't reproducible but I added a test for it anyway.
*
*
UPDATE: If the form submit is triggered by JavaScript then the onsubmit handler is not
* supposed to be called so it doesn't matter what value it returns.
* @throws Exception if the test fails
*/
@Test
public void testSubmit_AnchorCausesSubmit_onSubmitHandler_returnFalse() throws Exception {
final String firstContent
= "First\n"
+ "\n"
+ "\n"
+ "Click me\n"
+ "";
final String secondContent = "Second";
final WebClient client = new WebClient();
final List collectedAlerts = new ArrayList();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection(client);
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setDefaultResponse(secondContent);
client.setWebConnection(webConnection);
final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
final HtmlAnchor anchor = (HtmlAnchor) firstPage.getHtmlElementById("link1");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = (HtmlPage) anchor.click();
assertEquals("Second", secondPage.getTitleText());
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_NoDefaultValue() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button");
button.click();
final List expectedParameters = Arrays.asList(new NameValuePair[]{
new NameValuePair("textfield", ""), new NameValuePair("button", "foo")
});
final List collectedParameters = webConnection.getLastParameters();
assertEquals(expectedParameters, collectedParameters);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_NoNameOnControl() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button");
button.click();
final List expectedParameters =
Arrays.asList(new NameValuePair[]{new NameValuePair("button", "foo")});
final List collectedParameters = webConnection.getLastParameters();
assertEquals(expectedParameters, collectedParameters);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_NoNameOnButton() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlButton button = (HtmlButton) page.getHtmlElementById("button");
button.click();
final List expectedParameters =
Arrays.asList(new NameValuePair[]{new NameValuePair("textfield", "blah")});
final List collectedParameters = webConnection.getLastParameters();
assertEquals(expectedParameters, collectedParameters);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_NestedInput() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button");
button.click();
final List expectedParameters = Arrays.asList(new NameValuePair[]{
new NameValuePair("textfield", "blah"),
new NameValuePair("button", "foo")
});
final List collectedParameters = webConnection.getLastParameters();
assertEquals(expectedParameters, collectedParameters);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_IgnoresDisabledControls() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button");
button.click();
final List expectedParameters =
Arrays.asList(new NameValuePair[]{new NameValuePair("button", "foo")});
final List collectedParameters = webConnection.getLastParameters();
assertEquals(expectedParameters, collectedParameters);
}
/**
* Reset buttons should not be successful controls.
* @see Spec
* @throws Exception if the test fails
*/
@Test
public void testSubmit_IgnoresResetControls() throws Exception {
final String htmlContent = "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("submit");
button.click();
final List expectedParameters =
Arrays.asList(new NameValuePair[] {new NameValuePair("submit", "submit")});
final List collectedParameters = webConnection.getLastParameters();
assertEquals(expectedParameters, collectedParameters);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_CheckboxClicked() throws Exception {
final String htmlContent
= "foo\n"
+ "\n"
+ "\n"
+ "";
final HtmlPage page1 = loadPage(htmlContent);
final MockWebConnection webConnection1 = getMockConnection(page1);
final HtmlForm form1 = (HtmlForm) page1.getHtmlElementById("form1");
final HtmlSubmitInput button1 = (HtmlSubmitInput) form1.getInputByName("button");
final HtmlPage page2 = (HtmlPage) button1.click();
final List collectedParameters1 = webConnection1.getLastParameters();
final List expectedParameters1 =
Arrays.asList(new NameValuePair[] {new NameValuePair("button", "foo")});
final MockWebConnection webConnection2 = getMockConnection(page2);
final HtmlForm form2 = (HtmlForm) page2.getHtmlElementById("form1");
final HtmlCheckBoxInput checkBox2 = (HtmlCheckBoxInput) form2.getInputByName("Format");
final HtmlSubmitInput button2 = (HtmlSubmitInput) form2.getInputByName("button");
checkBox2.click();
button2.click();
final List collectedParameters2 = webConnection2.getLastParameters();
final List expectedParameters2 = Arrays.asList(new NameValuePair[] {
new NameValuePair("Format", "html"),
new NameValuePair("button", "foo")
});
assertEquals(expectedParameters1, collectedParameters1);
assertEquals(expectedParameters2, collectedParameters2);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testGetInputByValue() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final List actualInputs = new ArrayList();
for (final HtmlInput input : form.getInputsByValue("foo")) {
actualInputs.add(input.getNameAttribute());
}
final String[] expectedInputs = {"textfield", "button1", "button2"};
assertEquals("Get all", expectedInputs, actualInputs);
assertEquals(Collections.EMPTY_LIST, form.getInputsByValue("none-matching"));
Assert.assertEquals("Get first", "button", form.getInputByValue("bar").getNameAttribute());
try {
form.getInputByValue("none-matching");
fail("Expected ElementNotFoundException");
}
catch (final ElementNotFoundException e) {
// Expected path.
}
}
/**
* Test that {@link HtmlForm#getTextAreaByName(String)} returns
* the first textarea with the given name.
*
* @throws Exception if the test page can't be loaded
*/
@Test
public void testGetTextAreaByName() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
Assert.assertEquals("First textarea with name 'ta1'", form.getHtmlElementById("ta1_1"),
form.getTextAreaByName("ta1"));
Assert.assertEquals("First textarea with name 'ta2'", form.getHtmlElementById("ta2_1"),
form.getTextAreaByName("ta2"));
try {
form.getTextAreaByName("ta3");
fail("Expected ElementNotFoundException as there is no textarea with name 'ta3'");
}
catch (final ElementNotFoundException e) {
// pass: exception is expected
}
}
/**
* Test that {@link HtmlForm#getButtonByName(String)} returns
* the first button with the given name.
*
* @throws Exception if the test page can't be loaded
*/
@Test
public void testGetButtonByName() throws Exception {
final String htmlContent
= "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
Assert.assertEquals("First button with name 'b1'", form.getHtmlElementById("b1_1"),
form.getButtonByName("b1"));
Assert.assertEquals("First button with name 'b2'", form.getHtmlElementById("b2_1"),
form.getButtonByName("b2"));
try {
form.getTextAreaByName("b3");
fail("Expected ElementNotFoundException as there is no button with name 'b3'");
}
catch (final ElementNotFoundException e) {
// pass: exception is expected
}
}
/**
* Tests that the result of the form will get loaded into the window specified by "target".
* @throws Exception if the test fails
*/
@Test
public void testSubmitToTargetWindow() throws Exception {
final String firstContent
= "first\n"
+ "";
final WebClient client = new WebClient();
final MockWebConnection webConnection = new MockWebConnection(client);
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponseAsGenericHtml(URL_SECOND, "second");
client.setWebConnection(webConnection);
final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
final WebWindow firstWindow = client.getCurrentWindow();
Assert.assertEquals("first window name", "", firstWindow.getName());
assertSame(page, firstWindow.getEnclosedPage());
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button");
final HtmlPage secondPage = (HtmlPage) button.click();
assertEquals("window2", secondPage.getEnclosingWindow().getName());
assertSame(secondPage.getEnclosingWindow(), client.getCurrentWindow());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_SelectHasNoOptions() throws Exception {
final String htmlContent
= "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlPage secondPage = (HtmlPage) page.getFormByName("form").submit((SubmittableElement) null);
assertNotNull(secondPage);
Assert.assertEquals("parameters", Collections.EMPTY_LIST, webConnection.getLastParameters());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testSubmit_SelectOptionWithoutValueAttribute() throws Exception {
final String htmlContent
= "";
final HtmlPage page = loadPage(htmlContent);
final HtmlPage secondPage = (HtmlPage) page.getFormByName("form").submit((SubmittableElement) null);
assertNotNull(secondPage);
assertEquals(page.getWebResponse().getUrl().toExternalForm() + "action.html?select=second+value",
secondPage.getWebResponse().getUrl());
}
/**
* At one point this test was failing because deeply nested inputs weren't getting picked up.
* @throws Exception if the test fails
*/
@Test
public void testSubmit_DeepInputs() throws Exception {
final String htmlContent
= "";
final HtmlPage page = loadPage(htmlContent);
final MockWebConnection webConnection = getMockConnection(page);
final HtmlInput submitButton = (HtmlInput) page.getHtmlElementById("submitButton");
submitButton.click();
final List collectedParameters = webConnection.getLastParameters();
final List expectedParameters = Arrays.asList(new NameValuePair[] {
new NameValuePair("data", "NOT_SUBMITTED"),
new NameValuePair("submit", "Submit Query")
});
assertEquals(expectedParameters, collectedParameters);
}
/**
* Test order of submitted parameters matches order of elements in form.
* @throws Exception if the test fails
*/
@Test
public void testSubmit_FormElementOrder() throws Exception {
final String htmlContent
= "";
final WebClient client = new WebClient();
final MockWebConnection webConnection = new MockWebConnection(client);
webConnection.setDefaultResponse(htmlContent);
client.setWebConnection(webConnection);
final WebRequestSettings settings = new WebRequestSettings(URL_GARGOYLE, HttpMethod.POST);
final HtmlPage page = (HtmlPage) client.getPage(settings);
final HtmlInput submitButton = (HtmlInput) page.getHtmlElementById("submitButton");
submitButton.click();
final List collectedParameters = webConnection.getLastParameters();
final List expectedParameters = Arrays.asList(new NameValuePair[] {
new NameValuePair("dispatch", "Save"),
new NameValuePair("dispatch", "TAB"),
});
assertEquals(expectedParameters, collectedParameters);
}
/**
* Tests the 'Referer' HTTP header.
* @throws Exception on test failure
*/
@Test
public void testSubmit_refererHeader() throws Exception {
final String firstContent
= "First\n"
+ "\n"
+ "";
final String secondContent = "Second";
final WebClient client = new WebClient();
final MockWebConnection webConnection = new MockWebConnection(client);
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);
client.setWebConnection(webConnection);
final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
final HtmlSubmitInput button = (HtmlSubmitInput) firstPage.getHtmlElementById("button");
button.click();
final Map lastAdditionalHeaders = webConnection.getLastAdditionalHeaders();
assertEquals(URL_FIRST.toString(), lastAdditionalHeaders.get("Referer"));
}
/**
* Simulates a bug report where using JavaScript to submit a form that contains a
* JavaScript action causes a an "IllegalArgumentException: JavaScript URLs can only
* be used to load content into frames and iframes".
*
* @throws Exception if the test fails
*/
@Test
public void testJSSubmit_JavaScriptAction() throws Exception {
final String htmlContent
= "First\n"
+ "\n"
+ "";
final String[] expectedAlerts = {"1", "val2"};
createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
final List collectedAlerts = new ArrayList();
loadPage(content, collectedAlerts);
assertEquals(expectedAlerts, collectedAlerts);
}
/**
* @throws Exception if the test page can't be loaded
*/
@Test
public void malformedHtml_fieldGetters() throws Exception {
final String content
= "foo\n"
+ "