1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 import com.gargoylesoftware.htmlunit.KeyValuePair; 44 import com.gargoylesoftware.htmlunit.MockWebConnection; 45 import com.gargoylesoftware.htmlunit.Page; 46 import com.gargoylesoftware.htmlunit.SubmitMethod; 47 import com.gargoylesoftware.htmlunit.WebTestCase; 48 49 55 public class HtmlTextAreaTest extends WebTestCase { 56 61 public HtmlTextAreaTest( final String name ) { 62 super( name ); 63 } 64 65 66 69 public void testFormSubmission_OriginalData() 70 throws Exception { 71 final String htmlContent 72 = "<html><head><title>foo</title></head><body>" 73 + "<form id='form1'>" 74 + "<textarea name='textArea1'>foo</textarea>" 75 + "</form></body></html>"; 76 final HtmlPage page = loadPage(htmlContent); 77 final MockWebConnection webConnection = getMockConnection(page); 78 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 79 80 final HtmlTextArea textArea 81 = ( HtmlTextArea )form.getTextAreasByName( "textArea1" ).get( 0 ); 82 assertNotNull(textArea); 83 84 final Page secondPage = form.submit(); 85 86 final List expectedParameters = new ArrayList(); 87 expectedParameters.add( new KeyValuePair( "textArea1", "foo" ) ); 88 89 assertEquals("url", URL_GARGOYLE, secondPage.getWebResponse().getUrl()); 90 assertEquals( "method", SubmitMethod.GET, webConnection.getLastMethod() ); 91 assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() ); 92 } 93 94 95 98 public void testFormSubmission_NewValue() 99 throws Exception { 100 final String htmlContent 101 = "<html><head><title>foo</title></head><body>" 102 + "<form id='form1'>" 103 + "<textarea name='textArea1'>foo</textarea>" 104 + "</form></body></html>"; 105 final HtmlPage page = loadPage(htmlContent); 106 final MockWebConnection webConnection = getMockConnection(page); 107 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 108 109 final HtmlTextArea textArea 110 = ( HtmlTextArea )form.getTextAreasByName( "textArea1" ).get( 0 ); 111 textArea.setText( "Flintstone" ); 112 final Page secondPage = form.submit(); 113 114 final List expectedParameters = new ArrayList(); 115 expectedParameters.add( new KeyValuePair( "textArea1", "Flintstone" ) ); 116 117 assertEquals("url", URL_GARGOYLE, secondPage.getWebResponse().getUrl()); 118 assertEquals( "method", SubmitMethod.GET, webConnection.getLastMethod() ); 119 assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() ); 120 } 121 124 public void testGetText() throws Exception { 125 final String htmlContent 126 = "<html><head><title>foo</title></head><body>" 127 + "<form id='form1'>" 128 + "<textarea name='textArea1'> foo \n bar </textarea>" 129 + "</form></body></html>"; 130 final HtmlPage page = loadPage(htmlContent); 131 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 132 133 final HtmlTextArea textArea 134 = ( HtmlTextArea )form.getTextAreasByName( "textArea1" ).get( 0 ); 135 assertNotNull(textArea); 136 assertEquals("White space must be preserved!", " foo \n bar ", textArea.getText()); 137 } 138 } 139 140 | Popular Tags |