I am wanting to create a custom method in a Base page object that will allow me to validate the results returned in a dynamic table after searching.
I have the below method ..
It takes as arguments the WebElement table, a column name that I want to validate against and and expected value that I expect to be returned by a row of that column.
I am trying to find the column index of the column with name that I pass in as columnName and assign it to a variable called searchedColIndex then use this to get all values returned under that column and then validate whether the expected result was in the list.
However I am not sure how to get the index of the column.
Also I never seem to get inside the if statement, even though when I print both column.getText() and columnName to console the values are correct and should match for one of columns, but that if statement is never evaluated as true.
public void validateTableData2(WebElement table,String columnName, String expectedSearchResult){
List<WebElement> tableRows = table.findElements(By.xpath(".//tr"));
List<WebElement> tableColumns = table.findElements(By.tagName("th"));
int searchedColIndex = 1;
for (WebElement column : tableColumns) {
if(column.getText().trim() == columnName.trim()){
System.out.println("We don't get in this if statement");
System.out.println("The values in column headers are available as if I print column.getText() then it does return correct column headings");
//Not sure how I can assign the index of the column I am interested in
//column.getText() does
}
}
List<String> returnedVals = new ArrayList<String>();
for (int i = 1; i < tableRows.size(); i ) {
String returnedVal = table.findElement(By.xpath("./tbody/tr[" i "]/td[" searchedColIndex "]")).getText();
returnedVals.add(returnedVal);
}
Assert.assertTrue(returnedVals.contains(expectedSearchResult));
}
Any help would be much appreciated.
CodePudding user response:
For your second question, use equals() instead of ==, then you should get inside the if statement.
CodePudding user response:
I was able to come up with the below method, which seems to work, although perhaps won't work for every table I try to use it with ..
public void validateTableData2(WebElement table,String columnName, String expectedSearchResult){
List<WebElement> tableRows = table.findElements(By.xpath(".//tr"));
System.out.println("Col name is " columnName);
int searchedColIndex = table.findElements(By.xpath("//thead/tr/th/a[text()='" columnName "']/../preceding-sibling::th")).size() 1;
List<String> returnedVals = new ArrayList<String>();
for (int i = 1; i < tableRows.size(); i ) {
String returnedVal = table.findElement(By.xpath("./tbody/tr[" i "]/td[" searchedColIndex "]")).getText();
System.out.println("Returned val is " returnedVal);
returnedVals.add(returnedVal);
}
Assert.assertTrue(returnedVals.contains(expectedSearchResult));
}