From: Baubak G. <Wo...@Ga...> - 2012-12-07 09:02:07
|
Hi There, This is not a bug report, but more a small complaint about syntax and its use. I am having a problem with the logic of the method: com.gargoylesoftware.htmlunit.html.HtmlElement.hasHtmlElementWithId(String) It’s logic is : Returns true if there is an element in this element's page with the specified ID. Now I want to test it to see if a row with a given id is present in the given table: <tableid="t3"> <tr> <td>c</td> <td>d</td> <td>e</td> </tr> <tr> <td>1</td> <td>2</td> <td></td> <td>4</td> </tr> <tr> <td>+</td> </tr> </table> <tableid="t4"> <trid="row1"> <td>c</td> <td>d</td> <td>e</td> </tr> <trid="row2"> <td>1</td> <td>2</td> <td></td> <td>4</td> </tr> <trid="row3"> <td>+</td> </tr> In my case I create an HtmlTable object with the table with the id “t4”. Here is my test: @Test publicvoidtestGetLastCellById_withoutId() throwsFailingHttpStatusCodeException, IOException { HtmlTable l_currentTable = (HtmlTable) ((HtmlPage) webClient.getPage(getClass().getResource( "/testExamples/HTMLTableTest.html"))).getElementById("t3"); assertEquals("e", DBExelTestTools.getLastCell(l_currentTable, "row1").getTextContent()); } My goal is that if the “id” is not present in the table I return the last cell of the first row. Here is the code of DBExelTestTools.getLastCell. I look for an id = “row1” in a table with the id “t3”that does not have that id. /** * This method returns the last cell in the given row in the given table, * given the id of the row. If not rows have id's it is the very first row * that is returned * * @paramin_table * @paramin_id * @return */ publicstaticHtmlTableCell getLastCell(HtmlTable in_table, String in_id) { if(in_table.hasHtmlElementWithId(in_id)) { HtmlTableRow l_tableRow = in_table.getRowById(in_id); returnl_tableRow.getCell(l_tableRow.getCells().size() - 1); } else{ returngetLastCell(in_table, 0); } } What happens is that in the condition “if(in_table.hasHtmlElementWithId(in_id))” is “true” because there is a row in another table that contains that id. Although it is according to the specification, it does not seem very intuitive. I could solve this with a try/catch for ElementNotFound, but that is not considered as good practice… Best regards Baubak Gandomi Visit the wonderful : http://www.uglycityguide.com |