Mobile Automation – How to capture text for multiple elements via list

In Mobile Automation, I tried capturing text for multiple elements using get text and assigned it to array list, but it is not working. So is there any other way for capturing multiple elements text in one shot. Below is the code for reference

Arraylist<String>Menu = Mobile.getText(findTestObject(‘path of items list’), 0)

for (String i : Menu) {
if (i.equals(‘Profile’)) {
Mobile.tap(findTestObject(‘Mobile/Profile icon’), 0)
}
}

Hi

As you suggested, I changed this
List ls = driver.findElementsByClassName(‘android.widget.TextView’)
to
ArrayList ls = driver.findElementsByClassName(‘android.widget.TextView’)
Now the list size is displayed correctly but again error on tapping the element
Code :
try {
AppiumDriver<?> driver = MobileDriverFactory.getDriver()

ArrayList ls = driver.findElementsByClassName(“android.widget.TextView”)

println(ls.size())

System.out.println(ls.size())

for (int index = 1; index < (ls.size() - 5); index++) {
if (index == 5) {
Mobile.tap(findTestObject(ls.get(index)),6)
break
}
}
}
catch (Exception e) {
e.printStackTrace()
}
Error:
07-26-2017 11:03:25 AM - [END] - End action : Statement - ls = driver.findElementsByClassName(“android.widget.TextView”)
07-26-2017 11:03:25 AM - [START] - Start action : Statement - println(ls.size())
[36minfo[39m: [37m<-- POST /wd/hub/session/ac2001c2-e96f-4ad2-9fb2-50e00da39281/elements [39m[32m200[39m[90m 780.500 ms - 271[39m [90m{“status”:0,“value”:[{“ELEMENT”:“4”},{“ELEMENT”:“5”},{“ELEMENT”:“6”},{“ELEMENT”:“7”},{“ELEMENT”:“8”},{“ELEMENT”:“9”},{“ELEMENT”:“10”},{“ELEMENT”:“11”},{“ELEMENT”:“12”},{“ELEMENT”:“13”},{“ELEMENT”:“14”},{“ELEMENT”:“15”}],“sessionId”:“ac2001c2-e96f-4ad2-9fb2-50e00da39281”}[39m
12
07-26-2017 11:03:25 AM - [END] - End action : Statement - println(ls.size())
07-26-2017 11:03:25 AM - [START] - Start action : Statement - out.println(ls.size())
12
07-26-2017 11:03:25 AM - [END] - End action : Statement - out.println(ls.size())
07-26-2017 11:03:25 AM - [START] - Start action : Statement - For ([index = 1, index < ls.size() - 5, (index++)])
07-26-2017 11:03:25 AM - [START] - Start action : Statement - If (index == 5)
07-26-2017 11:03:25 AM - [END] - End action : Statement - If (index == 5)
07-26-2017 11:03:25 AM - [START] - Start action : Statement - If (index == 5)
07-26-2017 11:03:25 AM - [END] - End action : Statement - If (index == 5)
07-26-2017 11:03:25 AM - [START] - Start action : Statement - If (index == 5)
07-26-2017 11:03:25 AM - [END] - End action : Statement - If (index == 5)
07-26-2017 11:03:25 AM - [START] - Start action : Statement - If (index == 5)
07-26-2017 11:03:25 AM - [END] - End action : Statement - If (index == 5)
07-26-2017 11:03:25 AM - [START] - Start action : Statement - If (index == 5)
07-26-2017 11:03:25 AM - [START] - Start action : tap
07-26-2017 11:03:25 AM - [END] - End action : tap
07-26-2017 11:03:25 AM - [END] - End action : Statement - If (index == 5)
07-26-2017 11:03:25 AM - [END] - End action : Statement - For ([index = 1, index < ls.size() - 5, (index++)])
07-26-2017 11:03:25 AM - [END] - End action : Statement - Try
07-26-2017 11:03:25 AM - [START] - Start action : Statement - Catch (Exception e)
07-26-2017 11:03:25 AM - [START] - Start action : Statement - e.printStackTrace()
07-26-2017 11:03:25 AM - [END] - End action : Statement - e.printStackTrace()
07-26-2017 11:03:25 AM - [END] - End action : Statement - Catch (Exception e)
07-26-2017 11:03:25 AM - [PASSED] - Test Cases/LandingPage
07-26-2017 11:03:25 AM - [END] - End Test Case : Test Cases/LandingPage
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.testobject.ObjectRepository.findTestObject() is applicable for argument types: (io.appium.java_client.android.AndroidElement) values: [[[SwipeableAndroidDriver: Android on ANDROID (ac2001c2-e96f-4ad2-9fb2-50e00da39281)] -> class name: android.widget.TextView]]
Possible solutions: findTestObject(java.lang.String), findTestObject(java.lang.String, java.util.Map)
at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1503)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1489)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.callStatic(StaticMetaClassSite.java:65)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:206)
at Script1500464198678.run(Script1500464198678.groovy:64)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:180)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:106)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:213)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:204)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:183)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:175)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:158)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.groovy:102)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141)
at TempTestCase1501047084814.run(TempTestCase1501047084814.groovy:21)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)

