9

I'm working with native iOS app through Appium.

I have the following structure:

UIAApplication ->
                  UIAWindow ->
                              UIATextBox
                              UIALabel
                  UIAWindow ->
                              SomethingElse

I have found a way to get the first UIAWindow and I'd like to get the list of all elements in that window. How do I do that?

I'd like to get UIATextBox and UIALabel from first UIAWindow but NOT SomethingElse element.

How do I do list of child elements in general?

  @Test
  public void testListWindows() {
      List<MobileElement> windows = driver.findElementsByClassName("UIAWindow");
      List<MobileElement> elements = windows.get(0).?????
  }
Artem
  • 7,275
  • 15
  • 57
  • 97

2 Answers2

8

You're almost there. What you have is giving you a list of all UIAWindows when what you want is a list of all the elements of the first UIAWindow. I'd suggest using Xpath with a wildcard.

This will get you every element that is a direct child of the first UIAWindow:

List<MobileElement> elements = driver.findElements(MobileBy.xpath("//UIAWindow[1]/*");

And this will get you every child and sub-child and sub-sub-child etc. of the first UIAWindow:

List<MobileElement> elements = driver.findElements(MobileBy.xpath("//UIAWindow[1]//*");

An extra tip: If you're automating for iOS, I assume that means you have access to OS X, where you can install the Appium dot app and use inspector. The inspector is a great tool to test out xpath queries before putting them into your code.

econoMichael
  • 657
  • 3
  • 13
  • Are you saying that List elements = windows.get(0).findElements(MobileBy.xpath("./*")); will never work? What if I need windows in other places? I will end up with duplication? – Artem Dec 15 '15 at 07:38
  • Thank you so much. working on me this code)) i havevery important question to you,. if i don't have a source code of application when i am tersting it with appium in java. if the app crashes in some point can i handle it? or i need to source code to see the name of exception ? example: i am clicking the button it throws nullpointerexception but i can't handle it in appium java how to do it? – Rashad Nasirli Feb 04 '21 at 07:01
2

You can also find textfield and label by its class for iOS app in Appium. findElementsByClassName method will return all elements on current screen that matches that class.

List<MobileElement> textFields = driver.findElementsByClassName("UIATextField");
MobileElement textField = textFields.get(0);

List<MobileElement> labels = driver.findElementsByClassName("UIAStaticText");
MobileElement label = labels.get(0);
Yogesh Solanki
  • 487
  • 5
  • 12