# Test case fails because of problem with table

**URL:** <https://forum.katalon.com/t/test-case-fails-because-of-problem-with-table/30999>\
**Category:** Web Testing\
**Tags:** web-testing, katalon-studio\
**Created:** [August 6, 2019, 1:34pm UTC](https://forum.katalon.com/t/test-case-fails-because-of-problem-with-table/30999 "2019-08-06T13:34:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![guillenoble59](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/guillenoble59/32/16104_2.png) [@guillenoble59](https://forum.katalon.com/u/guillenoble59)\
**Post date:** [August 6, 2019, 1:34pm UTC](https://forum.katalon.com/t/test-case-fails-because-of-problem-with-table/30999/1 "2019-08-06T13:34:42Z")

</div>

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:

1. I can have more than one row with the same expected value in the table and I want to click the last one
2. 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

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [August 6, 2019, 1:40pm UTC](https://forum.katalon.com/t/test-case-fails-because-of-problem-with-table/30999/2 "2019-08-06T13:40:57Z")

</div>

Where and how is `Aux` defined?

---

<div class="post-metadata">

**Author:** ![guillenoble59](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/guillenoble59/32/16104_2.png) [@guillenoble59](https://forum.katalon.com/u/guillenoble59)\
**Post date:** [August 6, 2019, 1:53pm UTC](https://forum.katalon.com/t/test-case-fails-because-of-problem-with-table/30999/3 "2019-08-06T13:53:06Z")

</div>

Sorry for not adding that. Here it is the complete final code I am working with:  
‘Expected value from Table’  
String ExpectedValue = ‘CI: 47241111’

```
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'))
//List<WebElement> Aux; //<--- HERE IT IS
List<WebElement> Aux = new List<WebElement>();
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(j).getText())

        'Verifying the expected text in the each cell'
		if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {
			Aux.add(Cols.get(j))
			println("aqui " + Cols.get(j))
			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
	}
}

```

}

---

<div class="post-metadata">

**Author:** ![guillenoble59](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/guillenoble59/32/16104_2.png) [@guillenoble59](https://forum.katalon.com/u/guillenoble59)\
**Post date:** [August 6, 2019, 2:10pm UTC](https://forum.katalon.com/t/test-case-fails-because-of-problem-with-table/30999/4 "2019-08-06T14:10:08Z")

</div>

I think I now understand what it is happening.  
I needed to do sometihing like this:  
List Aux = new ArrayList();

---

<div class="post-metadata">

**Author:** ![albert.vu](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/albert.vu/32/20595_2.png) [@albert.vu](https://forum.katalon.com/u/albert.vu)\
**Post date:** [August 8, 2022, 7:43am UTC](https://forum.katalon.com/t/test-case-fails-because-of-problem-with-table/30999/5 "2022-08-08T07:43:01Z")

</div>

Archived on August 8, 2022, 7:43am UTC


