android自动化测试工具【UiAutomator】——打开settings实例

   以下为使用UiAutomation打开settings的一个标本实例,来简单说明使用该工具的测试步骤

package com.uia.example.my;

//导入需要的内容
import android.os.RemoteException;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class LaunchSettings extends UiAutomatorTestCase {   

   public void testDemo() throws UiObjectNotFoundException, RemoteException {   
      
      System.out.println("My first time to use it") ;
       
      // Simulate a short press on the HOME button. 模拟点击home键
      getUiDevice().pressHome();
      System.out.println("Step 1: Open home screen. ") ;  
      // We’re now in the home screen. Next, we want to simulate
      // a user bringing up the All Apps screen.
      // If you use the uiautomatorviewer tool to capture a snapshot
      // of the Home screen, notice that the All Apps button’s
      // content-description property has the value “Apps”.  We can
      // use this property to create a UiSelector to find the button. 根据属性来获取一个对象
      UiObject allAppsButton = new UiObject(new UiSelector()
         .description("Apps"));
      getUiDevice().sleep() ;
      
      // Simulate a click to bring up the All Apps screen. //模拟点击来搭载所有程序界面
      allAppsButton.clickAndWaitForNewWindow();
      
      System.out.println("Step 2: Bring up the all apps screen. ") ;
      // In the All Apps screen, the Settings app is located in
      // the Apps tab. To simulate the user bringing up the Apps tab,
      // we create a UiSelector to find a tab with the text
      // label “Apps”.
      UiObject appsTab = new UiObject(new UiSelector()
         .text("Apps"));
      
      // Simulate a click to enter the Apps tab. 模拟点击进入所有程序列表
      appsTab.click();

            System.out.println("Step 3: Enter the apps tab. ") ;
      // Next, in the apps tabs, we can simulate a user swiping until
      // they come to the Settings app icon.  Since the container view
      // is scrollable, we can use a UiScrollable object.  使用UiScrollable 设置手机界面显示方向,方便查找需要的程序
      UiScrollable appViews = new UiScrollable(new UiSelector()
         .scrollable(true));
      
      // Set the swiping mode to horizontal (the default is vertical)  设置界面为水平
      appViews.setAsHorizontalList();
      
      System.out.println("Step 4: Set the swiping mode to horizontal. ") ;
      // Create a UiSelector to find the Settings app and simulate      
      // a user click to launch the app.  根据属性className为Settings来获取一个对象
      UiObject settingsApp = appViews.getChildByText(new UiSelector()
         .className(android.widget.TextView.class.getName()),
         "Settings");
      settingsApp.clickAndWaitForNewWindow(); //点击该对象
      
      System.out.println("Step 5: Tap app with name settings. ") ;
      
      // Validate that the package name is the expected one 根据对象的属性来进行验证,比如packageNametext
      UiObject settingsValidation = new UiObject(new UiSelector()
         .packageName("com.android.settings"));

      assertTrue("Unable to detect Settings",  //此处根据断言来判断对象是否存在,如果不存在则给出错误提示
         settingsValidation.exists());  
      System.out.println("Step 5555: Test text is: " + settingsValidation.exists()) ;
      //verify text
     //String sr = new UiSelector.test("Settings") ;
      //UiObject settingsValidation = new UiObject(new UiSelector()
         //     .text("Settings"));
      
      //assertTrue("Unable to detect Settings",
        // settingsValidation.exists());  
      System.out.println("Step 6: Verify this app with text Settings. ") ;
      System.out.println("Step 7: Test text is: " + settingsValidation.exists()) ;
  }   
}

你可能感兴趣的:(android自动化测试工具【UiAutomator】——打开settings实例)