安卓自動化測試:Robotium(二)page 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();
}
}
安卓自動化測試:Robotium(二)page object 模式完善測試框架