UiAutomator Android 的自动测试框架(UiAutomator 快速调试)

上一张我们说了UiAutomator的基础,同时我们发现在实际开发中也很费时间,  本章我们将采用快速调试来节省大家的时间。

用到的工具包  UiAutomatorHelper,”点击下载“  你也可以去github上去下载。

UiAutomatorHelper工具包主要步骤如图:

  

省去我们大量的时间,快速测试调试。

UiAutomatorHelper的结构大家都应该看的懂,里面的代码都是有注释的。

下面主要是给大家讲讲怎么用,

打开eclipse开发工具,建立一个java项目,创建libs文件夹,用于装android.jar 和uiautomator.jar,他在android 的sdk文件sdk-platgorms-android19,或者android17以上,android17这些我就不做过多解释了,相信大家做android开发的都知道。

将android.jar 和uiautomator.jar复制到java项目的libs文件夹中,在将android.jar 和uiautomator.jar 添加(bulid path)到项目(add)中,这样就可以用了。

新建java类(Test) 继承UiAutomatorTestCase类。

public class Test extends UiAutomatorTestCase {

	public static void main(String[] args) {

		String jarName = "UIDemoTest";//需要生成的jar包
		String testClass = "com.cyj.uiauto.Test";//类名
		String testName = "testRecent";//方法名  
		String androidId = "1"; //android id 可以在命令窗体输入android list查看

		new UiAutomatorHelper(jarName, testClass, testName, androidId);

	}
/**
 * 点击最近运行的按钮建
 * @throws RemoteException
 * @throws 
 * @throw
 */
	public void testRecent() throws RemoteException
	{
		UiDevice.getInstance().pressRecentApps();
		sleep(2000);
		
	}
	/**
	 * 自动点击浏览器,并输入网址进行预览
	 * @throws UiObjectNotFoundException
	 * @throws 
	 * @throw
	 */
	public void testDrowser() throws UiObjectNotFoundException {
		UiDevice.getInstance().pressHome();// 点击home键
		UiObject browser = new UiObject(new UiSelector().text("互联网"));
		browser.clickAndWaitForNewWindow();// 点击屏幕
		UiObject edit = new UiObject(
				new UiSelector().className("android.widget.EditText"));
		edit.click();
		UiDevice.getInstance().pressDelete();
		edit.setText("www.baidu.com");
		UiDevice.getInstance().pressEnter();
		sleep(3000);

	}
}

新建测试的方法,注意的是方法必须是test开头的。

我在类里创建了2个方法testRecent()与testDrowser(),

并且创建了个main方法,大家都知道程序要在控制台上运行必须要有main主方法。

在主方法中通过

String jarName = "UIDemoTest";//需要生成的jar包
        String testClass = "com.cyj.uiauto.Test";//类名
        String testName = "testRecent";//方法名  
        String androidId = "1"; //android id 可以在命令窗体输入android list查看

        new UiAutomatorHelper(jarName, testClass, testName, androidId);

来实现调用。

这时只需要运行我们的Test就能实现调试了。

源码点击下载

想要更多的编程学习资料,请关注微信公众号:IT010101

发布了24 篇原创文章 · 获赞 16 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/ccc920123/article/details/51321971
今日推荐