|
2#
楼主 |
发表于 2018-4-26 16:39:24
|
只看该作者
- solo.clickOnButton(3);
- if(solo.searchText("订单一")){
- if(solo.searchButton("取消订单")){
- solo.clickOnButton("取消订单");
- solo.waitForDialogToOpen(1000);
- solo.clickOnButton("放弃取消");
- }
- solo.waitForDialogToClose(1000);
- if(solo.searchButton("取消订单")){
- solo.clickOnButton("取消订单");
- solo.waitForDialogToOpen(1000);
- solo.clickOnButton("确认取消");
- }
- }
- }
- public void testOrderSingleTicket() throws Exception{
- Log.v(Tag,"买单程船票");
- Log.v(Tag,"testOrderSingleTicket。。。。");
- solo.waitForActivity(LAUNCHER_ACTIVITY_FULL_CLASSNAME, 5000);
- is_login=IsLogin();
- //点击进入买船票的页面
- View BuyTicket = solo.getCurrentActivity().findViewById(3);//3、4、5分别代表主要当中的可点击的三个,按顺序进行
- solo.clickOnView(BuyTicket);
- solo.clickOnRadioButton(0);//页面使用RadioButton(单程)为0,往返为1
- //选择出发地点
- Log.v(Tag, "选择出发地点");
- solo.clickOnText("出发地点");
- solo.clickOnText("广州-番禺莲花山");
- solo.sleep(1000);
- //选择到达地点
- Log.v(Tag, "选择到达地点");
- solo.clickOnText("到达地点");
- solo.clickOnText("香港-中港码头");
- solo.sleep(1000);
- //选择出发日期
- Log.v(Tag, "选择出发日期");
- solo.clickOnText("出发日期");
- //获取需要点击的时间日
- int day = GetCurrentDay();
- if(day<=10){
- //day为index
- day = day + 30;
- }else{
- day = day + 39;
- }
- View time_day = null;
- time_day = solo.getView(TextView.class, day);
- solo.clickOnView(time_day);
- solo.clickOnButton("选择航班");
- //获取航班列表,固定选取当前能选取的最后的航班
- // ArrayList<TextView> list = solo.clickInList(0);
- // if (list.isEmpty()){
- // solo.clickOnText("前一天");
- // }
- solo.sleep(500);
- solo.clickInList(3, 0);
- solo.clickOnText("普通舱");
- solo.clickOnText("头等舱");
- solo.clickOnButton(3);
- solo.clickOnButton(5);
- solo.clickOnText("立即购票");
- if(solo.waitForDialogToOpen(1000)){
- Log.v(Tag,"成人票和儿童票不一致");
- if(solo.searchText("提示")){
- solo.clickOnButton("继续");
- }
- }
- SelectPayWay(is_login);
- Log.v(Tag,"完成买船票");
- }
- public void testOrderDoubleTicket() throws Exception{
- Log.v(Tag,"testOrderDoubleTicket starting...");
- solo.waitForActivity(LAUNCHER_ACTIVITY_FULL_CLASSNAME, 5000);
- is_login=IsLogin();
- //点击进入买船票的页面
- View BuyTicket = solo.getCurrentActivity().findViewById(3);//3、4、5分别代表主要当中的可点击的三个,按顺序进行
- solo.clickOnView(BuyTicket);
- solo.clickOnRadioButton(1);//页面使用RadioButton(单程)为0,往返为1
- solo.clickOnText("出发地点");
- solo.clickOnText("广州-番禺莲花山");
- solo.sleep(1000);
- solo.clickOnText("到达地点");
- solo.clickOnText("香港-中港码头");
- solo.sleep(1000);
- solo.clickOnText("出发日期");
- int day = GetCurrentDay();
- if(day<=10){
- //day为index
- day = day + 29;
- }else{
- day = day + 38;
- }
- View time_day = null;
- time_day = solo.getView(TextView.class, day);
- solo.clickOnView(time_day);
- solo.clickOnText("出发航班");
- solo.sleep(1000);
- solo.clickInList(3, 0);
- //返程日期使用默认输入的日期
- solo.clickOnText("返程航班");
- solo.clickInList(3, 0);
- solo.clickOnButton("下订单");
- // solo.clickOnButton(5);
- solo.clickOnButton("立即购票");
- SelectPayWay(is_login);
- Log.v(Tag,"购票完成");
- }
- //该方法需要在一个test最初进行调用
- private boolean IsLogin(){
- Log.v(Tag,"IsLogin。。。");
- solo.waitForActivity(LAUNCHER_ACTIVITY_FULL_CLASSNAME, 5000);
- //点击进入买船票的页面
- View BuyTicket = solo.getCurrentActivity().findViewById(4);//3、4、5分别代表主要当中的可点击的三个,按顺序进行
- solo.clickOnView(BuyTicket);
- if(solo.searchText("个人账户")){
- solo.goBack();
- return true;
- }
- solo.goBack();
- return false;
- }
- @SuppressWarnings("unused")
- private void Logout(){
- if(IsLogin()){
- View BuyTicket = solo.getCurrentActivity().findViewById(4);//3、4、5分别代表主要当中的可点击的三个,按顺序进行
- solo.clickOnView(BuyTicket);
- solo.clickOnButton("退出登录");
- }
- solo.goBack();
- }
- //可能重复使用到的函数
- private int GetCurrentDay(){
- Time now_t = new Time();
- now_t.setToNow();
- now_day = now_t.monthDay;
- return now_day;
- }
- private void SelectPayWay(final boolean is_login){
- Log.v(Tag,"Select Pay way");
- if(solo.searchText("选择支付方式")){
- if(!is_login){
- Log.v(Tag,"未登录");
- if(solo.searchText("登录/注册")){
- solo.clickOnText("登录/注册");
- solo.sleep(1000);
- if(solo.searchText("会员中心")){
- //注册新用户
- solo.clickOnButton("注册");
- if(solo.searchText("请输入电话号码")){
- solo.enterText(0, phone_no);
- solo.sleep(5000);
- if(solo.searchText("号码已存在,请重新输入")){
- Log.v(Tag,"注册页面OK。。。");
- solo.goBack();
- }
- }
- //进入登录页面登录
- solo.clearEditText(0);
- solo.clearEditText(1);
- solo.enterText(0, phone_no);
- solo.enterText(1, phone_sc);
- solo.clickOnButton("登录");
- }
- }
- }else{
- Log.v(Tag,"已经登录");
- solo.clickOnText("充值");
- solo.goBack();
- }
- switch(ticket_pay_way){
- case 2:
- solo.clickOnText("支付宝快捷支付");
- break;
- case 3:
- solo.clickOnText("微信支付");
- break;
- default:
- solo.clickOnText("现金账户支付");
- break;
- }
- }
- solo.clickOnButton("立即支付");
- solo.sleep(5000);
- solo.goBack();
- }
- }
- 复制代码
- 通过以上代码可知,在编写测试类的时候,需要注意的是:
- 测试类需要继承自ActivityInstrumentationTestCase2
- 需要在测试包中导入robotium-solo-x.x.x.jar,以便能够在代码中import com.robotium.solo.Solo;
- 测试类中需要有一个launcherActivityClass(启动activity),以便启动对应的测试APP,否则无法进行测试;
- 当然,如果要此测试类能够正常执行,还需要在androidmanifest.xml中配置。
- <instrumentation
- android:name="android.test.InstrumentationTestRunner"
- android:targetPackage="com.your.packagename" />
复制代码 |
|