MobileBy Class Notes By ShariqSP

Understanding the MobileBy Class in Appium Testing

The MobileBy class in Appium provides a collection of mobile-specific locator strategies for identifying UI elements in Android and iOS applications. It extends the capabilities of Selenium's By class with additional locators tailored for mobile environments.

Role of MobileBy in Appium Testing

  • Mobile-Specific Locators:

    Enables the use of advanced strategies such as finding elements by accessibility ID, Android UI Automator expressions, and iOS predicates.

  • Cross-Platform Compatibility:

    Provides a unified way to write tests that work across both Android and iOS platforms using mobile-specific locators.

Key Methods of MobileBy

The MobileBy class offers several locator strategies, each tailored to specific use cases in mobile testing:

  • MobileBy.AccessibilityId(String id):

    Locates an element using its accessibility ID. This is a cross-platform strategy supported by both Android and iOS.

    
                  // Locate an element using accessibility ID
                  MobileElement element = driver.findElement(MobileBy.AccessibilityId("login_button"));
                  element.click();
                        
  • MobileBy.AndroidUIAutomator(String expression):

    Locates elements in Android using UI Automator syntax, allowing for advanced queries such as scrolling and filtering.

    
                  // Locate an element using Android UI Automator
                  MobileElement element = driver.findElement(MobileBy.AndroidUIAutomator(
                      "new UiSelector().text(\"Submit\")"
                  ));
                  element.click();
                        
  • MobileBy.iOSNsPredicate(String predicate):

    Locates elements in iOS using NSPredicate strings, offering flexibility to query elements based on properties and conditions.

    
                  // Locate an element using iOS NSPredicate
                  MobileElement element = driver.findElement(MobileBy.iOSNsPredicate("label == 'Submit'"));
                  element.click();
                        
  • MobileBy.iOSClassChain(String chain):

    Locates elements in iOS using Class Chain queries, which are efficient for identifying elements in complex view hierarchies.

    
                  // Locate an element using iOS Class Chain
                  MobileElement element = driver.findElement(MobileBy.iOSClassChain("**/XCUIElementTypeButton[`label == 'Submit'`]"));
                  element.click();
                        
  • MobileBy.xpath(String xpathExpression):

    Locates elements using XPath queries. While versatile, XPath should be used sparingly in mobile tests due to performance considerations.

    
                  // Locate an element using XPath
                  MobileElement element = driver.findElement(MobileBy.xpath("//android.widget.Button[@text='Submit']"));
                  element.click();
                        

Examples of Using MobileBy in Tests

Here's a comprehensive example demonstrating multiple MobileBy strategies in an Appium test:


              import io.appium.java_client.MobileElement;
              import io.appium.java_client.android.AndroidDriver;
              import io.appium.java_client.MobileBy;
              import org.openqa.selenium.remote.DesiredCapabilities;
              
              import java.net.URL;
              
              public class MobileByExample {
                  public static void main(String[] args) throws Exception {
                      DesiredCapabilities caps = new DesiredCapabilities();
                      caps.setCapability("platformName", "Android");
                      caps.setCapability("deviceName", "Pixel_4");
                      caps.setCapability("automationName", "UiAutomator2");
                      caps.setCapability("app", "/path/to/app.apk");
              
                      // Initialize AndroidDriver
                      AndroidDriver driver = new AndroidDriver<>(
                          new URL("http://127.0.0.1:4723/wd/hub"), caps
                      );
              
                      // Using MobileBy.AccessibilityId
                      MobileElement elementByAccessibility = driver.findElement(MobileBy.AccessibilityId("login_button"));
                      elementByAccessibility.click();
              
                      // Using MobileBy.AndroidUIAutomator
                      MobileElement elementByUIAutomator = driver.findElement(MobileBy.AndroidUIAutomator(
                          "new UiSelector().text(\"Submit\")"
                      ));
                      elementByUIAutomator.click();
              
                      driver.quit();
                  }
              }
                    

Best Practices for Using MobileBy

  • Prefer AccessibilityId for cross-platform consistency when possible.
  • Use platform-specific locators like AndroidUIAutomator or iOSNsPredicate only when necessary.
  • Minimize the use of XPath locators as they can be slow and brittle.
  • Validate locators in the respective mobile device's UI hierarchy viewer (e.g., Android Studio's Layout Inspector or Appium Inspector).