I got a table where I created a loop for searching a specific element here is the code for the loop that searches a specific item in the table(I know the code is trash but I don’t know coding):
def v = WebUI.getAttribute(findTestObject('Object Repository/Page_LeadManager - Leads/input_Leads_searchInput'),'value')
//WebUI.click(findTestObject('Object Repository/Page_LeadManager - Leads/input_Modal Title_form-control'))
//WebUI.setText(findTestObject('verifiyelements/Page_LeadManager - Leads/input_Leads_searchInput'), address)
//WebUI.sendKeys(findTestObject('Object Repository/Page_LeadManager - Leads/input_Leads_searchInput'), Keys.chord(Keys.ENTER))
String ExpectedValue = v;
WebDriver driver = DriverFactory.getWebDriver()
//Expected value from Table
WebElement Table = driver.findElement(By.xpath('//*[@id="leadtable"]'));
//To locate table
rows_table = Table.findElements(By.tagName('/tr/th'));
// To locate rows of table it will Capture all the rows available in the table
List<WebElement> rows_table = Table.findElements(By.tagName('tr'));
// To calculate no of rows In table
int rows_count = rows_table.size();
println("rows_count=${rows_count}");
// Loop will execute for all the rows of the table
Loop:
for (int row = 0; row < rows_count; row++) {
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName('td'));
int columns_count = Columns_row.size();
println((('Number of cells In Row ' + row) + ' are ') + columns_count);
boolean found = false
for (int column = 0; column < columns_count; column++) {
celltext = Columns_row.get(column).getText()
println((((('Cell Value Of row number ' + row) + ' and column number ') + column) + ' Is ') + celltext)
if (celltext == ExpectedValue) {
found = true
println('Lead is present ' + Columns_row.get(10).getText())
break Loop;
}
}
}
So far so good is what I was looking for in the last post:
But the issue what I got is the next one:
If I input a value for String ExpectedValue = "test"; and this value is not existing in that table I got this Actual result:
So far so good because my loop does not stop until all elements are finding from that row.
But when I add the else statement
def v = WebUI.getAttribute(findTestObject('Object Repository/Page_LeadManager - Leads/input_Leads_searchInput'),'value')
//WebUI.click(findTestObject('Object Repository/Page_LeadManager - Leads/input_Modal Title_form-control'))
//WebUI.setText(findTestObject('verifiyelements/Page_LeadManager - Leads/input_Leads_searchInput'), address)
//WebUI.sendKeys(findTestObject('Object Repository/Page_LeadManager - Leads/input_Leads_searchInput'), Keys.chord(Keys.ENTER))
String ExpectedValue = v;
WebDriver driver = DriverFactory.getWebDriver()
//Expected value from Table
WebElement Table = driver.findElement(By.xpath('//*[@id="leadtable"]'));
//To locate table
rows_table = Table.findElements(By.tagName('/tr/th'));
// To locate rows of table it will Capture all the rows available in the table
List<WebElement> rows_table = Table.findElements(By.tagName('tr'));
// To calculate no of rows In table
int rows_count = rows_table.size();
println("rows_count=${rows_count}");
// Loop will execute for all the rows of the table
Loop:
for (int row = 0; row < rows_count; row++) {
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName('td'));
int columns_count = Columns_row.size();
println((('Number of cells In Row ' + row) + ' are ') + columns_count);
boolean found = false;
for (int column = 0; column < columns_count; column++) {
celltext = Columns_row.get(column).getText()
println((((('Cell Value Of row number ' + row) + ' and column number ') + column) + ' Is ') + celltext);
if (celltext == ExpectedValue) {
found = true
println('Lead is present ' + Columns_row.get(10).getText())
}
else {
println('Lead is not added')
break Loop;
}
}
}
And the lead is added but the console returns me the Lead is not added and here I am stuck. I was tried different methods from the previous post and I did not find any clue how to manage to work with else statement or to fail if the lead is really not added.
And here a strange thing or I don’t understand why this is the good result:
String ExpectedValue = "test";
WebDriver driver = DriverFactory.getWebDriver()
//Expected value from Table
WebElement Table = driver.findElement(By.xpath('//*[@id="leadtable"]'));
//To locate table
rows_table = Table.findElements(By.tagName('/tr/th'));
// To locate rows of table it will Capture all the rows available in the table
List<WebElement> rows_table = Table.findElements(By.tagName('tr'));
// To calculate no of rows In table
int rows_count = rows_table.size();
println("rows_count=${rows_count}");
// Loop will execute for all the rows of the table
Loop:
for (int row = 0; row < rows_count; row++) {
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName('td'));
int columns_count = Columns_row.size();
println((('Number of cells In Row ' + row) + ' are ') + columns_count);
boolean found = false
for (int column = 0; column < columns_count; column++) {
//Here I add value 10 in the row.get()
celltext = Columns_row.get(10).getText()
println((((('Cell Value Of row number ' + row) + ' and column number ') + column) + ' Is ') + celltext)
if (celltext == ExpectedValue) {
found = true
println('Lead is present ' + Columns_row.get(10).getText())
}
else {
println('Lead is not added')
break Loop;
}
}
}
@costea.georgian89 I have amended your script some. The “locate table” by ‘/tr/th’ is not needed at all because the next row down you search by ‘tr’. You also should not break out of the inner loop like you are in the else clause because you want to find your match first and then break out of the whole loop. I even removed the inner loop completely because you didn’t need it since you were already testing for one specific cell. Where I have a concern is the leadtable id. Is that associated with the body of the table? So, review and see what you think:
def ExpectedValue = WebUI.getAttribute(findTestObject('Page_LeadManager - Leads/input_Leads_searchInput'),'value')
WebDriver driver = DriverFactory.getWebDriver()
WebUI.delay(2)
//Expected value from Table
WebElement Table = driver.findElement(By.xpath('id("leadtable")/tbody'));
// To locate rows of table it will Capture all the rows available in the table
List<WebElement> rows_table = Table.findElements(By.tagName('tr'));
boolean found = false;
// To calculate no of rows In table
int rows_count = rows_table.size();
println("rows_count=${rows_count}");
// Loop will execute for all the rows of the table
Loop:
for (int row = 0; row < rows_count; row++) {
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName('td'));
int columns_count = Columns_row.size();
println('Number of cells In Row ' + row + ' are ' + columns_count);
'Loop will execute till the last cell of that specific row'
for (int column = 0; column < columns_count; column++) {
'It will retrieve text from each cell'
String celltext = Columns_row.get(column).getText()
println('Cell Value Of row number ' + row + ' and column ' + column) + ' is ' + celltext)
if (celltext == ExpectedValue) {
found = true;
println('Lead is present ' + Columns_row.get(10).getText());
break Loop;
}
}
}
if (!found) {
KeywordUtil.markFailedAndStop('Lead is not added')
}
@costea.georgian89 The else will get run EVERYTIME the if condition does not match, so you will break out of the loop on the first column, unless the the match occurs in the first column. If the match occurs in the first column, then the loop will break on the second column. Is that what you want?
Personally, you have the boolean found, so let’s use it. I have amended the code above by adding a check after looping through the rows, like below:
if (!found) {
KeywordUtil.markFailedAndStop('Lead is not added')
}
@grylion54
No I don’t want to break out of the loop on the first column only even if the specific term is not match.
And that was the only idea I got with else and KeywordUtil.markFailedAndStop.
L.E: I saw the bottom of the first replay now sorry
Count the columns in the web table starting from zero (0). So the first column is zero, the second column is one, etc. With the above, we are wanting the text in the eleventh column. Is that correct?
Okay, so the log also says we have zero (0) columns in the rows. So we need to fix that too.
@grylion54
I count from 0 to 10 and when I check where is locate(First Image) the element is returned to me like:
Cell Value of row number 1 and column number is test1312321312@itest.com
@grylion54
Yes, the first row is where the title of the column is writs and the second is where the data is returned because I use a search field to don’t have all the rows from the table.
//Here I want to check the value in the eleventh column
celltext = Columns_row.get(10).getText();
So it becomes:
//Here I want to check the value in the first column
celltext = Columns_row.get(0).getText();
println('Cell Value Of row number ' + row + ' and column 1 is ' + celltext)
//Here I want to check the value in the second column
celltext = Columns_row.get(1).getText();
println('Cell Value Of row number ' + row + ' and column 2 is ' + celltext)
and in the console it return only 2 column but is wrong because my table got more I know I said early but according with that katalon prin here: int columns_count = Columns_row.size(); println("columns_count=${columns_count}")
it return: