Currently I am working on a TC where I have to iterate over a table and find an object with a certain ID and click in one of its buttons this object has in the row of the table. This table is filled with objects I can add to my system. The problem with this are the next things:
- I can have more than one row with the same expected value in the table and I want to click the last one
- If I have more than one, it will try to click the first it finds and enter the page, ignore the break I use and keep searching. This will end up in a failed TC.
For 2) I tried using a break but for some reason it does not work. Regardless of this, how could I make it to search for the last one?
This is the code I use to search in the table:
‘Expected value from Table’
String ExpectedValue = ‘CI: 47242222’
WebDriver driver = DriverFactory.getWebDriver()
'To locate table'
WebElement Table = driver.findElement(By.id('GridContainerDiv'))
'To locate rows of table it will Capture all the rows available in the table '
List<WebElement> Rows = Table.findElements(By.tagName('tr'))
println('No. of rows: ' + Rows.size())
'Find a matching text in a table and performing action'
'Loop will execute for all the rows of the table'
table: for (int i = 0; i < Rows.size(); i++) {
'To locate columns(cells) of that specific row'
List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))
println(Rows.get(i).getText())
for (int j = 0; j < Cols.size(); j++) {
println(Cols.get(i).getText())
'Verifying the expected text in the each cell'
if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {
WebUI.delay(5)
TestObject to = new TestObject()
String objectXpath = '//img[@alt=\'Ver Resultado\']'
to.addProperty('xpath', ConditionType.EQUALS, objectXpath)
WebUI.waitForElementVisible(to, 30)
//WebUI.click(to)
WebUI.delay(3)
CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(to, 30)
//Cols.get(0).findElement(By.tagName('img')).click()
break
}
}
}
I tried clicking the last element of the Cols list but it does not work cause it says its null so what I am trying to accomplish now is to add the rows with the expected value in a new list and click the last one. The problem with this is that it says the value I add is null which is not.
I try to do something as simple as this:
(…code…)
for (int j = 0; j < Cols.size(); j++) {
println(Cols.get(j).getText())
if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {
Aux.add(Cols.get(j))
(…code…)
}
}
Whenever I try to do Aux.add(…) it throws something like this:
java.lang.NullPointerException: Cannot invoke method add() on null object
at Validacion(Test).run(Validacion(Test):92)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1565097759227.run(TempTestCase1565097759227.groovy:21)
Do you have any suggestion? I am new to Katalon studio and I could really need your help