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

使用UiAutomation打开设置
本文通过一个示例演示了如何使用UiAutomation工具打开Android设备上的设置应用。具体步骤包括模拟点击Home键返回主屏幕,找到并点击所有应用程序按钮,进入应用标签页,在应用列表中滑动查找并点击设置图标。

以下为使用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 根据对象的属性来进行验证,比如packageName,text
      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()) ;
  }   
}


转载来源:http://blog.youkuaiyun.com/shandong_chu/article/details/8890224

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值