1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 import com.gargoylesoftware.htmlunit.WebTestCase; 44 45 51 public class HtmlTableTest extends WebTestCase { 52 57 public HtmlTableTest( final String name ) { 58 super( name ); 59 } 60 61 62 67 public void testGetTableCell() throws Exception { 68 final String htmlContent 69 = "<html><head><title>foo</title></head><body>" 70 + "<table id='table1' summary='Test table'>" 71 + "<tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>" 72 + "<tr><td colspan='2'>cell3</td></tr>" 73 + "</table>" 74 + "</body></html>"; 75 final HtmlPage page = loadPage(htmlContent); 76 77 final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" ); 78 79 final HtmlTableCell cell1 = table.getCellAt( 0, 0 ); 80 assertEquals( "cell1 contents", "cell1", cell1.asText() ); 81 82 final HtmlTableCell cell2 = table.getCellAt( 0, 1 ); 83 assertEquals( "cell2 contents", "cell2", cell2.asText() ); 84 85 final HtmlTableCell cell3 = table.getCellAt( 1, 0 ); 86 assertEquals( "cell3 contents", "cell3", cell3.asText() ); 87 assertSame( "cells (1,0) and (1,1)", cell3, table.getCellAt( 1, 1 ) ); 88 89 final HtmlTableCell cell4 = table.getCellAt( 0, 2 ); 90 assertEquals( "cell4 contents", "cell4", cell4.asText() ); 91 assertSame( "cells (0,2) and (1,2)", cell4, table.getCellAt( 1, 2 ) ); 92 } 93 94 95 100 public void testGetTableCell_NotFound() 101 throws Exception { 102 103 final String htmlContent 104 = "<html><head><title>foo</title></head><body>" 105 + "<table id='table1' summary='Test table'>" 106 + "<tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>" 107 + "<tr><td colspan='2'>cell3</td></tr>" 108 + "</table>" 109 + "</body></html>"; 110 final HtmlPage page = loadPage(htmlContent); 111 112 final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" ); 113 114 final HtmlTableCell cell = table.getCellAt( 99, 0 ); 115 assertNull( "cell", cell ); 116 } 117 118 119 122 public void testGetTableRows() 123 throws Exception { 124 125 final String htmlContent 126 = "<html><head><title>foo</title></head><body>" 127 + "<table id='table1'>" 128 + "<tr id='row1'><td>cell1</td></tr>" 129 + "<tr id='row2'><td>cell2</td></tr>" 130 + "<tr id='row3'><td>cell3</td></tr>" 131 + "<tr id='row4'><td>cell4</td></tr>" 132 + "<tr id='row5'><td>cell5</td></tr>" 133 + "<tr id='row6'><td>cell6</td></tr>" 134 + "</table>" 135 + "</body></html>"; 136 final HtmlPage page = loadPage(htmlContent); 137 138 final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" ); 139 140 final List expectedRows = new ArrayList(); 141 expectedRows.add( table.getRowById( "row1" ) ); 142 expectedRows.add( table.getRowById( "row2" ) ); 143 expectedRows.add( table.getRowById( "row3" ) ); 144 expectedRows.add( table.getRowById( "row4" ) ); 145 expectedRows.add( table.getRowById( "row5" ) ); 146 expectedRows.add( table.getRowById( "row6" ) ); 147 148 assertEquals( expectedRows, table.getRows() ); 149 } 150 151 152 155 public void testGetTableRows_WithHeadBodyFoot() 156 throws Exception { 157 158 final String htmlContent 159 = "<html><head><title>foo</title></head><body>" 160 + "<table id='table1'>" 161 + "<thead>" 162 + " <tr id='row1'><td>cell1</td></tr>" 163 + " <tr id='row2'><td>cell2</td></tr>" 164 + "</thead>" 165 + "<tbody>" 166 + " <tr id='row3'><td>cell3</td></tr>" 167 + " <tr id='row4'><td>cell4</td></tr>" 168 + "</tbody>" 169 + "<tfoot>" 170 + " <tr id='row5'><td>cell5</td></tr>" 171 + " <tr id='row6'><td>cell6</td></tr>" 172 + "</tfoot>" 173 + "</table>" 174 + "</body></html>"; 175 final HtmlPage page = loadPage(htmlContent); 176 177 final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" ); 178 179 final List expectedRows = new ArrayList(); 180 expectedRows.add( table.getRowById( "row1" ) ); 181 expectedRows.add( table.getRowById( "row2" ) ); 182 expectedRows.add( table.getRowById( "row3" ) ); 183 expectedRows.add( table.getRowById( "row4" ) ); 184 expectedRows.add( table.getRowById( "row5" ) ); 185 expectedRows.add( table.getRowById( "row6" ) ); 186 187 assertEquals( expectedRows, table.getRows() ); 188 } 189 190 191 194 public void testRowGroupings_AllDefined() 195 throws Exception { 196 197 final String htmlContent 198 = "<html><head><title>foo</title></head><body>" 199 + "<table id='table1'>" 200 + "<thead>" 201 + " <tr id='row1'><td>cell1</td></tr>" 202 + " <tr id='row2'><td>cell2</td></tr>" 203 + "</thead>" 204 + "<tbody>" 205 + " <tr id='row3'><td>cell3</td></tr>" 206 + "</tbody>" 207 + "<tbody>" 208 + " <tr id='row4'><td>cell4</td></tr>" 209 + "</tbody>" 210 + "<tfoot>" 211 + " <tr id='row5'><td>cell5</td></tr>" 212 + " <tr id='row6'><td>cell6</td></tr>" 213 + "</tfoot>" 214 + "</table>" 215 + "</body></html>"; 216 final HtmlPage page = loadPage(htmlContent); 217 218 final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" ); 219 220 assertNotNull( table.getHeader() ); 221 assertNotNull( table.getFooter() ); 222 assertEquals( 2, table.getBodies().size() ); 223 } 224 225 226 231 public void testRowGroupings_NoneDefined() 232 throws Exception { 233 234 final String htmlContent 235 = "<html><head><title>foo</title></head><body>" 236 + "<table id='table1'>" 237 + " <tr id='row1'><td>cell1</td></tr>" 238 + " <tr id='row2'><td>cell2</td></tr>" 239 + " <tr id='row3'><td>cell3</td></tr>" 240 + " <tr id='row4'><td>cell4</td></tr>" 241 + " <tr id='row5'><td>cell5</td></tr>" 242 + " <tr id='row6'><td>cell6</td></tr>" 243 + "</table>" 244 + "</body></html>"; 245 final HtmlPage page = loadPage(htmlContent); 246 247 final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" ); 248 249 assertEquals( null, table.getHeader() ); 250 assertEquals( null, table.getFooter() ); 251 assertEquals( 1, table.getBodies().size() ); 252 } 253 254 255 258 public void testGetCaptionText() 259 throws Exception { 260 261 final String htmlContent 262 = "<html><head><title>foo</title></head><body>" 263 + "<table id='table1' summary='Test table'>" 264 + "<caption>MyCaption</caption>" 265 + "<tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>" 266 + "<tr><td colspan='2'>cell3</td></tr>" 267 + "</table>" 268 + "</body></html>"; 269 final HtmlPage page = loadPage(htmlContent); 270 271 final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" ); 272 273 assertEquals("MyCaption", table.getCaptionText()); 274 } 275 276 282 public void testInsertionOfTbodyTags() throws Exception { 283 284 final String htmlContent 285 = "<html><head><title>foo</title></head><body>" 286 + "<table>" 287 + "<tr><td id='cell1'>cell1</td></tr>" 288 + "</table>" 289 + "<table><tbody>" 290 + "<tr><td id='cell2'>cell1</td></tr>" 291 + "</tbody></table>" 292 + "</body></html>"; 293 final HtmlPage page = loadPage(htmlContent); 294 295 final HtmlTableDataCell cell1 = ( HtmlTableDataCell )page.getHtmlElementById( "cell1" ); 297 assertInstanceOf( cell1.getParentNode(), HtmlTableRow.class ); 298 assertInstanceOf( cell1.getParentNode().getParentNode(), HtmlTableBody.class ); 299 assertInstanceOf( cell1.getParentNode().getParentNode().getParentNode(), HtmlTable.class ); 300 301 final HtmlTableDataCell cell2 = ( HtmlTableDataCell )page.getHtmlElementById( "cell2" ); 303 assertInstanceOf( cell2.getParentNode(), HtmlTableRow.class ); 304 assertInstanceOf( cell2.getParentNode().getParentNode(), HtmlTableBody.class ); 305 assertInstanceOf( cell2.getParentNode().getParentNode().getParentNode(), HtmlTable.class ); 306 } 307 } 308 309 | Popular Tags |