How can I set a text to every text box?

So I am new to katalon and what I am trying to accomplish is to set a a certain text in every text box of my table. I know that the text box tag name is an input and that its id has the next form:
//input[@id=‘vRESULTADOVALOR_0001’]
//input[@id=‘vRESULTADOVALOR_0002’]
//input[@id=‘vRESULTADOVALOR_0003’]
(…)
So I am trying to do something like this but it is not working:
@Keyword
def getDynamicObjects(){
int number = 1
String dynamicId = ‘vRESULTADOVALOR_000’ + number
String xpath = ‘//div[@id="’ + dynamicId + ‘"]’
System.out.println(xpath)
number = number+1
}

As a first step I am trying to print the dynamic id for every column in the table. Then, find every text box using that dynamic id and set a certain text
Like this:
WebDriver driver = DriverFactory.getWebDriver()
WebElement Table = driver.findElement(By.id(‘Grid1ContainerDiv’))
List Rows = Table.findElements(By.tagName(‘tr’))
table: for (int i = 0; i < Rows.size(); i++) {
List Cols = Rows.get(i).findElements(By.tagName(‘td’))
for (int j = 0; j < Cols.size(); j++) {
CustomKeywords.‘keywordPrueba.CustomFunction.getDynamicObjects’()
break
}
}

Maybe my approach is not the correct one maybe my code is not correct. I am new to this and I am having some difficulties. If you have any suggestions I would be really happy

Hi,
Every time you call this function, the number is set to 1. So you will never have vRESULTADOVALOR_0002

Try something like

def getDynamicObjects(int number){
String xpath = "//div[@id='vRESULTADOVALOR_000${number}']"
System.out.println(xpath)
}

Then

for (int i = 0; i < Rows.size(); i++) {
    List Cols = Rows.get(i).findElements(By.tagName('td'))
    for (int j = 0; j < Cols.size(); j++) {
        CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(j+1)
    }
}
1 Like

Thanks for the answer, I will try this and let you know if anything goes wrong

And fix the XPath to use //input instead of //div.

I see another helper function is needed to pad the correct number of leading zeros too. What happens when j+1 gives 10?

1 Like

That’s true.

So try

def getDynamicObjects(int number){
    String numbWithZero = String.format('%04d', number)
    String xpath = "//input[@id='vRESULTADOVALOR_${numbWithZero}']" 
    System.out.println(xpath) 
}
1 Like

Once again, thanks for the answer.
It is still throwing the same id all the time which it is this one vRESULTADOVALOR_0001
Am I missing something? Is there a better solution for what I want to do?

In a different TC I do something like this…When I was trying a different solution I was trying to accomplish something similar but there is not a “set text” option for that case:
WebElement Table = driver.findElement(By.id(‘GRIDSDTORDENMUESTRASSTABLEWITHPAGINATIONBAR’))
List Rows = Table.findElements(By.tagName(‘tr’))
table: for (int i = 0; i < Rows.size(); i++) {
List Cols = Rows.get(i).findElements(By.tagName(‘td’))
for (int j = 0; j < Cols.size(); j++) {
if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {
Cols.get(0).findElement(By.tagName(‘input’)).click()
break
}
}
}

Is there any other option or something else I can do?

Maybe you don’t need columns.
Try call the method on the row number :

WebDriver driver = DriverFactory.getWebDriver()
WebElement Table = driver.findElement(By.id('Grid1ContainerDiv'))
List Rows = Table.findElements(By.tagName('tr'))
for (int i = 0; i < Rows.size(); i++) {
    List Cols = Rows.get(i).findElements(By.tagName('td'))
    for (int j = 0; j < Cols.size(); j++) {
        CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(i+1)
    }
}

If it’s still doesn’t work, please share the html code of your table

2 Likes

Thank you very much, now it works ! For the next step I have to find each element on the table with that ID and then set the text. I will try to figure out how to do that and if anything goes wrong I will tell you.

1 Like

So I managed myself to enter a certain text in every empty text box of the table. This took me some time to realize how to do.
Now the problem that I am not able to resolve is the next one…
Whenever I write in every text box I have to do something like this:

WebDriver driver = DriverFactory.getWebDriver()
WebElement Table = driver.findElement(By.id('Grid1ContainerDiv'))
List<WebElement> Rows = Table.findElements(By.tagName('tr'))
table: for (int i = 0; i < Rows.size(); i++) {
    List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))
    for (int j = 0; j < Cols.size(); j++) {
		CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(i+1)
		TestObject toV = new TestObject()
		
		String objectXpathV = CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(i+1)
		
		toV.addProperty('xpath', ConditionType.EQUALS, objectXpathV)
		
		WebElement elementPrueba = WebUiCommonHelper.findWebElement(toV, 30)

		String textInsideInputBox = elementPrueba.getAttribute("value");
		if(textInsideInputBox.isEmpty()){
			CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(toV,30)
			WebUI.executeJavaScript('arguments[0].value=\'12\'', Arrays.asList(elementPrueba))
			}
		table: break
		}
    }

So the problem is that whenever it finishes setting every text box it continues with its search and I want to finish it. Clearly the break I have in my code is not working.
The error I have is like this:
Unable to find the element located by ‘By.xpath: //input[@id=‘vRESULTADOVALOR_0082’]’. Please recheck the objects properties to make sure the desired element is located.
Any ideas?

For example one of the things I am trying to do is to check if the element exists or not

