if element not found skip to next iteration else continue statement


#1

I need help in katalon studio case script for if else statement. If the element ‘Page_Quick Inbound/input_Bad_quantity’ is not found then it should skip the current iteration and continue with the next iteration. 15th line in the code I have tried the if statement but it is not working.

for (def row = 1; row <= findTestData('Ship Plan Data').getRowNumbers(); row++)     {
                 WebUI.delay(2)
                 WebUI.setText(findTestObject('Page_Quick Inbound/input_Scan or type SKU_itemId'), 
                 findTestData('Ship Plan Data').getValue('fnsku', row))                 rb.keyPress(KeyEvent.VK_ENTER)
                 WebUI.delay(1)
                 rb.keyRelease(KeyEvent.VK_ENTER)
                 WebUI.delay(2)        if (WebUI.verifyElementNotpresent(findTestObject('Page_Quick Inbound/input_Bad_quantity',10,FailureHandling.OPTIONAL) ))
                        {continue} 
        else{
             WebUI.setText(findTestObject('Page_Quick Inbound/input_Bad_quantity'), findTestData('Ship Plan Data').getValue('Quantity',
                                row))
              rb.keyPress(KeyEvent.VK_ENTER)     
              WebUI.delay(2)
              rb.keyRelease(KeyEvent.VK_ENTER)
              WebUI.delay(3)              WebUI.setText(findTestObject('Page_Quick Inbound/input_(You can select bin from'), findTestData('Ship Plan Data').getValue(
                                'bin', row))              rb.keyPress(KeyEvent.VK_ENTER)
              WebUI.delay(2)
              rb.keyRelease(KeyEvent.VK_ENTER)
              WebUI.delay(2)              WebUI.click(findTestObject('Page_Quick Inbound/button_RECEIVE  STORE'))
             }
    } 

#2

You are using WebUI.verifyElementNotPresent(). Calling verifyElementNotPresent makes your script fragile in case that the target web pages responds slowly. If the response is slow (e.g. 3 second), your call to WebUI.verifyElementNotPresent() would quickly return true. You should use WebUI.verifyElementNotPresent() only when you are 100% sure that the element will be found in the page at the first iteration and you expect it to disappear after a few iteration. I suppose this scenario is not what you have in mind.

Your problem can be restated:

Wait for the element ‘Page_Quick Inbound/input_Bad_quantity’ to be found; If it is found then the script do something … , otherwise the script skip the current iteration and continue with the next iteration.

You can implement it using WebUI.verifyElementPresent(). Your script would be as such:

....def timeout = 1if (WebUI.verifyElementPresent(        findTestObject('Page_Quick Inbound/input_Bad_quantity',        timeout, FailureHandling.OPTIONAL) )) {    WebUI.setText(        findTestObject('Page_Quick Inbound/input_Bad_quantity'),        findTestData('Ship Plan Data').getValue('Quantity', row))    ...} else {    continue}....

Possibly you would prefer shorter timeout, for example 1 sec, to longer timeout, for example 10 secs.


#3

:o


#4

Plus,

findTestObject('Page_Quick Inbound/input_Bad_quantity',10,FailureHandling.OPTIONAL)accepts only string (test object id/path) as parameter. 

#5

@Mate Mrse and @4280-kazurayam Thanks been trying all possibilities for last 48 hrs. I am not a coder but putting my sincere effort to get this automated. Did try the below as said and got error. Let me know what to be changed.

for (def row = 1; row <= findTestData('Ship Plan Data').getRowNumbers(); row++) 
{
    WebUI.delay(2)
    WebUI.setText(findTestObject('Page_Quick Inbound/input_Scan or type SKU_itemId'),      findTestData('Ship Plan Data').getValue(
    'fnsku', row))
    rb.keyPress(KeyEvent.VK_ENTER)
    WebUI.delay(1)
    rb.keyRelease(KeyEvent.VK_ENTER)
    WebUI.delay(4)
   
def timeout = 3
 if (WebUI.verifyElementPresent(
findTestObject('Page_Quick Inbound/input_Bad_quantity',timeout, FailureHandling.OPTIONAL) )) 
{
      WebUI.setText(findTestObject('Page_Quick Inbound/input_Bad_quantity'), findTestData('Ship Plan Data').getValue('Quantity', 
                row))
        rb.keyPress(KeyEvent.VK_ENTER)
        WebUI.delay(2)
        rb.keyRelease(KeyEvent.VK_ENTER)
        WebUI.delay(3)
        WebUI.setText(findTestObject('Page_Quick Inbound/input_(You can select bin from'), findTestData('Ship Plan Data').getValue(
                'bin', row))
        rb.keyPress(KeyEvent.VK_ENTER)
        WebUI.delay(2)
        rb.keyRelease(KeyEvent.VK_ENTER)
        WebUI.delay(2)
        WebUI.click(findTestObject('Page_Quick Inbound/button_RECEIVE  STORE'))
    } else 
{
continue
}
}

