How To use if Code

I am trying to get data from excel sheet in a xpath like id: = ‘id("P75_BWAREHOUSExpath ")’,
Try to use codes if there is any data available in the field then code will not take data from excel. But if the field empty then get data from my excel sheet. if any data found in field id: = ‘id("P75_BWAREHOUSExpath ")’, then it will ingore the data from second column of the sheet.
Hero.xlsx (8.0 KB)
Please help me.
I am using this codes:

def data = TestDataFactory.findTestData(“Data Files/SMART”)

WebUI.comment(“row count is ${data.getRowNumbers()}”)

for (row = 1; row <= data.getRowNumbers(); row++) {

def data = TestDataFactory.findTestData(“Data Files/JJJ”)

WebUI.comment(“row count is ${data.getRowNumbers()}”)

for (row = 1; row <= data.getRowNumbers(); row++) {

xpath = 'id("P75_SEARCH_LC")'

myItem = new TestObject(xpath)

myItem.addProperty('xpath', ConditionType.EQUALS, xpath)

WebUI.verifyElementVisible(myItem)

WebUI.setText(myItem, data.getValue(1, row))

   WebUI.click(findTestObject('Object Repository/Local LC amendment Helper/Page_Form on LOCAL_LC/a_Search'))

xpath = 'id("P75_BWAREHOUSE")'

myItem = new TestObject(xpath)

myItem.addProperty('xpath', ConditionType.EQUALS, xpath)

WebUI.verifyElementVisible(myItem)

WebUI.setText(myItem, data.getValue(2, row))

html.txt (56.7 KB)
My codes.txt (900 Bytes)

Sorry, your sentences are too difficult to understand as English. Your sentences don’t have SUBJECTS. It is not the way English sentences to be written. Please rephrase your question more gramatically correctly.

Also please use “Code Formatting” style when you show your codes in the posts for better readability.

1 Like

I am trying to paste value from excel to web. But there are one field where I have to use if condition so that if the field have any value then katalon will skip that field and for next one. But if the field have no value then it will take value from excel and paste it to the web field. @kazurayam

The code you posted above has no if (...) { ... } clause.

Do you want us, guys in this forum, to rewrite you code to have a if (....) { ... } so that it works as you want?

1 Like

I don’t know the use of if functions. That’s why I am asking for your help.

Start here:

after that read more on:
https://groovy-lang.org/semantics.html#_conditional_structures

and do not avoid:
https://docs.groovy-lang.org/latest/html/documentation/#_groovy_language_specification

So what you seem to want to do is check if the textbox, or textarea, has some content first before you insert text to the textbox, or textarea. For this, put an if statement just before your setText statement to check if the box has content, like below:

Maybe like:

As you can see, there are lots of ways you can form your code to check. You can try any of these to see if they match what you want. They should all do the same thing–make a condition that checks if there is anything in the field.

myItem = new TestObject(xpath)

myItem.addProperty('xpath', ConditionType.EQUALS, xpath)

WebUI.verifyElementVisible(myItem)

"if there is nothing in the box, then put something in"
if (WebUI.getAttribute(myItem, "value").length == 0) {
    WebUI.setText(myItem, data.getValue(1, row))
}

or

"if there is nothing in the box"
if (WebUI.getAttribute(myItem, "value") == "") {
    WebUI.setText(myItem, data.getValue(1, row))
}

or

"if there is 'not' something in the box (sort of the same thing as above)"
if (!WebUI.getAttribute(myItem, "value").length > 0) {
    WebUI.setText(myItem, data.getValue(1, row))
}

One thing I noticed is that you are putting the contents of the “JJJ” data file and the “SMART” data file to the same variable. That will not work if you want to use both. As you have it, only the “JJJ” data file will work as that is the last one to be assigned the variable. Assign the first one to a different variable, like smartData and then you can have both. Like below:

def smartData = TestDataFactory.findTestData("Data Files/SMART")

WebUI.comment("row count is ${smartData.getRowNumbers()}")

for (row = 1; row <= smartData.getRowNumbers(); row++) {

    def data = TestDataFactory.findTestData("Data Files/JJJ")

    WebUI.comment("row count is ${data.getRowNumbers()}"

Edit: As you currently have it, the first time through the loop, you would be using the number of rows of the SMART data file. The second time through the loop, I am not exactly certain what reference you would be using (due to scope of variable), but I would think it would be the JJJ data file. If the SMART data file and the JJJ data file are not the same size, then you will definitely have issues.