yjh的实习生涯之自动化测试(2)---新建一个自动登录淘宝例子以及如何将java代码运行到手机上

来源:互联网 发布:化合物遗传毒性数据库 编辑:程序博客网 时间:2024/04/29 13:39

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

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("123456");
//点击登录按钮
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/lihongjian944043440/article/details/50325947  发现这样做:


第一步:

创建build文件

cmd下运行以下代码进行build文件创建

[java] view plain copy
  1. android create uitest-project -n <jar name> -t 1 -p <workspace path>  
  2. -n指的是jar包名字(可以自己定义)  
  3. -t指的是你电脑中的第几个SDK,可以通过android list查看  
  4. -p指的是你的工作空间的路径 

例如我先获得-p前面的值


可以看到id是1

那么我的例子可以直接运行,运行成功之后会显示在Demo1工程下创建了build.xml文件:



第二步:

刷新工程之后,我们发现确实多了build.xml文件,将文件打开并将default值“help”修改为“build”:


进入build.xml,把最上方的help改成build:


改成:



第三步:

开始编译,编译方法:

ant –buildfile <build.xml文件路径>

例如我这:



编译成功!生成的jar包文件存放在:E:\AndroidWorkSpace\MyDemo\bin\test3.jar




第四步:

将编译完成的jar包push到手机中,具体操作为:

adb push <path_to_output_jar> /data/local/tmp/

注意,必须push到该路径下!!!

例如:adb push E:\AndroidWorkSpace\MyDemo\bin\test3.jar /data/local/tmp/



最后一步:

运行测试,具体操作为:

adb shell uiautomator runtest<jarname>-c <包名>.<类名>[#test name]

例如:adb shell uiautomator runtest test1.jar -c com.dd.yjh.test



好了,这样既实现了一个最简单的自动登录淘宝的流程。


过程中有两个问题,一个是出现device not found现象,一个是如果用户名中文的话,会输入不了,具体解决方法,我会在下一篇写出。


PS:如果想再次进行测试,需要再循环以上的操作!


0 0
原创粉丝点击