Test Cases/shipplancase2 FAILED because (of) (Stack trace: groovy.lang.MissingMethodException: No signature of method: Script1536501262795.findTestObject() is applicable for argument types: (java.lang.String, java.lang.Integer, com.kms.katalon.core.model.FailureHandling) values: [Page_Quick Inbound/input_Bad_quantity, 3, OPTIONAL]


#6

timeout (int) is not part of the parms for findTestObject, but for WebUI.verifiyElementPresent.

Type Name and description
static [TestObject](https://api-docs.katalon.com/studio/v4.6.0.2/api/com/kms/katalon/core/testobject/TestObject.html) **[findTestObject](https://api-docs.katalon.com/studio/v4.6.0.2/api/com/kms/katalon/core/testobject/ObjectRepository.html#findTestObject(java.lang.String))**([String](http://download.oracle.com/javase/6/docs/api/java/lang/String.html "Link: http://download.oracle.com/javase/6/docs/api/java/lang/String.html") testObjectRelativeId)
Finds TestObject by its id or relative id
static [TestObject](https://api-docs.katalon.com/studio/v4.6.0.2/api/com/kms/katalon/core/testobject/TestObject.html) **[findTestObject](https://api-docs.katalon.com/studio/v4.6.0.2/api/com/kms/katalon/core/testobject/ObjectRepository.html#findTestObject(java.lang.String,%20Map%3CObject,%20Object%3E))**([String](http://download.oracle.com/javase/6/docs/api/java/lang/String.html) testObjectRelativeId, [Map](http://download.oracle.com/javase/6/docs/api/java/util/Map.html)<[Object](http://download.oracle.com/javase/6/docs/api/java/lang/Object.html), [Object](http://download.oracle.com/javase/6/docs/api/java/lang/Object.html)> variables)

Finds TestObject by its id or relative id using a variables map to parameterized its properties values
|

static boolean verifyElementPresent(TestObject to, int timeOut, FailureHandling flowControl)
Verify if the given web element presents on the DOM

so

if (WebUI.verifyElementPresent(
findTestObject('Page_Quick Inbound/input_Bad_quantity',timeout, FailureHandling.OPTIONAL) )) 
{rather should beif (WebUI.verifyElementPresent(
findTestObject('Page_Quick Inbound/input_Bad_quantity') ,timeout, FailureHandling.OPTIONAL) ) 
{


#7

Did the above still it never goes to else part. hence tried this to find the element visible/clickable/present

def clickd = WebUI.verifyElementPresent(findTestObject('Page_Quick Inbound/input_Bad_quantity'))Println(clickd)

this does not prints the result


#8

Println should be println
and if that is just your typo error, can you please show some partly console log output


#9

vinod,

Could you take a screenshot of the TestObject definition of ‘Page_Quick Inbound/input_Bad_quantity’?

And also we need to see HTML fragment around the element which ‘Page_Quick Inbound/input_Bad_quantity’ is targeted to.


#10

@4280-kazurayam & @Gerard van der Winkel Thanks I had achieved it using finding text in element ,

WebUI.navigateToUrl('https://www.amazon.in/')
WebUI.setText(findTestObject('Object Repository/Page_Amazon Sign In/input_Email or mobile phone nu'), 'ABC@XYZ.COM')
WebUI.setEncryptedText(findTestObject('Object Repository/Page_Amazon Sign In/input_Forgot Password_password'), 
    'ABC')
WebUI.click(findTestObject('Object Repository/Page_Amazon Sign In/input_Enter your password_reme'))
WebUI.click(findTestObject('Object Repository/Page_Amazon Sign In/input_Enter your password_sign'))
WebUI.click(findTestObject('Object Repository/Page_Two-Step Verification/span_Text me at my number endi'))
WebUI.click(findTestObject('Object Repository/Page_Two-Step Verification/input_Call me at my number end'))
WebUI.delay(15)
WebUI.click(findTestObject('Object Repository/Page_Orders/i_Orders_was-inventory-icon'))
WebUI.click(findTestObject('Object Repository/Page_Orders/i_Load More_was-receive-and-st'))
WebUI.setText(findTestObject('Page_Quick Inbound/input_Scan or Search Shipment'), '121W22HM')
Robot rb = new Robot()
rb.keyPress(KeyEvent.VK_ENTER)
WebUI.delay(1)
rb.keyRelease(KeyEvent.VK_ENTER)
WebUI.delay(5)
for (def row = 1; row <= findTestData('Ship Plan Data').getRowNumbers(); row++) {
    WebUI.waitForElementPresent(findTestObject('Page_Quick Inbound/input_Scan or type SKU_itemId'), 10)
WebUI.delay(4)
WebUI.setText(findTestObject('Page_Quick Inbound/input_Scan or type SKU_itemId'), findTestData('Ship Plan Data').getValue('fnsku', row))
    rb.keyPress(KeyEvent.VK_ENTER)
    WebUI.delay(1)
    rb.keyRelease(KeyEvent.VK_ENTER)
    WebUI.delay(4)
    if (WebUI.verifyElementText(findTestObject('Object Repository/Page_Quick Inbound/div_Please update the dimensio'), 'Please update the dimensions of the product and try again', 
        FailureHandling.CONTINUE_ON_FAILURE) == true) {
        continue
    } else {
        WebUI.waitForElementPresent(findTestObject('Page_Quick Inbound/input_Bad_quantity'), 20)
        WebUI.delay(4)
        WebUI.setText(findTestObject('Page_Quick Inbound/input_Bad_quantity'), findTestData('Ship Plan Data').getValue('Quantity', 
                row))
        WebUI.delay(1)
        rb.keyPress(KeyEvent.VK_ENTER)
        WebUI.delay(2)
        rb.keyRelease(KeyEvent.VK_ENTER)
        WebUI.delay(5)
        WebUI.setText(findTestObject('Page_Quick Inbound/input_(You can select bin from'), findTestData('Ship Plan Data').getValue(
                'bin', row))
        rb.keyPress(KeyEvent.VK_ENTER)
        WebUI.delay(2)
        rb.keyRelease(KeyEvent.VK_ENTER)
        WebUI.delay(2)
        WebUI.click(findTestObject('Page_Quick Inbound/button_RECEIVE  STORE'))
    }
}

Now I need to exit for loop if the page title is matched for that I have tried the below code

title = WebUI.getWindowTitle()
WebUI.verifyMatch(title, 'Amazon Sign In', true)

plan to put this below the for statement and don’t know how to exit the for loop but it should continue from the iteration where it left. This is because this website throws me out and brings sign in page. Hence need to sign in again and continue iteration


#11

WebUI.verifyMatch() returns Boolean result

Therefore you can do something like this:

for (loop) {
    ....
    title = WebUI.getWindowTitle()
    def result = WebUI.verifyMatch(title, 'Amazon Sign In', true)
    if (result) {
       // break or continue
    } else {
       // break or continue
    }    ....
}

#12
 Sign In
 ....
 for ( loop) {
     title = WebUI.getWindowTitle()
     def result = WebUI.verifyMatch(title, 'Amazon Sign In', true)
 if (result) {
              // It should go to sigin in step and 
                 move to else part and it should continue                  the iteration where it left
 } else {
      ..... 
   }
   ....
}



#13

@4280-kazurayam so if the sign in page is found it should sign in and proceed with iteration code which is present in else part, can duplicate the signin part in title match condition but how to proceed in with sequence continuation


#14

kazurayam said:

vinod,

Could you take a screenshot of the TestObject definition of ‘Page_Quick Inbound/input_Bad_quantity’?

And also we need to see HTML fragment around the element which ‘Page_Quick Inbound/input_Bad_quantity’ is targeted to.

@4280-kazurayam hope this is the one which you had asked. the “0” box is the Page_Quick Inbound/input_Bad_quantity I guess all these are hidden and enabled at we proceed. It that the reason I was not successful with VerifyElementPresent. sharing you another screen shot where the qty box is not present.

azpage.jpg

azpage2.jpg


#15

vinod,

Thank you for providing screenshots. But I am afraid I am lost.

Your previsous comment makes me confused:

Now I need to exit for loop if the page title is matched for that I have tried the below code

title = WebUI.getWindowTitle()

WebUI.verifyMatch(title, ‘Amazon Sign In’, true)


  
plan to put this **below the for statement**

What do I mean?

I would see the following code sequence reasonable:

WebUI.navigateToUrl('https://www.amazon.in/')def title = WebUI.getWindowTitle()if (WebUI.verifyMatch(title, 'Amazon Sign In'), true) {    // login processing if necessary}...for (def row = 1; row <= findTestData('Ship Plan Data').getRowNumbers(); row++) {    // do something for each entry of the Ship Plan Data}

But the following code sequence seems to be a riddle for me:

WebUI.navigateToUrl('https://www.amazon.in/')...for (def row = 1; row <= findTestData('Ship Plan Data').getRowNumbers(); row++) {    def title = WebUI.getWindowTitle()    if (WebUI.verifyMatch(title, 'Amazon Sign In'), true) {        // login processing    }    // do something for each entry of the Ship Plan Data}

You said you “plan to put this below the for statement”, which would mean you want the latter code sequence… I am lost.


#16

After sign for the first time then ship plan iteration begins.The website throws a sign in page in suddenly hence wanted to sign-in again and then continue with iteration let’s say after 100 th ( indicativei) iteration it shows a sign in page,we need to sigin and then proceed with 101 record of iteration


#17

Then the latter script is what you want, isn’t it?

WebUI.navigateToUrl('https://www.amazon.in/')...for (def row = 1; row <= findTestData('Ship Plan Data').getRowNumbers(); row++) {    def title = WebUI.getWindowTitle()    if (WebUI.verifyMatch(title, 'Amazon Sign In'), true) {        // login processing if necessary    }    // do something for each entry of the Ship Plan Data}

#18

To whom it may concern,

In Katalon Studio 7.0.0, we have already fixed the bug that caused the verifyElementPresent() and verifyElementNotPresent() keywords to throw an exception instead of returning false when the verified elements do not exist.

Please first check out the release note and click here to download if you want to try version 7.0.0 (beta) in advance.

Happy Testing!

Jass