MobileElements

As we discussed in the previous sections, the locators for MobileElements are limited to ID, className, tagName, and XPath. That doesn't mean you cannot use other attributes in XPath queries when defining the locators. Let's take a look at a few MobileElements and define the XPath locators.

In the following screenshot of the ScratchTones native mobile iOS app, when we highlight the My Scratchtones button in the Appium Inspector, it displays the attributes in the Details frame:

Appium iOS mobile elements

We have the name, type, value, and label to use as attributes; also, notice that the user is given a generic XPath locator to use. The problem with the generic locators is that they include too much hierarchy in the locator. Let's build a couple of single attribute XPath locators for this element:

@iOSFindBy(xpath = "//UIAButton[@name='My ScratchTones']")
protected M myScratchTones;

or

@iOSFindBy(xpath = "//UIAButton[@label='My ScratchTones']")
protected M myScratchTones;

or

@iOSFindBy(xpath = "//*[@value='1']")
protected M myScratchTones;

or

@iOSFindBy(xpath = "//UIATabBar[1]/UIAButton[1]")
protected M myScratchTones;

In the first and second locators, the class was used along with the name and label attributes. The class is not required if the locator is unique using just the attribute, so the third example is sufficient when wildcarding it. The third example is less robust using the value provided, and the most generic locator is the fourth example. When there are no unique attributes to use, users must use the class and an index number if there are multiple ones on the page (one-based numbering).

In the following screenshot, when we highlight the first StaticText field, it displays the attributes in the Details frame:

Appium iOS mobile elements

When StaticText is the only real attribute, we have to build the locator. We can use a partial string match in the XPath query:

@iOSFindBy(xpath = "//UIAStaticText[starts-with(@name,'1. Connect your device')]")

@iOSFindBy(xpath = "//UIAStaticText[contains(@name,'Connect your device')]")

@iOSFindBy(xpath = "//UIAStaticText[ends-with(@name,'menubar.')]")

@iOSFindBy(xpath = "//UIAStaticText[contains(text(),'Connect your device')]")

@iOSFindBy(xpath = "//UIAStaticText[.='Connect your device to a computer, launch iTunes,...']")

In the third screenshot, when we highlight the first button in the recorder, it displays the attributes in the Details frame. However, a lot of the buttons do not have any text attributes associated with them, such as the Start button:

Appium iOS details frame

In this case, the user would most likely have to use class and index numbers for some of the buttons, as follows:

@iOSFindBy(xpath = "//UIAButton[@name='Start']")
protected M start;

@iOSFindBy(xpath = "//UIAButton[@name='record active']")
protected M recordTrack1;

@iOSFindBy(xpath = "//UIAButton[@name='off unselected']")
protected M offTrack1;

@iOSFindBy(xpath = "//UIAButton[@name='play unselected']")
protected M playTrack1;

@iOSFindBy(xpath = "//UIASlider[1]")
protected M balanceTrack1;

@iOSFindBy(xpath = "//UISlider[3]")
protected M volumeTrack1;

etc...
The ScratchTones iOS Mobile Music Recording Studio application is provided by Graphixware, LLC:                                                                                                                      

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.221.67