package rom; import java.lang.*; import java.util.Random; import java.io.File; import com.android.uiautomator.core.UiDevice; import com.android.uiautomator.core.UiScrollable; import com.android.uiautomator.core.UiSelector; import com.android.uiautomator.core.UiObject; import com.android.uiautomator.testrunner.UiAutomatorTestCase; import android.R.id; import android.R.string; import android.graphics.Rect; import android.graphics.SumPathEffect; import android.os.RemoteException; import android.util.Log; import com.android.uiautomator.core.UiWatcher; import com.android.uiautomator.core.UiObjectNotFoundException; import com.android.uiautomator.core.UiDevice; public class BTtest extends UiAutomatorTestCase{ public int startapp(String componentName){ StringBuffer app = new StringBuffer(); app.append("am start -n"); app.append(componentName); int ret = 1; try{ Process process = Runtime.getRuntime().exec(app.toString()); ret = process.waitFor(); }catch(Exception e){ e.printStackTrace(); System.out.println("startapp Exception!!! "); } return ret; } public void Screenshot(String name) { //截取当前屏幕图片的方法 File file = new File("/mnt/sdcard/Podcasts/log"+name+".png"); UiDevice.getInstance().takeScreenshot(file); } public void longclick (UiObject ojb){ //长按方法 try{ Rect ojbValue = ojb.getBounds(); UiDevice.getInstance().swipe(ojbValue.centerX(), ojbValue.centerY(), ojbValue.centerX()+5, ojbValue.centerY()+5, 250); }catch(UiObjectNotFoundException e ){ e.printStackTrace(); } } public void testbt()throws UiObjectNotFoundException,RemoteException{ String BTname = "HG11"; sleep(10000); for(int i=0;i<50;i++){ UiDevice.getInstance().sleep(); sleep(1000); UiDevice.getInstance().wakeUp(); sleep(1000); try{ UiObject Unlocked = new UiObject(new UiSelector().resourceId("com.android.systemui:id/notification_stack_scroller")); assertTrue("Unlocked fail!...",Unlocked.exists()); Unlocked.swipeUp(25); sleep(1000); }catch(UiObjectNotFoundException e1){ e1.printStackTrace(); Log.d("Unlocked", "Exception!!!", e1); System.out.println("Unlocked Exception!!!"); Screenshot("Unlocked_"+i); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().sleep(); sleep(1000); UiDevice.getInstance().wakeUp(); sleep(1000); } startapp("com.android.settings/com.android.settings.Settings"); sleep(1000); UiObject bt = new UiObject(new UiSelector().text("蓝牙").resourceId("android:id/title")); assertTrue("bt fail!....",bt.exists()); bt.clickAndWaitForNewWindow(); UiObject off = new UiObject(new UiSelector().text("开启").resourceId("com.android.settings:id/switch_widget")); assertTrue("off fail!....",off.exists()); off.click(); sleep(3000); UiObject on = new UiObject(new UiSelector().text("关闭").resourceId("com.android.settings:id/switch_widget")); assertTrue("on fail!...",on.exists()); on.click(); sleep(1000); UiObject bt1 = new UiObject(new UiSelector().text(BTname).resourceId("android:id/title")); //搜索配对蓝牙 int time = 0 ; do { time++; sleep(5000); } while (!(bt1.exists()||time<12)); sleep(1000); bt1.clickAndWaitForNewWindow(); Screenshot("Pairing_Bluetooth"+i); sleep(2000); UiObject pair = new UiObject(new UiSelector().text("配对").resourceId("android:id/button1")); // 点击配对 assertTrue("pair fail!...",pair.exists()); pair.clickAndWaitForNewWindow(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); startapp("com.mediatek.filemanager/com.mediatek.filemanager.FileManagerOperationActivity"); //进入文件管理 UiObject sd = new UiObject(new UiSelector().text("SD卡").resourceId("com.mediatek.filemanager:id/edit_adapter_name")); assertTrue("sd fail!...",sd.exists()); sd.clickAndWaitForNewWindow(); //点击进入sd卡 UiObject Video_folder = new UiObject(new UiSelector().text("视频shiping") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Picture_folder = new UiObject(new UiSelector().text("墙纸qiangzhi") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Music_folder = new UiObject(new UiSelector().text("音乐yinyue") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject [] random_document = {Video_folder,Picture_folder,Music_folder}; Random random = new Random(); int Value =random.nextInt(3); UiScrollable scroll = new UiScrollable(new UiSelector().resourceId("com.mediatek.filemanager:id/list_view") .className("android.widget.ListView").packageName("com.mediatek.filemanager")); assertTrue("scroll fail!...", scroll.exists()); scroll.scrollIntoView(random_document[Value]); sleep(1000); random_document[Value].clickAndWaitForNewWindow(); if(random_document[Value].equals(Video_folder)){ //进入视频文件夹后 Random Video_random = new Random(); UiObject Video_1 = new UiObject(new UiSelector().text("爱有你才完整官方版--音悦tai.mp4") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Video_2 = new UiObject(new UiSelector().text("荷塘月色--音悦tai.mp4") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Video_3 = new UiObject(new UiSelector().text("江南style完整版--音悦tai.mp4") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Video_4 = new UiObject(new UiSelector().text("泡沫--音悦tai.mp4") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject[] Video_group = {Video_1,Video_2,Video_3,Video_4}; longclick(Video_group[Video_random.nextInt(4)]); //长按 sleep(1000); UiObject share = new UiObject(new UiSelector().description("分享").resourceId("com.mediatek.filemanager:id/share")); assertTrue("share fail!...",share.exists()); share.clickAndWaitForNewWindow(); sleep(1000); }else if(random_document[Value].equals(Picture_folder)){ //进入图片文件夹后 UiObject Picture_1 = new UiObject(new UiSelector().text("087cc91d9f304e05d0581bca.jpg") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Picture_2 = new UiObject(new UiSelector().text("1-100PP93123.jpg") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Picture_3 = new UiObject(new UiSelector().text("1216-110424100921.jpg") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Picture_4 = new UiObject(new UiSelector().text("1229390021306i5q8hgj9cb.jpg") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); longclick(Picture_1); //长按 sleep(1000); Picture_2.click(); sleep(1000); Picture_3.click(); sleep(1000); Picture_4.click(); sleep(1000); UiObject share = new UiObject(new UiSelector().description("分享").resourceId("com.mediatek.filemanager:id/share")); assertTrue("share fail!...",share.exists()); share.clickAndWaitForNewWindow(); sleep(1000); }else if(random_document[Value].equals(Music_folder)){ //进入音乐文件夹后 UiScrollable Music_scroll = new UiScrollable(new UiSelector().resourceId("com.mediatek.filemanager:id/list_view") .className("android.widget.ListView").packageName("com.mediatek.filemanager")); UiObject Music_1 =new UiObject(new UiSelector().text("阿悄 - 神武雨霖铃.mp3") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Music_2 =new UiObject(new UiSelector().text("蔡健雅,周三 - 周三的情书.mp3") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Music_3 =new UiObject(new UiSelector().text("陈奕迅 - 浮夸.mp3") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject Music_4 =new UiObject(new UiSelector().text("陈奕迅 - 十年.mp3") .resourceId("com.mediatek.filemanager:id/edit_adapter_name")); UiObject[] Music_group = {Music_1,Music_2,Music_3,Music_4}; Random Music_random = new Random(); int Music_random_Value = Music_random.nextInt(4); Music_scroll.scrollIntoView(Music_group[Music_random_Value]); sleep(1000); longclick(Music_group[Music_random_Value]); sleep(1000); UiObject share = new UiObject(new UiSelector().description("分享").resourceId("com.mediatek.filemanager:id/share")); assertTrue("share fail!...",share.exists()); share.clickAndWaitForNewWindow(); sleep(1000); } UiObject Share_Bluetooth = new UiObject(new UiSelector().text("蓝牙") .resourceId("android:id/text1").className("android.widget.TextView")); assertTrue("Share_Bluetooth fail!...", Share_Bluetooth.exists()); Share_Bluetooth.clickAndWaitForNewWindow(); sleep(1000); Screenshot("Share_Bluetooth_"+i); UiObject Select_Bluetooth_to_share = new UiObject(new UiSelector().text(BTname).resourceId("android:id/title")); do { sleep(2000); } while (!Select_Bluetooth_to_share.exists()); Select_Bluetooth_to_share.clickAndWaitForNewWindow(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().openNotification(); sleep(5000); /* UiObject Share_progress = new UiObject(new UiSelector().resourceId("com.android.systemui:id/notification_stack_scroller") .className("android.widget.ScrollView")); UiObject Share_progress1 = Share_progress.getChild(new UiSelector().index(0).className("android.widget.FrameLayout")); */ UiObject Share_progress1 = new UiObject(new UiSelector().resourceId("android:id/progress") .className("android.widget.ProgressBar")); for(int j=0;j<5;j++){ //判断蓝牙文件是否传输完毕 do { sleep(1000); } while (Share_progress1.exists()); } sleep(1000); Screenshot("Share_progress_End_"+i); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); startapp("com.android.settings/com.android.settings.Settings"); //再次进入设置 sleep(1000); UiObject bt2 = new UiObject(new UiSelector().text("蓝牙").resourceId("android:id/title")); //点击蓝牙 assertTrue("bt fail!....",bt2.exists()); bt2.clickAndWaitForNewWindow(); UiObject Bluetooth_settings = new UiObject(new UiSelector().description("设备设置") //点击连接的蓝牙设置 .resourceId("com.android.settings:id/deviceDetails")); assertTrue("Bluetooth_settings fail!...",Bluetooth_settings.exists()); Bluetooth_settings.clickAndWaitForNewWindow(); sleep(1000); UiObject Cancel_save = new UiObject(new UiSelector().text("取消保存").resourceId("android:id/button2")); //取消已经连接的蓝牙 Cancel_save.clickAndWaitForNewWindow(); sleep(1000); Screenshot("Cancel_save_"+i); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); UiDevice.getInstance().pressBack(); sleep(1000); } } }
uiautomator 代码记录 : BT发送测试
最新推荐文章于 2019-02-27 17:30:56 发布