安卓自动化测试:Robotiumpage object 模式完善测试框架
Posted 拉努斯石
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓自动化测试:Robotiumpage object 模式完善测试框架相关的知识,希望对你有一定的参考价值。
1、目前比较流行的设计模式就是page object,page object就是把页面作为对象,在使用中传递页面对象,来使用页面对象中相应的成员或者方法,能更好的提现java的面向对象和封装特性
2、具体思想:测试框架分为四大类
1)编写抽象父类:建立Utils包,存放工具类,用于自动完成唤醒手机、解锁、登录等前置操作,之后的用例只需继承抽象父类即可
2)提取控件ID类:在工程中建立从com.pacagename.test.elements包用例存放控件ID类。命名一般以Element开头,后面接Activity结尾,举个栗子:ElementLoginActivity,代码如下:
package com.coolwi.km.user.elements;
import android.R;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.coolwi.km.user.test.myOperationFun;
import com.robotium.solo.Solo;
public class ElementsLoginActivity {
private Solo solo;
private EditText login_numEditText,noteEditText;
private Button bt_loginButton;
private LinearLayout delete_numButton,login_ll_wexin;
private TextView gain_note;
public ElementsLoginActivity(Solo solo) {
this.solo=solo;
}
public void initViews(){
try {
login_numEditText=(EditText) clickById("login_numEditText");
noteEditText=(EditText) clickById("noteEditText");
delete_numButton=(LinearLayout) clickById("delete_num");
bt_loginButton=(Button) clickById("bt_login");
gain_note=(TextView) clickById("gain_note");
login_ll_wexin=(LinearLayout) clickById("login_ll_wexin");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*noteEditText=(EditText)solo.getCurrentActivity().findViewById(R.id.noteEditText);
delete_numButton=(LinearLayout)solo.getCurrentActivity().findViewById(R.id.delete_num);
bt_loginButton=(Button)solo.getCurrentActivity().findViewById(R.id.bt_login);
gain_note=(TextView)solo.getCurrentActivity().findViewById(R.id.gain_note);
login_ll_wexin=(LinearLayout)solo.getCurrentActivity().findViewById(R.id.login_ll_wexin); */
}
public EditText getlogin_numEditText(){
return login_numEditText;
}
public EditText getnoteEditText() {
return noteEditText;
}
public LinearLayout getdelete_numButton(){
return delete_numButton;
}
public Button getbt_loginButton() {
return bt_loginButton;
}
public void enterlogin_num(String text) {
solo.enterText(login_numEditText, text);
}
public void enternote(String text) {
solo.enterText(noteEditText, text);
}
public void enterbt_loginButton(){
solo.clickOnView(bt_loginButton);
}
public void clickdelete_numButton(){
solo.clickOnView(delete_numButton);
}
public void clickgain_note() {
solo.clickOnView(gain_note);
}
public void clicklogin_ll_wexin() {
solo.clickOnView(login_ll_wexin);
}
private void clickbt_loginButton() {
// TODO Auto-generated method stub
solo.clickOnView(bt_loginButton);
}
}
3)操作统一入口类:用于操作提取的控件ID类。新建一个UIHeleper类,用于操控所有的控件类。
package com.coolwi.km.user.test;
import com.coolwi.km.user.elements.ElementsLoginActivity;
import com.robotium.solo.Solo;
public class UIHelper {
private Solo solo=null;
private ElementsLoginActivity ElementsLoginActivity;
public UIHelper() {
// TODO Auto-generated constructor stub
}
public UIHelper(Solo solo){
this.solo=solo;
}
public Solo getsolo(){
return solo;
}
public ElementsLoginActivity getElementsLoginActivity() {
if(ElementsLoginActivity==null){
ElementsLoginActivity=new ElementsLoginActivity(solo);
}
ElementsLoginActivity.initViews();
return ElementsLoginActivity;
}
}
4)更新抽象父类和测试用例:用于更新抽象父类,并根据需求更新对应的测试用例。举个栗子:
a、基本类如下:
package com.coolwi.km.user.test;
import android.test.ActivityInstrumentationTestCase2;
import com.robotium.solo.Solo;
public class BasicTestCase extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.coolwi.km.user.activity.SplashActivity";
@SuppressWarnings("unused")
private static Class<?> launchActiviClass;
static{
try {
launchActiviClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);// TODO: handle exception
}
}
@SuppressWarnings("unchecked")
public BasicTestCase() throws Exception{
super(launchActiviClass);
}
public Solo solo;
public UIHelper uiHelper=null;
public void init() {
// TODO Auto-generated method stub
solo=new Solo(getInstrumentation(),getActivity());
uiHelper=new UIHelper(solo);
}
}
b、登录类继承基本类,并成功登录
package com.coolwi.km.user.test;
public class TestCase1 extends BasicTestCase {
public TestCase1() throws Exception {
super();
// TODO Auto-generated constructor stub
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@SuppressWarnings("unused")
private void testTestCase1() {
// TODO Auto-generated method stub
// uiHelper.getElementsLoginActivity().
uiHelper.getElementsLoginActivity().enterlogin_num("15088132074");
uiHelper.getElementsLoginActivity().enternote("159753");
uiHelper.getElementsLoginActivity().enterbt_loginButton();
}
}
以上是关于安卓自动化测试:Robotiumpage object 模式完善测试框架的主要内容,如果未能解决你的问题,请参考以下文章