I am trying to check if element exist, but appium seems to ignore searching an element when we specified that it should be inside another element. For example:
driver.findElementByAccessibilityId("First element").findElementByAccessibilityId("Second element");
It should work somehow since there is option in Appium inspector called Locator where after selecting strategy and choosing search from selected element option it finds what I expect.
What is the proper way of finding such elements ?
You can use a xpath for the same. Sample xpath for the situation will be like :
driver.findElement(By.xpath("//*[@accessibility-id='FirstElement']//*[@accessibility-id='FirstElement']"))
The // between first element and second element indicates first element is the base element for start searching the second element. So it will search for the second element between the scope of the first element
Simple solution but not the cleanest below:
I am using TestNG framework which I suppose do not allow declarations such as
private WebElement element= driver.findElementByAccessibilityId("First element").findElementByAccessibilityId("Second element");
Simple solution but not the best is to declare only parents as:
@FindBy(name="Parent")
private WebElement ParentElement;
Then in test case for example:
ParentElement.findElement(By.name("childElement")).isDisplayed())
Put the first element as parent and second element as child -
MobileElement parentButtonList = (MobileElement)driver.findElementById(parentClassIdentifier);
List<MobileElement> childButtonList = parentButtonList.findElementsById(childClassIdentifier);