Verify content of a ViewGroup in Android

Hello,

I am looking to implement a method to verify the content of a ViewGroup in Android.
Let me set a context, I have the following structure in my screen:


ViewGroup
TextView1
TextView2
ViewGroup
TextView1
TextView2
ViewGroup
TextView1
TextView2

I would like to verify that on my screen, a ViewGroup exists which have TextView1 = “Test” and TextView2= “10” for example.
Does anyone has an idea on how to implement it?

Thanks

Hi @quentin,

If you’re always expecting the TextView1 and TextView2 objects to have the same text, then I would first use Katalon Studio’s Mobile Object Spy utility to capture the TextView1 and TextView2 objects on the screen:

Next, when you write your test, you can use Katalon’s verifyElementExist function to check that both the TextView1 and TextView2 elements show up on the screen:

Hope this helps,

Chris

Hi Chris,

Thanks for your answer. In fact with your solution I cannot guarantee that TextView 1 and 2 are on the same group. I will only be able to say that both text are present in the screen, but not on the same group.
Do you know a way to maybe check that parent object is the same?

Hi @quentin

If you’re trying to test that TextView1 and TextView2 both exist in the same ViewGroup, you could approach it in a couple of ways, both of which will rely on creating the Test Objects for TextView1 and TextView2 based on their xpaths:

  1. If you have a way of identifying the ViewGroup (resource-id, text, etc.) that will contain TextView1 and TextView2, you can create xpaths for TextView1 and TextView2 that include the parent ViewGroup.
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile

TestObject testObjectOne = new TestObject()
testObjectOne.addProperty("xpath", ConditionType.EQUALS, "//android.widget.ViewGroup[@resource-id='myViewGroupId']/*[@class='android.widget.TextView' and @text='Test']")

TestObject testObjectTwo = new TestObject()
testObjectTwo.addProperty("xpath", ConditionType.EQUALS, "//android.widget.ViewGroup[@resource-id='myViewGroupId']/*[@class='android.widget.TextView' and @text='10']")

Mobile.verifyElementExist(testObjectOne, 10)
Mobile.verifyElementExist(testObjectTwo, 10)

You can then verify that both objects exist (per the verifyElementExist function. If they don’t that means one or both of the TextViews are not in the appropriate ViewGroup and the test should fail.

  1. If you can’t identify the ViewGroup ahead of time, you could use the parent “axis” for xpaths:

https://www.w3schools.com/xml/xpath_axes.asp

You could then build a Test Object for each of TextView1 and TextView2’s parents and compare them to verify that it’s the same parent.

import org.openqa.selenium.remote.RemoteWebElement
import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory
import io.appium.java_client.AppiumDriver

AppiumDriver<?> driver = MobileDriverFactory.getDriver()

RemoteWebElement viewGroupOne = (RemoteWebElement) driver.findElementsByXPath("parent:://android.widget.TextView[@text='Test']").get(0)

RemoteWebElement viewGroupTwo = (RemoteWebElement) driver.findElementsByXPath("parent:://android.widget.TextView[@text='10']").get(0)

assert viewGroupOne.getAttribute('xpath') == viewGroupTwo.getAttribute('xpath')

Hope this helps,

Chris