Looping can't stop, even the element found

Can anyone help me, i want to make my looping stop when find the object with verifyElementPresent, but when the object found, the looping still going and open new browser, but log in with another username, how to make the looping stop??, here is my script.

WebUI.openBrowser(‘’)

WebUI.navigateToUrl(‘https:testing.com’)

WebUI.maximizeWindow()

for (def rowNum = 1; rowNum <= findTestData(‘Login’).getRowNumbers(); rowNum++) {

WebUI.setText(findTestObject('Object Repository/Log_IN/Page_Testing/input_Sign In To Testing_username'), findTestData(
        'Login').getValue(1, rowNum))

WebUI.setText(findTestObject('Object Repository/Log_IN/Page_Testing/input_Sign In To Testing_password'), findTestData(
        'Login').getValue(2, rowNum))

WebUI.click(findTestObject('Object Repository/Log_IN/Page_Testing/button_Sign In'))

if (WebUI.verifyElementPresent(findTestObject('Object Repository/Log_IN/Page_Testing/Apuppt'), 3, FailureHandling.OPTIONAL))
	{
		WebUI.click(findTestObject('Object Repository/Log_IN/Page_Testing/Clickbutton'))
			
		break ;	
}

WebUI.delay(2)

}

@Mikha_M, instead of using a for loop to read the row in the file, you can import the data file, which can also read the value from the row. You can read Manage Test Data | Katalon Docs and Data-driven testing with Katalon Studio | Katalon Docs.

You can then use WebUI.verifyElementPresent to check.

2 Likes

A method to “break” out of your loop is to use the break command, like you have below. However, if you have multiple “levels” of if conditions and loops, then you need to use: break label (the label can be any word except reserved ones), like break Loop; (as below). Put the label above your for loop and then your loop will exit all “levels”.

i did like you say but the looping still going, it’s like the katalon read all my 4 row in file and looping it for 4 times :smiling_face_with_tear:

How about adding the above statement? It might be looping 4 times because the text you are looking for is not present yet as the page may be forming. You sometimes have to slow your script down to allow your web page to form and elements to appear.

And perhaps the below as well:

    WebUI.waitForElementVisible(findTestObject('Object Repository/Log_IN/Page_Testing/input_Sign In To Testing_username'))

    WebUI.setText(findTestObject('Object Repository/Log_IN/Page_Testing/input_Sign In To Testing_username'), findTestData( 'Login').getValue(1, rowNum))

    WebUI.setText(findTestObject('Object Repository/Log_IN/Page_Testing/input_Sign In To Testing_password'), findTestData('Login').getValue(2, rowNum))

    WebUI.click(findTestObject('Object Repository/Log_IN/Page_Testing/button_Sign In'))

    WebUI.waitForElementPresent(findTestObject('Object Repository/Log_IN/Page_Testing/Apuppt'), 10)

    if (WebUI.verifyElementPresent(findTestObject('Object Repository/Log_IN/Page_Testing/Apuppt'), 3, FailureHandling.OPTIONAL))
	{
		WebUI.click(findTestObject('Object Repository/Log_IN/Page_Testing/Clickbutton'))
			
		break Pizza;	  // here we call the label to break out of all loops
    }

1 Like

Probably a troll of a response, that I’m about to make…

…but I can’t help but question the veracity of this approach…

Should we really be brute-forcing every user login, just to find an element on the page (presumably corresponding to some data about that user)?

How’s come we don’t just log in, with some manager-level user, and look up that piece of data on the page (and who it applies to? For example, an upcoming appointment or member status…