Showing posts with label UiScrollable. Show all posts
Showing posts with label UiScrollable. Show all posts

Thursday, December 5, 2013

How can we handle Toggle Switch in UiAutomator?

Let us see how we can handle toggle buttons in UiAutomator. I am taking an example of toggle Wi-Fi buttons from settings page.

Test steps:
1. Launch settings application from the App tray
2. Go to Wi-Fi Settings and toggle the wi-fi settings

Test Procedure:

Step 1: Follow this post on how to launch settings application from App tray

Step 2: Toggle wi-fi settings

Before accessing the wi-fi settings, let us write small code to check if the settings application is already launched. Because the UiAutomatorTestCase class extends junit.framework.TestCase, we can use the JUnit Assert class to test that UI components in the app return the expected results

    // Validate that the package name is the expected one
      UiObject settingsValidation = new UiObject(new UiSelector()
         .packageName("com.android.settings"));
      assertTrue("Unable to detect Settings", 
         settingsValidation.exists()); 

Now, let us get the properties of Wi-Fi settings from UiAutomatorViewer

From the above Screnshot, we can get the properties of Wi-Fi settings,
  • Index = 1
  • class = android.widget.LinearLayout
  • scrollable = false
Using these properties, we can create UiObject for Wi-Fi settings. it is also clear that "Switch: ON" and "TextView: Wi-Fi" are child objects of the LinearLayout.
 

UiObject WiFiSettings = new UiObject(new UiSelector().className("android.widget.LinearLayout") .scrollable(false).index(1));
if(WiFiSettings.exists())              WiFiSettings.click();

Now, let us create child object for "Switch: ON"

UiObject WifiOnButton = WiFiSettings.getChild(new UiSelector().text("ON"));

if (WifiOnButton.exists()){              WifiOnButton.click();



If the Wi-Fi is already turned off, we can get the properties of "Switch:OFF"

 
UiObject WifiOffButton = WiFiSettings.getChild(new UiSelector().text("OFF"));
if (WifiOffButton.exists())
 WifiOffButton.click();
Here is the complete code
 public void testToggleWiFiSettings() throws RemoteException,
   UiObjectNotFoundException {

  // Validate that the package name is the expected one
  UiObject settingsValidation = new UiObject(
    new UiSelector().packageName("com.android.settings"));
  assertTrue("Unable to detect Settings", settingsValidation.exists());

  // Wi-Fi settings
  UiObject WiFiSettings = new UiObject(new UiSelector()
    .className("android.widget.LinearLayout").scrollable(false)
    .index(1));

  if (WiFiSettings.exists())
   WiFiSettings.click();

  // Switch ON
  UiObject WifiOnButton = WiFiSettings.getChild(new UiSelector()
    .text("ON"));
  // Switch OFF
  UiObject WifiOffButton = WiFiSettings.getChild(new UiSelector()
    .text("OFF"));

  if (WifiOnButton.exists()) {
   WifiOnButton.click();
  } else if (WifiOffButton.exists()) {
   WifiOffButton.click();
  }

 }

How to launch Settings Application using UiAutomator?

Let us a write a program to Automate the case to toggle the Wi-Fi

Test Case:
1. From the Home page, click on App Tray icon
2. Launch settings from the App Tray by clicking on the Settings icon
3. Turn ON/OFF wi-fi from settings page

Test Procedure:

Step 1: Click on App Tray Icon
See the previous post to know how to click on the App Tray

Step 2:  Launch Settings
From the App Tray, we have to click on the "Settings" icon. App Tray is a scrollable container, we can scroll it horizontally

First screen:

Second screen:

From the above screen it is clear that the settings icon is present in the second screen of the App tray. To click on the settings icon, we have to navigate to the second screen and then click on the settings icon.

Using "UiScrollable" class, we can simulate vertical or horizontal scrolling across a display. This technique is helpful when a UI element is positioned off-screen and you need to scroll to bring it into view.

Create a scrollable object for App view using the class "android.view.view" shown in the UiAutomatorViewer. You can also use the property "scrollable=true"

The following code explains how to create a scrollable object

UiScrollable appViews = new UiScrollable(new UiSelector().className("android.view.View")
        .scrollable(true));


After the scrollable object is created, we have to specify the swipe direction. Default is the vertical scrolling. Since our App view only supports the horizontal swipe, set the direction as horizontal


appViews.setAsHorizontalList();

Now, in the App tray we have to look for the "Settings" application. From the following screenshot, we can get it's class name as 'android.widget.TextView' and contains the text "Settings". Using these properties, we can create a UiObject for Settings app.



Since the settings app is inside the App tray, we have to create a child object for App tray.

   UiObject settingsApp = appView.getChildByText(new UiSelector()
        .className(android.widget.TextView.class.getName()),
        "Settings",true);

In the above code we have used the method "getChildByText(childPattern, text, allowScrollSearch)", which accepts three arguments
     a. Child pattern -- select pattern for the UI element
     b. text  -- Text of the element
     c. AllowScrollSearch -- accepts either true/false. If value is set to true, then UiAutomator will search for the element in all the screens by scrolling the view. When it is set to false, then UiAutomator will only search in the current screen. Since the Settings app is in the second screen, we have to set "allowScrollSearch" to true.


Here is the code   

    //Launch Settings from App Tray
    public void testLaunchSettings() throws UiObjectNotFoundException{

        UiObject AppsTab = new UiObject(new UiSelector().className("android.widget.TextView").description("Apps"));

        if(AppsTab.exists())
            AppsTab.click();
       
        UiScrollable appView = new UiScrollable(new UiSelector().className("android.view.View")
        .scrollable(true));
    
     // Set the swiping mode to horizontal (the default is vertical)
     appView.setAsHorizontalList();
    
     // Search for the settings application in all the screens
     UiObject settingsApp = appView.getChildByText(new UiSelector()
        .className(android.widget.TextView.class.getName()),
        "Settings",true);
    
    if(settingsApp.exists())
     settingsApp.clickAndWaitForNewWindow();
      
    }