1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import com.gargoylesoftware.htmlunit.MockWebConnection; 41 import com.gargoylesoftware.htmlunit.WebClient; 42 import com.gargoylesoftware.htmlunit.WebTestCase; 43 44 50 public class HtmlOptionTest extends WebTestCase { 51 55 public HtmlOptionTest( final String name ) { 56 super(name); 57 } 58 59 60 63 public void testSelect() throws Exception { 64 65 final String htmlContent 66 = "<html><head><title>foo</title></head><body>" 67 + "<form id='form1'><select name='select1' id='select1'>" 68 + "<option value='option1' id='option1'>Option1</option>" 69 + "<option value='option2' id='option2' selected='selected'>Option2</option>" 70 + "<option value='option3' id='option3'>Option3</option>" 71 + "</select>" 72 + "<input type='submit' name='button' value='foo'/>" 73 + "</form></body></html>"; 74 final HtmlPage page = loadPage(htmlContent); 75 76 final HtmlOption option1 = ( HtmlOption )page.getHtmlElementById( "option1" ); 77 final HtmlOption option2 = ( HtmlOption )page.getHtmlElementById( "option2" ); 78 final HtmlOption option3 = ( HtmlOption )page.getHtmlElementById( "option3" ); 79 80 assertFalse( option1.isSelected() ); 81 assertTrue ( option2.isSelected() ); 82 assertFalse( option3.isSelected() ); 83 84 option3.setSelected(true); 85 86 assertFalse( option1.isSelected() ); 87 assertFalse( option2.isSelected() ); 88 assertTrue ( option3.isSelected() ); 89 90 option3.setSelected(false); 91 92 assertFalse( option1.isSelected() ); 93 assertFalse( option2.isSelected() ); 94 assertFalse( option3.isSelected() ); 95 } 96 97 100 public void testGetValue() throws Exception { 101 102 final String htmlContent 103 = "<html><head><title>foo</title></head><body>" 104 + "<form id='form1'><select name='select1' id='select1'>" 105 + "<option value='option1' id='option1'>Option1</option>" 106 + "<option id='option2' selected>Number Two</option>" 107 + "</select>" 108 + "<input type='submit' name='button' value='foo'/>" 109 + "</form></body></html>"; 110 final WebClient client = new WebClient(); 111 112 final MockWebConnection webConnection = new MockWebConnection( client ); 113 webConnection.setDefaultResponse( htmlContent ); 114 client.setWebConnection( webConnection ); 115 116 final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST); 117 118 final HtmlOption option1 = ( HtmlOption )page.getHtmlElementById( "option1" ); 119 final HtmlOption option2 = ( HtmlOption )page.getHtmlElementById( "option2" ); 120 121 assertEquals("option1", option1.getValue()); 122 assertEquals("Number Two", option2.getValue()); 123 124 } 125 128 public void testGetValue_ContentsIsValue() throws Exception { 129 130 final String htmlContent 131 = "<html><head><title>foo</title></head><body>" 132 + "<form id='form1'>" 133 + "<select name='select1' id='select1'>" 134 + " <option id='option1'>Option1</option>" 135 + " <option id='option2' selected>Number Two</option>" 136 + "</select>" 137 + "<input type='submit' name='button' value='foo'/>" 138 + "</form></body></html>"; 139 final WebClient client = new WebClient(); 140 141 final MockWebConnection webConnection = new MockWebConnection( client ); 142 webConnection.setDefaultResponse( htmlContent ); 143 client.setWebConnection( webConnection ); 144 145 final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST); 146 147 final HtmlOption option1 = ( HtmlOption )page.getHtmlElementById( "option1" ); 148 final HtmlOption option2 = ( HtmlOption )page.getHtmlElementById( "option2" ); 149 150 assertEquals("Option1", option1.getValue()); 151 assertEquals("Number Two", option2.getValue()); 152 153 } 154 } 155 | Popular Tags |