Saturday, September 9, 2023

Android Architectural Pattern - Databinding or 2 way Databinding.

' We don't use resource Id's for these. They are designed using a different architectural pattern called data binding and in some cases 2 way data binding.'

This is what my friendly app lead told me when I told him I'd like to have 'resource IDs' for some of the elements in the UI. He further said to look 'automated testing using Android dataBinding' and for any hook onto the databindingImpl classes. Very useful hints :).. thanks G

Android DataBinding

Android DataBinding is a library that allows you to bind UI components in your app's layout files directly to data sources (e.g., ViewModel).

It simplifies the process of updating UI elements when data changes and can improve code readability

Steps to identify and interact with elements

Assuming you are using appium/java. Also you have setup necesssary env setup such as mobile capabilties and driver initialisation , server startup, here is how the element capturing happens.

Since DataBinding allows you to bind UI elements directly to data sources, it is a bit different from the traditional method of locating elements using resource IDs or XPath
  • In Android DataBinding layouts, UI elements are bound to data sources using expressions like @{viewModel.text}.
  • To locate and interact with these elements in your automated tests, you can use the driver.findElementByAndroidUIAutomator method.
  • You construct a UiSelector expression that matches the text or other attributes bound to the element. 
  • In this case, I am looking for a TextView with the text matching viewModel.text.
    import io.appium.java_client.android.AndroidElement;
    
    // Locate TextView using DataBinding expression
    AndroidElement textView = driver.findElementByAndroidUIAutomator(
        "new UiSelector().textMatches(\".*" + viewModel.text + ".*\")"
    );
    

  • Once I have identified the element using DataBinding expression, I can perform various interactions such as clicking, sending text, or verifying properties. 
    // Click a button
    button.click();
    


Friday, September 8, 2023

How to scroll down in ScollView uiElement using Selenium WebDriver with Appium

A ScrollView is a common UI element in mobile app development, particularly in Android and iOS development. It's used to create a scrollable view that can contain content that doesn't fit entirely on the screen, allowing users to scroll vertically or horizontally to see more content.


Now let's see appium code for scroll ingto an element within a ScrollView in Android.
 public void scrollDownInScrollView(WebElement scrollview) {
        // Calculate screen dimensions
        int screenHeight = driver.manage().window().getSize().getHeight();

        // Define scroll points
        int startX = scrollview.getLocation().getX() + scrollview.getSize().getWidth() / 2;
        int startY = scrollview.getLocation().getY() + scrollview.getSize().getHeight() * 3 / 4;
        int endY = scrollview.getLocation().getY() + scrollview.getSize().getHeight() / 4;

        // Perform the scroll action
        TouchAction<?> action = new TouchAction<>((PerformsTouchActions) driver);
        action.press(PointOption.point(startX, startY))
                .waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
                .moveTo(PointOption.point(startX, endY))
                .release()
                .perform();
    }


Featured

Selenium - Page Object Model and Action Methods

  How we change this code to PageObjectModel and action classes. 1 2 3 driver . findElement ( By . id ( "userEmail" )). sendKeys (...

Popular Posts