上一篇(http://blog.csdn.net/_/article/details/)我们了解了uiautomator的一些基本用法和方法,现在我们再来看看怎么创建一个uiautomator简单的自动登陆淘宝的示例,以下参考网络:http://bbs.csdn.net/topics/
1.新增一个java project

讯享网
2.添加junit


点击 add library,选择junit4

3.添加Android库
点击add external jars

此处地址选为SDK安装目录下的platforms-->android-18之下的两个文件:uiatomation.jar,android.jar
添加成功后如图

4.在java project中新建class

创建完成后,我们就开始编写自动登录的代码,下面贴上:
public class test extends UiAutomatorTestCase {
public void testDemo() throws UiObjectNotFoundException {
String Target = "LOGIN";
UiDevice uiDevice = getUiDevice();
// 模拟 HOME 键点击事件
uiDevice.pressHome();
// 找到 Apps tab按钮
UiObject appsTab = new UiObject(new UiSelector().textContains("手机淘宝"));
sleep(2000);
// 打开App
if (!appsTab.exists()) {
appsTab = new UiObject(new UiSelector().descriptionContains("手机淘宝"));
}
if (appsTab.exists()) {
appsTab.clickAndWaitForNewWindow();
sleep(5000);
}
// 找到我的淘宝控件
UiObject myTab = new UiObject(new UiSelector().text("我的淘宝"));
myTab.clickAndWaitForNewWindow();
// 设置循环次数 这样可以反复执行20次输入账号密码 登录
for (int i = 20; i > 0; i++) {
// 直接去输入账号密码界面
UiObject username = new UiObject(
new UiSelector().resourceId("com.taobao.taobao:id/accountCompleteTextView"));
//清空用户输入栏的内容
username.clearTextField();
//输入用户名
username.setText("zhangsan");
UiObject passwd = new UiObject(new UiSelector().resourceId("com.taobao.taobao:id/content"));
//清空输入密码栏的内容
passwd.clearTextField();
//输入密码
passwd.setText("");
//点击登录按钮
UiObject login = new UiObject(new UiSelector().resourceId("com.taobao.taobao:id/loginButton"));
login.clickAndWaitForNewWindow();
sleep(2000);//等待2秒
String target = "用户名或密码不正确";
UiObject allow = findTextContain(target);
if (!allow.exists()) {
return;
}
UiObject ensure = findTextView("确定");
if (!ensure.exists()) {
ensure = findText("确定");
}
if (!ensure.exists()) {
return;
}
ensure.click();
}
}
//查找控件方法
public UiObject findText(String text) {
UiObject elem = null;
elem = new UiObject(new UiSelector().text(text));
if (elem.exists()) {
return elem;
}
return elem;
}
public UiObject findDesc(String text) {
UiObject elem = null;
elem = new UiObject(new UiSelector().description(text));
if (elem.exists()) {
return elem;
}
return elem;
}
public UiObject findDescContains(String text) {
UiObject elem = null;
elem = new UiObject(new UiSelector().descriptionContains(text));
if (elem.exists()) {
return elem;
}
return elem;
}
public UiObject findTextView(String text) {
UiObject elem = null;
elem = new UiObject(new UiSelector().className(TextView.class).text(
text));
if (elem.exists()) {
return elem;
}
return elem;
}
public UiObject findTextViewContains(String text) {
UiObject elem = null;
elem = new UiObject(new UiSelector().className(TextView.class).textContains(text));
if (elem.exists()) {
return elem;
}
return elem;
}
public UiObject findTextContain(String text) {
int i = 0;
UiObject elem = null;
elem = new UiObject(new UiSelector().textContains(text));
if (elem.exists()) {
return elem;
}
return elem;
}
public boolean hasResId(String resId) {
return findByResId(resId).exists();
}
// Android API Level18及其以上的版本支持此方法
public UiObject findByResId(String resId) {
UiObject elem = null;
elem = new UiObject(new UiSelector().resourceId(resId));
if (elem.exists()) {
return elem;
}
return elem;
}
}
代码上注释都很清楚,就不在说明,接下来是我一开始完全想不通如何把代码运行到手机上去,后来百度后,参考了 http://blog.csdn.net/lihongjian/article/details/ 发现这样做:

第一步:
创建build文件
cmd下运行以下代码进行build文件创建
[java] view plain copy
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/14314.html