WebDriver driver = DriverFactory.getWebDriver()
WebElement Table = driver.findElement(By.id('Grid1ContainerDiv'))
List<WebElement> Rows = Table.findElements(By.tagName('tr'))
table: for (int i = 0; i < Rows.size(); i++) {
    List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))
    for (int j = 0; j < Cols.size(); j++) {
		CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(i+1)
		TestObject toV = new TestObject()
		String objectXpathV = CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(i+1)
		toV.addProperty('xpath', ConditionType.EQUALS, objectXpathV)
		if(WebUI.verifyElementPresent(toV, FailureHandling.OPTIONAL)){
			WebElement elementPrueba = WebUiCommonHelper.findWebElement(toV, 30)
			String textInsideInputBox = elementPrueba.getAttribute("value");
			if(textInsideInputBox.isEmpty()){
				CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(toV,30)
				WebUI.executeJavaScript('arguments[0].value=\'12\'', Arrays.asList(elementPrueba))
				}
			}
		}
    } 

But this also fails

Add a verifyElementPresent verification

WebDriver driver = DriverFactory.getWebDriver()
WebElement Table = driver.findElement(By.id('Grid1ContainerDiv'))
List<WebElement> Rows = Table.findElements(By.tagName('tr'))
for (int i = 0; i < Rows.size(); i++) {
    List Cols = Rows.get(i).findElements(By.tagName('td'))

    for (int j = 0; j < Cols.size(); j++) {
        TestObject toV = new TestObject()
        String objectXpathV = CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(i + 1)
        toV.addProperty('xpath', ConditionType.EQUALS, objectXpathV)

        if (WebUI.verifyElementPresent(toV, 5, FailureHandling.OPTIONAL)) {
            WebElement elementPrueba = WebUiCommonHelper.findWebElement(toV, 30)
            String textInsideInputBox = elementPrueba.getAttribute('value')
            if (textInsideInputBox.isEmpty()) {
                CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(toV, 30)
                WebUI.executeJavaScript('arguments[0].value=\'12\'', Arrays.asList(elementPrueba))
            }
        }
    }
}

Edit for your second post

You only miss the timeout argument

Thank you for the reply.
Do I need to write something extra cause my code is till trying to find an object is not there.
Although I added a new condition it still fails

Event with the timeout ?
It’s the same error message ?

Yes, even with the time out.
5: if (verifyElementPresent(toV, 30, STOP_ON_FAILURE))
2019-08-22 11:55:52.360 INFO c.k.k.c.webui.common.WebUiCommonHelper - Unable to find the element located by ‘By.xpath: //input[@id=‘vRESULTADOVALOR_0082’]’. Please recheck the objects properties to make sure the desired element is located.

I also added an else statement to the if but keeps failing

It’s because of the FailureHandling. Set Optional
(and I think you can reduce the timout : 30 sec for one input seems a long time to wait)

1 Like

Hi,
I changed the time to 5 us you told me
I tried using optional and of course it continues but throwing its error every time it cannot find the object
I tried using STOP ON FAILURE and it gives me an error
I tried adding the break again and change it to different places to see what happens but it keeps searching
At this point I do not know what else to do

Does every cell in the table have an input element? Your code seems to expect it.

You shouldn’t need a break statement IF every cell is an input.

In the picture you can see the format of the table. As you can see the first space is blank but then that column has a textbox in every space.
This is like this because I have a certain object with sub objects and each one of this sub objects have the text box in the column "resultados’'.
This could change as I could have a table with more than one object and these with many sub objects.

TABLE:

This is the last code that I have:

WebDriver driver = DriverFactory.getWebDriver()
WebElement Table = driver.findElement(By.id('Grid1ContainerDiv'))
List<WebElement> Rows = Table.findElements(By.tagName('tr'))
table: for (int i = 0; i < Rows.size(); i++) {
    List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))
    for (int j = 0; j < Cols.size(); j++) {
		CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(i+1)
		TestObject toV = new TestObject()
		String objectXpathV = CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(i+1)
		toV.addProperty('xpath', ConditionType.EQUALS, objectXpathV)
		println("aqui estoy" + objectXpathV)
		if(WebUI.verifyElementPresent(toV, 5, FailureHandling.OPTIONAL)){
			WebElement elementPrueba = WebUiCommonHelper.findWebElement(toV, 5)
			String textInsideInputBox = elementPrueba.getAttribute("value");
			if(textInsideInputBox.isEmpty()){
				CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(toV,5)
				WebUI.executeJavaScript('arguments[0].value=\'12\'', Arrays.asList(elementPrueba))
				}
			}
		}
    }

I have optional in the if statement but I used stop on failure and tried many other things.

So the row count gives you the number of cells (possibly one too many). You only need one loop looking for the input elements which have unique IDs which you are constructing in code.

WebDriver driver = DriverFactory.getWebDriver()
WebElement Table = driver.findElement(By.id('Grid1ContainerDiv'))
List<WebElement> Rows = Table.findElements(By.tagName('tr'))
table: for (int i = 0; i < Rows.size(); i++) {
	println(Rows.size())
	CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(i+1)
	TestObject toV = new TestObject()
	String objectXpathV = CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'(i+1)
	toV.addProperty('xpath', ConditionType.EQUALS, objectXpathV)
	println("aqui estoy" + objectXpathV)
	//if(WebUI.verifyElementPresent(toV, 5, FailureHandling.STOP_ON_FAILURE)){
		WebElement elementPrueba = WebUiCommonHelper.findWebElement(toV, 5)
		String textInsideInputBox = elementPrueba.getAttribute("value");
		if(textInsideInputBox.isEmpty()){
			CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(toV,5)
			WebUI.executeJavaScript('arguments[0].value=\'12\'', Arrays.asList(elementPrueba))
			}
	//}
}

I have this code now and I have the next table is one “row” but the print is telling me it has 8. Why?