TableService extends BaseServiceHub { public void compareTable(ExpectedTableDefinition expectedContent, WebElement table) throws Throwable { try { SenBotContext.getSeleniumDriver().manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS); List<List<String>> expectedRows = expectedContent.getExpected().raw(); List<WebElement> foundRows = table.findElements(By.xpath("tr | */tr")); List<WebElement> filteredRows = new ArrayList<WebElement>(); expectedContent.cacheIncludeAndIgnore(table); for (WebElement row : foundRows) { if(!row.isDisplayed()) { continue; } if ((expectedContent.getIncludeByMatches().isEmpty() || expectedContent.getIncludeByMatches().contains(row)) && !expectedContent.getIgnoreByMatches().contains(row)) { filteredRows.add(row); } } assertEquals("The found table should have the expected number of rows", expectedRows.size(), filteredRows.size()); LinkedHashMap<String, Integer> columnHeaders = new LinkedHashMap<String, Integer>(); for (int rowCount = 0; rowCount < expectedRows.size(); rowCount++) { WebElement foundRow = filteredRows.get(rowCount); List<WebElement> foundCells = foundRow.findElements(By.xpath("td")); foundCells.addAll(foundRow.findElements(By.xpath("th"))); List<String> expectedRow = expectedRows.get(rowCount); if (rowCount == 0) { List<WebElement> registeredColumnHeaders = new ArrayList<WebElement>(); for (int c = 0; c < expectedRow.size(); c++) { String expectedHeader = expectedRow.get(c); String expectedHeaderKey = expectedHeader + "_" + c; Integer foundHeaderIndex = 0; for (WebElement foundHeaderCell : foundCells) { String foundHeaderText = foundHeaderCell.getText().trim(); foundHeaderText = foundHeaderText.replaceAll("\n", " "); if (foundHeaderText.equals(expectedHeader) && !registeredColumnHeaders.contains(foundHeaderCell)) { if (columnHeaders.get(expectedHeaderKey) != null && expectedContent.isMatchOnlyPassedInColumns()) { fail("The expected column header \"" + expectedHeader + "\" is not unique in the HTML table"); } columnHeaders.put(expectedHeaderKey, foundHeaderIndex); registeredColumnHeaders.add(foundHeaderCell); break; } foundHeaderIndex++; } if (columnHeaders.get(expectedHeaderKey) == null) { fail("The expected column header \"" + expectedHeaderKey + "\" was not found in the HTML table"); } } } int cellCount = 0; int expectedSpanCells = 0; for (String columnHeaderKey : columnHeaders.keySet()) { String expectedValue = getReferenceService().namespaceString(expectedRow.get(cellCount).trim()); if ("<rowspan>".equals(expectedValue) || "<colspan>".equals(expectedValue)) { expectedSpanCells++; } else { Integer foundColumnIndex = columnHeaders.get(columnHeaderKey); String foundValue = foundCells.get(foundColumnIndex - expectedSpanCells).getText(); foundValue = foundValue.trim().replaceAll("\n", " "); assertEquals("Cell " + (cellCount + 1) + " (column: " + columnHeaderKey + ") of row " + (rowCount + 1) + " should be as expected ", expectedValue, foundValue); } cellCount++; } if (expectedContent.isMatchOnlyPassedInColumns()) { assertEquals("Found row " + rowCount + " should have the expected number of cells", expectedRow.size(), columnHeaders.size()); } else { assertEquals("Found row " + rowCount + " should have the expected number of cells", expectedRow.size(), foundCells.size() + expectedSpanCells); } } } finally { SenBotContext.getSeleniumDriver().manage().timeouts().implicitlyWait(SenBotContext.getSenBotContext().getSeleniumManager().getTimeout(), TimeUnit.MILLISECONDS); } } TableService(ElementService seleniumElementService); void compareTable(ExpectedTableDefinition expectedContent, WebElement table);  }
@Test public void testCompareTable_fullMatch() throws Throwable { seleniumNavigationService.navigate_to_url(MockReferenceDatePopulator.TABLE_TEST_PAGE_URL); WebElement table = seleniumElementService.translateLocatorToWebElement("Table locator"); List<List<String>> expectedRows = new ArrayList<List<String>>(); final List<String> header = Arrays.asList(new String[]{"Table header 1", "Table header 2"}); final List<String> row1 = Arrays.asList(new String[]{"Table cell 1", "Table cell 2"}); final List<String> row2 = Arrays.asList(new String[]{"Table cell 3", "Table cell 4"}); final List<String> row3 = Arrays.asList(new String[]{"Table cell 5", "Table cell 6"}); expectedRows = new ArrayList<List<String>>() { { add(header); add(row1); add(row2); add(row3); } }; DataTable expectedContent = mock(DataTable.class); when(expectedContent.raw()).thenReturn(expectedRows); ExpectedTableDefinition expectedTableDefinition = new ExpectedTableDefinition(expectedContent); seleniumTableService.compareTable(expectedTableDefinition, table); }
@Test public void testCompareTable_fullMatch_complex() throws Throwable { seleniumNavigationService.navigate_to_url(MockReferenceDatePopulator.COMPLEX_TABLE_TEST_PAGE_URL); WebElement table = seleniumElementService.translateLocatorToWebElement("Table locator"); List<List<String>> expectedRows = new ArrayList<List<String>>(); final List<String> header = Arrays.asList(new String[]{"Table header 1", "Table header", "", "", "Table header 5", "Table header"}); final List<String> row1 = Arrays.asList(new String[]{"Table cell 1_1", "Table cell 1_2", "Table cell 1_3", "Table cell 1_4", "Table cell 1_5", "Table cell 1_6"}); final List<String> row2 = Arrays.asList(new String[]{"<rowspan>", "Table cell 2_2", "Table cell 2_3", "Table cell 2_4", "<colspan>", "<colspan>"}); final List<String> row3 = Arrays.asList(new String[]{"Table cell 3_1", "Table cell 3_2", "Table cell 3_3", "Table cell 3_4", "Table cell 3_5", "Table cell 3_6"}); final List<String> row4 = Arrays.asList(new String[]{"Table cell 4_1", "Table cell 4_2", "Table cell 4_3", "Table cell 4_4", "Table cell 4_5", "Table cell 4_6"}); expectedRows = new ArrayList<List<String>>() { { add(header); add(row1); add(row2); add(row3); add(row4); } }; DataTable expectedContent = mock(DataTable.class); when(expectedContent.raw()).thenReturn(expectedRows); ExpectedTableDefinition expectedTableDefinition = new ExpectedTableDefinition(expectedContent); seleniumTableService.compareTable(expectedTableDefinition, table); this.toString(); }
@Test(expected = AssertionError.class) public void testCompareTable_mismatch() throws Throwable { seleniumNavigationService.navigate_to_url(MockReferenceDatePopulator.TABLE_TEST_PAGE_URL); WebElement table = seleniumElementService.translateLocatorToWebElement("Table locator"); List<List<String>> expectedRows = new ArrayList<List<String>>(); final List<String> header = Arrays.asList(new String[]{"Table header 1", "Table header 2"}); final List<String> row1 = Arrays.asList(new String[]{"Table cell 1.1", "Table cell 2"}); final List<String> row2 = Arrays.asList(new String[]{"Table cell 3", "Table cell 4"}); final List<String> row3 = Arrays.asList(new String[]{"Table cell 5", "Table cell 6"}); expectedRows = new ArrayList<List<String>>() { { add(header); add(row1); add(row2); add(row3); } }; DataTable expectedContent = mock(DataTable.class); when(expectedContent.raw()).thenReturn(expectedRows); ExpectedTableDefinition expectedTableDefinition = new ExpectedTableDefinition(expectedContent); seleniumTableService.compareTable(expectedTableDefinition, table); }
@Test public void testCompareTable_rowIgnore() throws Throwable { seleniumNavigationService.navigate_to_url(MockReferenceDatePopulator.TABLE_TEST_PAGE_URL); WebElement table = seleniumElementService.translateLocatorToWebElement("Table locator"); List<List<String>> expectedRows = new ArrayList<List<String>>(); final List<String> row1 = Arrays.asList(new String[]{"Table cell 1", "Table cell 2"}); final List<String> row3 = Arrays.asList(new String[]{"Table cell 5", "Table cell 6"}); expectedRows = new ArrayList<List<String>>() { { add(row1); add(row3); } }; DataTable expectedContent = mock(DataTable.class); when(expectedContent.raw()).thenReturn(expectedRows); ExpectedTableDefinition expectedTableDefinition = new ExpectedTableDefinition(expectedContent); expectedTableDefinition.getIgnoreRowsMatching().add(By.className("even")); expectedTableDefinition.getIgnoreRowsMatching().add(By.id("headerRow")); seleniumTableService.compareTable(expectedTableDefinition, table); }
@Test public void testCompareTable_rowIncludeAndIgnore() throws Throwable { seleniumNavigationService.navigate_to_url(MockReferenceDatePopulator.TABLE_TEST_PAGE_URL); WebElement table = seleniumElementService.translateLocatorToWebElement("Table locator"); List<List<String>> expectedRows = new ArrayList<List<String>>(); final List<String> row3 = Arrays.asList(new String[]{"Table cell 5", "Table cell 6"}); expectedRows = new ArrayList<List<String>>() { { add(row3); } }; DataTable expectedContent = mock(DataTable.class); when(expectedContent.raw()).thenReturn(expectedRows); ExpectedTableDefinition expectedTableDefinition = new ExpectedTableDefinition(expectedContent); expectedTableDefinition.getIncludeOnlyRowsMatching().add(By.className("odd")); expectedTableDefinition.getIgnoreRowsMatching().add(By.id("row1")); seleniumTableService.compareTable(expectedTableDefinition, table); }
@Test public void testCompareTable_columnSelection() throws Throwable { seleniumNavigationService.navigate_to_url(MockReferenceDatePopulator.TABLE_TEST_PAGE_URL); WebElement table = seleniumElementService.translateLocatorToWebElement("Table locator"); List<List<String>> expectedRows = new ArrayList<List<String>>(); final List<String> header = Arrays.asList(new String[]{"Table header 2"}); final List<String> row1 = Arrays.asList(new String[]{"Table cell 2"}); final List<String> row2 = Arrays.asList(new String[]{"Table cell 4"}); final List<String> row3 = Arrays.asList(new String[]{"Table cell 6"}); expectedRows = new ArrayList<List<String>>() { { add(header); add(row1); add(row2); add(row3); } }; DataTable expectedContent = mock(DataTable.class); when(expectedContent.raw()).thenReturn(expectedRows); ExpectedTableDefinition expectedTableDefinition = new ExpectedTableDefinition(expectedContent); expectedTableDefinition.setMatchOnlyPassedInColumns(true); seleniumTableService.compareTable(expectedTableDefinition, table); }
@Test(expected = AssertionError.class) public void testCompareTable_columnSelectionWithNonUniqueColuns() throws Throwable { seleniumNavigationService.navigate_to_url(MockReferenceDatePopulator.COMPLEX_TABLE_TEST_PAGE_URL); WebElement table = seleniumElementService.translateLocatorToWebElement("Table locator"); List<List<String>> expectedRows = new ArrayList<List<String>>(); final List<String> header = Arrays.asList(new String[]{"Table header 1", "Table header", "", "Table header 5"}); final List<String> row1 = Arrays.asList(new String[]{"Table cell 1_1", "Table cell 1_2", "Table cell 1_4", "Table cell 1_5"}); final List<String> row2 = Arrays.asList(new String[]{"Table cell 2_1", "Table cell 2_2", "Table cell 2_4", "Table cell 2_5"}); final List<String> row3 = Arrays.asList(new String[]{"Table cell 3_1", "Table cell 3_2", "Table cell 3_4", "Table cell 3_5"}); final List<String> row4 = Arrays.asList(new String[]{"Table cell 4_1", "Table cell 4_2", "Table cell 4_4", "Table cell 4_5"}); expectedRows = new ArrayList<List<String>>() { { add(header); add(row1); add(row2); add(row3); add(row4); } }; DataTable expectedContent = mock(DataTable.class); when(expectedContent.raw()).thenReturn(expectedRows); ExpectedTableDefinition expectedTableDefinition = new ExpectedTableDefinition(expectedContent); expectedTableDefinition.setMatchOnlyPassedInColumns(true); seleniumTableService.compareTable(expectedTableDefinition, table); }
@Test public void testCompareTable_columnSelectionWithNonUniqueColunsOutsideMatch() throws Throwable { seleniumNavigationService.navigate_to_url(MockReferenceDatePopulator.COMPLEX_TABLE_TEST_PAGE_URL); WebElement table = seleniumElementService.translateLocatorToWebElement("Table locator"); List<List<String>> expectedRows = new ArrayList<List<String>>(); final List<String> header = Arrays.asList(new String[]{"Table header 1", "Table header 5"}); final List<String> row1 = Arrays.asList(new String[]{"Table cell 1_1", "Table cell 1_5"}); final List<String> row2 = Arrays.asList(new String[]{"<rowspan>", "<colspan>"}); final List<String> row3 = Arrays.asList(new String[]{"Table cell 3_1", "Table cell 3_5"}); final List<String> row4 = Arrays.asList(new String[]{"Table cell 4_1", "Table cell 4_5"}); expectedRows = new ArrayList<List<String>>() { { add(header); add(row1); add(row2); add(row3); add(row4); } }; DataTable expectedContent = mock(DataTable.class); when(expectedContent.raw()).thenReturn(expectedRows); ExpectedTableDefinition expectedTableDefinition = new ExpectedTableDefinition(expectedContent); expectedTableDefinition.setMatchOnlyPassedInColumns(true); seleniumTableService.compareTable(expectedTableDefinition, table); }
@Test public void testCompareTable_withNameSpacing() throws Throwable { seleniumNavigationService.navigate_to_url(MockReferenceDatePopulator.TABLE_NAMESPACE_TEST_PAGE_URL); String unNamespacenizedString = SenBotReferenceService.NAME_SPACE_PREFIX + "Table cell 3"; String namespacenizedString = SenBotContext.getSenBotContext().getReferenceService().namespaceString(unNamespacenizedString); List<List<String>> expectedRows = new ArrayList<List<String>>(); final List<String> header = Arrays.asList(new String[]{"Table header 1", "Table header 2"}); final List<String> row1 = Arrays.asList(new String[]{"Table cell 1", "Table cell 2"}); final List<String> row2 = Arrays.asList(new String[]{unNamespacenizedString, "Table cell 4"}); final List<String> row3 = Arrays.asList(new String[]{"Table cell 5", "Table cell 6"}); expectedRows = new ArrayList<List<String>>() { { add(header); add(row1); add(row2); add(row3); } }; DataTable expectedContent = mock(DataTable.class); when(expectedContent.raw()).thenReturn(expectedRows); ExpectedTableDefinition expectedTableDefinition = new ExpectedTableDefinition(expectedContent); expectedTableDefinition.setMatchOnlyPassedInColumns(true); WebDriver driver = SenBotContext.getSeleniumDriver(); WebElement inputField = driver.findElement(By.id("textField")); inputField.sendKeys(namespacenizedString); seleniumElementService.findExpectedElement(By.xpath(". WebElement table = driver.findElement(By.id("exampleTable")); seleniumTableService.compareTable(expectedTableDefinition, table); this.toString(); }