Thanks for the quick response.

I used the same code that you suggested but it is always showing list size is ‘0’(zero)

Code :

try {
AppiumDriver<?> driver = MobileDriverFactory.getDriver()

List ls = driver.findElementsByClassName(‘android.widget.TextView’)

println(ls.getSize())

System.out.println(ls.getSize())

for (int index = 0; index < (ls.getSize() - 5); index++) {
if (index == 2) {
Mobile.tap(driver.findElementsByClassName(‘android.widget.TextView’).get(index))
}
}
}
catch (Exception e) {
e.printStackTrace()
}

Below is the error that displayed on the Console :

Cannot cast object ‘[[[SwipeableAndroidDriver: Android on ANDROID (219546f6-8d1d-4b1c-810f-350a148f8d6c)] -> class name: android.widget.TextView], [[SwipeableAndroidDriver: Android on ANDROID (219546f6-8d1d-4b1c-810f-350a148f8d6c)] -> class name: android.widget.TextView], [[SwipeableAndroidDriver: Android on ANDROID (219546f6-8d1d-4b1c-810f-350a148f8d6c)] -> class name: android.widget.TextView], [[SwipeableAndroidDriver: Android on ANDROID (219546f6-8d1d-4b1c-810f-350a148f8d6c)] -> class name: android.widget.TextView], [[SwipeableAndroidDriver: Android on ANDROID (219546f6-8d1d-4b1c-810f-350a148f8d6c)] -> class name: android.widget.TextView], [[SwipeableAndroidDriver: Android on ANDROID (219546f6-8d1d-4b1c-810f-350a148f8d6c)] -> class name: android.widget.TextView], [[SwipeableAndroidDriver: Android on ANDROID (219546f6-8d1d-4b1c-810f-350a148f8d6c)] -> class name: android.widget.TextView], [[SwipeableAndroidDriver: Android on ANDROID (219546f6-8d1d-4b1c-810f-350a148f8d6c)] -> class name: android.widget.TextView], [[SwipeableAndroidDriver: Android on ANDROID (219546f6-8d1d-4b1c-810f-350a148f8d6c)] -> class name: android.widget.TextView]]’ with class ‘java.util.ArrayList’ to class ‘java.awt.List’ due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.awt.List(io.appium.java_client.android.AndroidElement, io.appium.java_client.android.AndroidElement, io.appium.java_client.android.AndroidElement, io.appium.java_client.android.AndroidElement, io.appium.java_client.android.AndroidElement, io.appium.java_client.android.AndroidElement, io.appium.java_client.android.AndroidElement, io.appium.java_client.android.AndroidElement, io.appium.java_client.android.AndroidElement)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:403)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
at Script1500464198678.run(Script1500464198678.groovy:56)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:180)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:106)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:213)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:204)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:183)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:175)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:158)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.groovy:102)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141)
at TempTestCase1500963307280.run(TempTestCase1500963307280.groovy:21)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)

Please suggest on this.

I have to click on the every element of the list

Please try with this instead:

**import** com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory

**import** io.appium.java_client.AppiumDriver

**import** io.appium.java_client.MobileElement

AppiumDriver<?> driver = MobileDriverFactory._getDriver_()

List<MobileElement> ls = driver.findElementsByClassName(".android.widget.TextView")

Hi

Please help on this.

I need to collect the same type element in an array list. I tried many ways but failed to get the elements in the list

I tried below code:

List ls = AppiumDriverManager.getDriver().findElementsByClassName(".android.widget.TextView");

Code is not giving any error and even not providing the valid output.

Hi There, I tried the following code for capturing all menu items but it’s not working. Is there is any other way for capturing all the menu items in to list in Katalon
List ls = AppiumDriverManager.getDriver().findElementsByXPath("//android.widget.ListView[@resource-id=‘com.kisangates.dairy:id/lv_drawer_menu’]")
println(ls.size())

for (int i = 0; i < ls.size; i++) {
println(ls.get(i).getText())

}