Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Sunday, September 10, 2023

How to Scroll Down to an Element in Selenium WebDriver with Appium

Let's say that you're automating a test scenario for a mobile application, and you need to interact with an element that is not initially visible on the screen. You need a way to scroll down until the element becomes visible, allowing you to perform actions on it.

To tackle this problem, you can create a reusable method that scrolls down to a specified element. This method will use the TouchAction class in Appium to simulate a touch-based scroll gesture until the element becomes visible. 

Here's a sample method in Java:

public void scrollDownToElement(WebElement element) {
    Dimension size = driver.manage().window().getSize();
    int startX = size.width / 2;
    int startY = (int) (size.height * 0.8);
    int endY = (int) (size.height * 0.2);

    while (!element.isDisplayed()) {
        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();
    }
}

Let's break down how this method works:
  1. We obtain the screen dimensions to calculate the starting and ending points for the scroll gesture. So we set startX to the middle of the screen, startY to 80% down from the top, and endY to 20% down from the top.
  2. We enter a while loop that continues until the specified element becomes visible.
  3. Inside the loop, we create a TouchAction instance and perform the following steps:
    • press: We press at the starting point.
    • waitAction: We introduce a small delay (500 milliseconds) to allow the element to become visible.
    • moveTo: We move the touch to the ending point.
    • release: We release the touch action.
  4. The scroll action is performed repeatedly until the element is displayed, indicating that it is now in view.

The scrollDownToElement method presented here provides a reusable solution for ensuring that your target element is within view, allowing you to automate interactions seamlessly. Happy testing!

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();
    


Sunday, July 30, 2023

How to identify the main actvity from the AndroidManifest.xml

 When you setup applium capacbilities giving the app package and app activity, you have to give the main activity as the app activity.

Here is how you identify the main activity from AndroidManifest.xml when it has multiple activities.

  • Locate the AndroidManifest.xml:

The AndroidManifest.xml file is located in the app folder of your Android project. The typical path is app/src/main/AndroidManifest.xml.

  • Open the AndroidManifest.xml:

Use a text editor or an XML editor to open the AndroidManifest.xml file.

  • Search for <activity> tags:

Inside the AndroidManifest.xml, look for <activity> tags. Each <activity> tag represents an activity (screen) of the Android application.

  • Find the Main Activity:

The AppActivity is usually the main activity of the app. It is the entry point of the application, and its intent filter is often set to respond to the android.intent.action.MAIN and android.intent.category.LAUNCHER actions.

  • Look for Intent Filters:

Inside the <activity> tags, check for <intent-filter> tags. The AppActivity should have an <intent-filter> that includes the android.intent.action.MAIN and android.intent.category.LAUNCHER actions.

  • Extract the AppActivity Name:

Extract the value of the android:name attribute from the <activity> tag that has the <intent-filter> with the android.intent.action.MAIN and android.intent.category.LAUNCHER actions. The value of android:name attribute will be the AppActivity.

<activity

    android:name=".ui.MainActivity"  <!-- This is the AppActivity -->

    android:label="@string/app_name">

    <intent-filter>

        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />

    </intent-filter>

</activity>


In this example, the AppActivity is .ui.MainActivity.


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