public void testAddToBookmarksVideo() throws UiObjectNotFoundException { findAndRunApp(); searchVideo(videoSearchString); addBookmark(videoSearchString); UiObject searchResult = new UiObject(new UiSelector().textContains(videoSearchString)); assertTrue(videoSearchString, searchResult.exists()); }
@Override public Result execute(Map<String, Object> arguments) throws UiObjectNotFoundException { super.execute(arguments); @SuppressWarnings("unchecked") List<String> optionNames = (List<String>) arguments.get("optionNames"); ; UiScrollable optionsMenu = new UiScrollable( new UiSelector() .className("android.widget.ListView") .packageName("com.android.settings") .focused(false)); if (!isUiObjectAvailable(optionsMenu, arguments)) { return Result.FAILURE; } for (String optionName : optionNames) { UiObject option = optionsMenu.getChildByText( new UiSelector().className(TextView.class.getName()), optionName); if (!isUiObjectAvailable(option, arguments) && !enabled.equals(option.isEnabled())) { return Result.FAILURE; } } return Result.OK; }
public boolean getBoolAttribute(final String attr) throws UiObjectNotFoundException, NoAttributeFoundException { boolean res = false; if (attr.equals("enabled")) { res = el.isEnabled(); } else if (attr.equals("checkable")) { res = el.isCheckable(); } else if (attr.equals("checked")) { res = el.isChecked(); } else if (attr.equals("clickable")) { res = el.isClickable(); } else if (attr.equals("focusable")) { res = el.isFocusable(); } else if (attr.equals("focused")) { res = el.isFocused(); } else if (attr.equals("longClickable")) { res = el.isLongClickable(); } else if (attr.equals("scrollable")) { res = el.isScrollable(); } else if (attr.equals("selected")) { res = el.isSelected(); } else if (attr.equals("displayed")) { res = el.exists(); } else { throw new NoAttributeFoundException(attr); } return res; }
/** * 获取默认搜索关键字并判断与预期结果是否一致 * * @throws UiObjectNotFoundException */ public void excuteSearchDefaultTex() throws UiObjectNotFoundException { UiSelector textView = new UiSelector(); textView.index(3); UiObject search = new UiObject(textView); String text = search.getText(); Assert.assertEquals("", text); }
public void testSearchVideo() throws UiObjectNotFoundException { findAndRunApp(); searchVideo(videoSearchString); getUiDevice().pressDPadDown(); // UiObject searchResult = new UiObject(new // UiSelector().className("android.widget.CheckedTextView").resourceId("com.ted.android:id/title")); UiObject searchResult = new UiObject( new UiSelector() .className("android.widget.CheckedTextView") .textContains(videoSearchString)); assertTrue(videoSearchString, searchResult.exists()); }
/** * Helper function to set an alarm * * @param minutesFromNow * @throws UiObjectNotFoundException */ public void setAlarm(int minutesFromNow) throws UiObjectNotFoundException { UiObject setAlarm = new UiObject(new UiSelector().description("Alarms")); if (!setAlarm.exists()) setAlarm = new UiObject(new UiSelector().textStartsWith("Set alarm")); setAlarm.click(); UtilityFunctions uti = Controller.utilities; // let's add an alarm uti.clickByDescription("Add alarm"); // let's set the time // clickByText("Time"); Calendar c = Calendar.getInstance(); SimpleDateFormat format = new SimpleDateFormat("HHmm"); c.add(Calendar.MINUTE, 1); String now = format.format(c.getTime()); Log.i("ROOT", "alarm time: " + now); new UiObject( new UiSelector() .packageName(packageName) .className("android.widget.Button") .text("" + now.charAt(0))) .click(); new UiObject( new UiSelector() .packageName(packageName) .className("android.widget.Button") .text("" + now.charAt(1))) .click(); new UiObject( new UiSelector() .packageName(packageName) .className("android.widget.Button") .text("" + now.charAt(2))) .click(); new UiObject( new UiSelector() .packageName(packageName) .className("android.widget.Button") .text("" + now.charAt(3))) .click(); // clickByText("Ok"); // few confirmations to click thru UiObject doneButton = new UiObject(new UiSelector().text("Done")); UiObject okButton = new UiObject(new UiSelector().text("OK")); // working around some inconsistencies in phone vs tablet UI if (doneButton.exists()) { doneButton.click(); } else { okButton.click(); // let it fail if neither exists } // we're done. Let's return to home screen // clickByText("Done"); uti.goToHomeScreen(); }
@Override public boolean checkForCondition() { UiObject message = new UiObject(new UiSelector().resourceId("android:id/message").textStartsWith("温馨提示")); if (message.exists()) { UiObject ok = new UiObject(new UiSelector().resourceId("android:id/button1")); try { ok.click(); } catch (UiObjectNotFoundException e) { e.printStackTrace(); } return true; } return super.checkForCondition(); }
public void searchVideo(String videoSearchString) throws UiObjectNotFoundException { UiObject searchVideoButton = new UiObject( new UiSelector() .className("android.widget.TextView") .resourceId("com.ted.android:id/menu_search")); searchVideoButton.clickAndWaitForNewWindow(); UiObject textBox = new UiObject( new UiSelector() .className("android.widget.EditText") .resourceId("android:id/search_src_text")); textBox.setText(videoSearchString); UiDevice device = UiDevice.getInstance(); device.pressEnter(); }
@Override protected void execute() throws UiObjectNotFoundException { super.execute(); // 设置允许加载图片 setLoadPicture(); // 下载指定图片文件 load("testplat.server.ztemt.com.cn/resource/Picture.jpg"); // 断言图片显示 UiObject picture = new UiObject(new UiSelector().descriptionStartsWith("Picture.jpg")); assertTrue("Unable to load picture", picture.waitForExists(8000)); // 退出浏览器 exit(); }
public boolean pinchOut(final int percent, final int steps) throws UiObjectNotFoundException { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { return el.pinchOut(percent, steps); } else { Logger.error("Device does not support API >= 18!"); return false; } }
public String getClassName() throws UiObjectNotFoundException { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { return el.getClassName(); } else { Logger.error("Device does not support API >= 18!"); return ""; } }
public boolean dragTo(final UiObject destObj, final int steps) throws UiObjectNotFoundException { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { return el.dragTo(destObj, steps); } else { Logger.error("Device does not support API >= 18!"); return false; } }
/** Current implementation ignores the exception and continues. */ public void postHandler() { // TODO: Add custom error logging here String formatedOutput = String.format( "UI Exception Message: %-20s\n", UiDevice.getInstance().getCurrentPackageName()); Log.e(LOG_TAG, formatedOutput); UiObject buttonOK = new UiObject(new UiSelector().text("OK").enabled(true)); // sometimes it takes a while for the OK button to become enabled buttonOK.waitForExists(5000); try { buttonOK.click(); } catch (UiObjectNotFoundException e) { Log.e(LOG_TAG, "Exception", e); } }
public void alarmHandler() throws UiObjectNotFoundException, RemoteException { Controller.utilities.execute(Controller.utilities.cmdBroadcastIntent("S-Alarm Setting")); setAlarm(1); Controller.device.sleep(); Controller.utilities.execute(Controller.utilities.cmdBroadcastIntent("E-Alarm Setting")); // wait for the alarm alert dialog Controller.utilities.execute(Controller.utilities.cmdBroadcastIntent("S-Alarm Wait And Ring")); UiObject alarmAlert = new UiObject( new UiSelector() .packageName("com.google.android.deskclock") .className(TextView.class.getName()) .text("Alarm")); Assert.assertTrue( "Timeout while waiting for alarm to go off", alarmAlert.waitForExists(2 * 60 * 1000)); Controller.instance.sleep(5000); Controller.device.swipe(380, 870, 750, 870, 50); Controller.utilities.execute(Controller.utilities.cmdBroadcastIntent("E-Alarm Wait And Ring")); }
public void testDemo() throws UiObjectNotFoundException { // Simulate a short press on the HOME button. getUiDevice().pressHome(); // We’re now in the home screen. Next, we want to simulate // a user bringing up the All Apps screen. // If you use the uiautomatorviewer tool to capture a snapshot // of the Home screen, notice that the All Apps button’s // content-description property has the value “Apps”. We can // use this property to create a UiSelector to find the button. UiObject allAppsButton = new UiObject(new UiSelector().description("Apps")); // Simulate a click to bring up the All Apps screen. allAppsButton.clickAndWaitForNewWindow(); // In the All Apps screen, the Settings app is located in // the Apps tab. To simulate the user bringing up the Apps tab, // we create a UiSelector to find a tab with the text // label “Apps”. UiObject appsTab = new UiObject(new UiSelector().text("Apps")); // Simulate a click to enter the Apps tab. appsTab.click(); // Next, in the apps tabs, we can simulate a user swiping until // they come to the Settings app icon. Since the container view // is scrollable, we can use a UiScrollable object. UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true)); // Set the swiping mode to horizontal (the default is vertical) appViews.setAsHorizontalList(); // Create a UiSelector to find the Settings app and simulate // a user click to launch the app. UiObject settingsApp = appViews.getChildByText( new UiSelector().className(android.widget.TextView.class.getName()), "Settings"); settingsApp.clickAndWaitForNewWindow(); // Validate that the package name is the expected one UiObject settingsValidation = new UiObject(new UiSelector().packageName("com.android.settings")); assertTrue("Unable to detect Settings", settingsValidation.exists()); }
public Point getAbsolutePosition(final Point point, final boolean boundsChecking) throws UiObjectNotFoundException, InvalidCoordinatesException { final Rect rect = el.getBounds(); final Point pos = new Point(); Logger.debug("Element bounds: " + rect.toShortString()); if (point.x == 0) { pos.x = rect.width() * 0.5 + rect.left; } else if (point.x <= 1) { pos.x = rect.width() * point.x + rect.left; } else { pos.x = rect.left + point.x; } if (boundsChecking == true) { if (pos.x > rect.right || pos.x < rect.left) { throw new InvalidCoordinatesException( "X coordinate (" + pos.x.toString() + " is outside of element rect: " + rect.toShortString()); } } if (point.y == 0) { pos.y = rect.height() * 0.5 + rect.top; } else if (point.y <= 1) { pos.y = rect.height() * point.y + rect.top; } else { pos.y = rect.left + point.y; } if (boundsChecking == true) { if (pos.y > rect.bottom || pos.y < rect.top) { throw new InvalidCoordinatesException( "Y coordinate (" + pos.y.toString() + " is outside of element rect: " + rect.toShortString()); } } return pos; }
public void addBookmark(String videoSearchString) throws UiObjectNotFoundException { UiObject checkedVideo = new UiObject( new UiSelector() .className("android.widget.CheckedTextView") .textContains(videoSearchString)); checkedVideo.clickAndWaitForNewWindow(); UiObject addBookmarkButton = new UiObject( new UiSelector() .className("android.widget.ImageView") .resourceId("com.ted.android:id/menu_bookmark")); addBookmarkButton.clickAndWaitForNewWindow(); getUiDevice().pressBack(); getUiDevice().pressBack(); UiObject myTalksButton = new UiObject(new UiSelector().className("android.widget.CheckedTextView").text("My Talks")); myTalksButton.clickAndWaitForNewWindow(); UiObject bookmarksButton = new UiObject( new UiSelector().className("android.widget.CheckedTextView").text("Bookmarks")); bookmarksButton.clickAndWaitForNewWindow(); }
public boolean click() throws UiObjectNotFoundException { return el.click(); }
private void findAndRunApp() throws UiObjectNotFoundException { getUiDevice().pressHome(); UiObject appsTab = new UiObject(new UiSelector().text("TED")); appsTab.click(); }
public Rect getVisibleBounds() throws UiObjectNotFoundException { return el.getVisibleBounds(); }
public void clearText() throws UiObjectNotFoundException { el.clearTextField(); }
public boolean isDialogPresent() { UiObject alertDialog = new UiObject(new UiSelector().packageName("com.android.systemui")); return alertDialog.exists(); }
public void testDemo() throws UiObjectNotFoundException, InterruptedException { // Simulate a short press on the HOME button. getUiDevice().pressHome(); // We’re now in the home screen. Next, we want to simulate // a user bringing up the All Apps screen. // If you use the uiautomatorviewer tool to capture a snapshot // of the Home screen, notice that the All Apps button’s // content-description property has the value “Apps”. We can // use this property to create a UiSelector to find the button. UiObject allAppsButton = new UiObject(new UiSelector().description("Apps")); // Simulate a click to bring up the All Apps screen. allAppsButton.clickAndWaitForNewWindow(); Thread.sleep(2000); // In the All Apps screen, the Settings app is located in // the Apps tab. To simulate the user bringing up the Apps tab, // we create a UiSelector to find a tab with the text // label “Apps”. UiObject appsTab = new UiObject(new UiSelector().text("Apps")); // Simulate a click to enter the Apps tab. appsTab.click(); // SystemClock.sleep(2000); // Next, in the apps tabs, we can simulate a user swiping until // they come to the Settings app icon. Since the container view // is scrollable, we can use a UiScrollable object. UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true)); // Set the swiping mode to horizontal (the default is vertical) appViews.setAsHorizontalList(); // Create a UiSelector to find the Netflix app and simulate // a user click to launch the app. UiObject netflixApp = appViews.getChildByText( new UiSelector().className(android.widget.TextView.class.getName()), "Netflix"); netflixApp.clickAndWaitForNewWindow(); SystemClock.sleep(3000); // create a virtual hand to click UiDevice handClick = UiDevice.getInstance(); // click the menu and "Comedies" handClick.click(20, 95); SystemClock.sleep(1500); handClick.click(300, 650); // enter the first movie in this page SystemClock.sleep(1000); handClick.click(150, 450); SystemClock.sleep(3000); for (int i = 0; i < 2; i++) { // the loop of we want to play // print out which loop now int counti = i + 1; System.out.println("the number of the loop is " + counti); // start play the movie handClick.click(400, 350); System.out.println("start play the movie 1 " + counti); SystemClock.sleep(10000); System.out.println("start play the movie 2 " + counti); // do some operation handClick.click(500, 500); SystemClock.sleep(20000); handClick.click(500, 500); SystemClock.sleep(1000); for (int j = 0; j < 10; j++) { int countj = j + 1; handClick.click(150, 675); // 10 sec seek, 10 times SystemClock.sleep(2000); System.out.println("sec seek " + countj); } handClick.click(750, 685); // normal seek SystemClock.sleep(2000); System.out.println("normal seek 1 " + counti); handClick.click(450, 685); // normal seek SystemClock.sleep(2000); System.out.println("normal seek 2 " + counti); handClick.click(590, 685); // normal seek SystemClock.sleep(2000); System.out.println("normal seek 3 " + counti); SystemClock.sleep(7000); // let is play 5000 ms System.out.println("playing" + counti); // back handClick.click(500, 500); SystemClock.sleep(100); getUiDevice().pressBack(); SystemClock.sleep(150); // add the movie in MyList handClick.click(550, 730); SystemClock.sleep(1000); // drag the menu and enter the next movie handClick.drag(16, 1025, 16, 185, 80); SystemClock.sleep(200); handClick.click(350, 1050); } // back and over // getUiDevice().pressHome(); handClick.click(20, 95); SystemClock.sleep(200); getUiDevice().pressBack(); System.out.println("The test is over"); // Validate that the package name is the expected one UiObject settingsValidation = new UiObject(new UiSelector().packageName("com.netflix.mediaclient")); assertTrue("Unable to detect Settings", settingsValidation.exists()); }
public void testcase() throws IOException { try { Common.startLog(runcase, "*****Start to run " + runcase + " *****"); // 通过是否有IPC按钮判断是否进入 Common.openActivity(runcase, in, "cn.com.mobnote.golukmobile:id/index_carrecoder_btn"); sleep(2000); // 选择 IPC按钮 从下边状态栏 Common.clickViewById(runcase, in, "cn.com.mobnote.golukmobile:id/index_carrecoder_btn"); sleep(2000); // 检查是否连接到IPC Common.connectWifi(runcase, in, "cn.com.mobnote.golukmobile:id/mConnectTip"); sleep(2000); Common.clickViewById(runcase, in, "cn.com.mobnote.golukmobile:id/mPlayBtn"); sleep(2000); int i = 1; UiObject waitingnote = Common.findViewById2(in, "cn.com.mobnote.golukmobile:id/mLoading"); while (i < 60) { if (waitingnote.exists()) { sleep(1000); Common.infoLog(runcase, "Loading Preview" + i + "sec"); i++; } else { Common.infoLog(runcase, "Loading Finish"); sleep(3000); break; } } if (i == 60) { throw new Exception("Loading more than 60s"); } sleep(2000); Common.clickViewById(runcase, in, "cn.com.mobnote.golukmobile:id/m8sBtn"); sleep(7000); int waitforshare = 1; UiObject shareBtn = null; while (waitforshare < 30) { shareBtn = Common.findViewById2(in, "cn.com.mobnote.golukmobile:id/new1"); if (shareBtn.exists()) { Common.infoLog(runcase, "精彩视频拍摄保存成功"); sleep(2000); Common.clickViewById(runcase, in, "cn.com.mobnote.golukmobile:id/image1"); UiObject btn = Common.findViewById2(in, "cn.com.mobnote.golukmobile:id/back_btn"); if (btn.exists()) { btn.click(); } break; } sleep(5000); waitforshare++; } if (waitforshare == 30) { throw new Exception("精彩视频拍摄保存失败"); } Common.backToHome(runcase, in); Common.passcase(runcase); Common.startLog(runcase, "*****End to run " + runcase + " *****"); } catch (Exception e) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); Date curDate = new Date(System.currentTimeMillis()); String str = formatter.format(curDate); String s = Common.checkFailReason2(runcase, in, str, e.getMessage()); Common.infoLog(runcase, "截图存储在 in /sdcard/GolukTest/" + runcase + "/" + str + ".png"); Common.takeScreen(in, runcase, str); // String s=null; // s=Common.checkFailReason(runcase,in, e.getMessage()); Common.backToHome(runcase, in); Common.errorLog(runcase, s); Common.failcase(runcase); Common.startLog(runcase, "*****End to run " + runcase + " *****"); } }
protected void setLoadPicture() throws UiObjectNotFoundException { // 点击更多按钮 showMore(); // 选择设置 UiObject settings = new UiObject(new UiSelector().resourceId("android:id/title").text("设置")); settings.clickAndWaitForNewWindow(); // 选择带宽管理 UiObject privacy = new UiObject(new UiSelector().resourceId("android:id/title").text("带宽管理")); privacy.clickAndWaitForNewWindow(); // 勾选加载图片 UiObject list = new UiObject(new UiSelector().className(ListView.class)); for (int i = 0; i < list.getChildCount(); i++) { UiObject ll = list.getChild(new UiSelector().className(LinearLayout.class).index(i)); UiObject tv = ll.getChild(new UiSelector().className(TextView.class).index(0)); if (tv.getText().equals("加载图片")) { UiObject enable = ll.getChild(new UiSelector().resourceId("android:id/checkbox")); if (enable.isCheckable() && !enable.isChecked()) { enable.click(); } break; } } // 返回浏览界面 getUiDevice().pressBack(); getUiDevice().pressBack(); }
/** * 点击搜索框后,输入搜索关键词进行搜索 * * @throws UiObjectNotFoundException */ public void setSearchWord() throws UiObjectNotFoundException { // 点击搜索框 UiObject textV = new UiObject(new UiSelector().resourceId("com.mappn.gfan:id/tv_search")); textV.click(); // 判断搜索框是否存在-输入QQ搜索 UiObject textVChild = new UiObject(new UiSelector().resourceId("com.mappn.gfan:id/top_search_container")); Assert.assertEquals(true, textVChild.exists()); textVChild.setText("QQ"); sleep(1000); // 获取搜索关键字列表 UiObject listViewOb = new UiObject(new UiSelector().resourceId("com.mappn.gfan:id/baselistview")); // Assert.assertEquals(0, listViewOb.getChildCount()); // 点击列表第一个搜索关键字 UiObject childOb = listViewOb.getChild(new UiSelector().index(6)); // childOb.click(); Assert.assertEquals(true, childOb.exists()); if (childOb.exists()) { childOb.clickAndWaitForNewWindow(); } else { UiObject selectButton = new UiObject(new UiSelector().resourceId("com.mappn.gfan:id/top_search_container")); selectButton.clickAndWaitForNewWindow(); } }
public void testLogin() throws UiObjectNotFoundException { UiObject validation = new UiObject(new UiSelector().packageName("com.chaozh.iReader")); assertTrue(validation.waitForExists(5000)); UiObject confirm = new UiObject( new UiSelector().resourceId("com.chaozh.iReader:id/defualt_compoundButton").text("确定")); if (confirm.waitForExists(3000)) { confirm.click(); } UiScrollable pager = new UiScrollable(new UiSelector().resourceId("com.chaozh.iReader:id/viewpager")); if (pager.exists()) { pager.setAsHorizontalList(); pager.scrollToEnd(1); UiObject startup = new UiObject(new UiSelector().resourceId("com.chaozh.iReader:id/btn_start")); startup.click(); } UiObject center = new UiObject(new UiSelector().resourceId("com.chaozh.iReader:id/bookShelf_head_Center_ID")); center.click(); center.click(); UiObject left = new UiObject(new UiSelector().resourceId("com.chaozh.iReader:id/bookShelf_head_left_ID")); left.click(); UiObject login = new UiObject(new UiSelector().resourceId("com.chaozh.iReader:id/person_longin")); login.clickAndWaitForNewWindow(); UiObject ireader = new UiObject( new UiSelector() .resourceId("com.chaozh.iReader:id/account_main_thirdlogin") .childSelector( new UiSelector() .index(0) .childSelector( new UiSelector() .index(1) .childSelector( new UiSelector() .index(1) .childSelector(new UiSelector().index(0)))))); ireader.clickAndWaitForNewWindow(); UiObject username = new UiObject( new UiSelector() .resourceId("com.chaozh.iReader:id/account_block_zhangyueid_login_name")); username.setText("18824238317"); UiObject password = new UiObject( new UiSelector() .resourceId("com.chaozh.iReader:id/account_block_zhangyueid_login_password")); password.setText("ztemt123"); UiObject loginbtn = new UiObject( new UiSelector() .resourceId("com.chaozh.iReader:id/account_block_zhangyueid_login_submit")); loginbtn.click(); }
public boolean setText(final String text) throws UiObjectNotFoundException { return el.setText(text); }
public void testLogin() throws UiObjectNotFoundException { UiObject validation = new UiObject(new UiSelector().packageName("com.jingdong.app.mall")); assertTrue(validation.waitForExists(5000)); UiScrollable pager = new UiScrollable(new UiSelector().resourceId("com.jingdong.app.mall:id/viewpager")); if (pager.waitForExists(3000)) { pager.setAsHorizontalList(); pager.scrollToEnd(4); UiObject startup = new UiObject(new UiSelector().resourceId("com.jingdong.app.mall:id/btn_experience")); startup.click(); } UiObject my = new UiObject( new UiSelector() .resourceId("com.jingdong.app.mall:id/bottomMenu") .childSelector(new UiSelector().className(RadioButton.class).index(4))); my.waitForExists(8000); my.click(); my.click(); UiObject login = new UiObject( new UiSelector().resourceId("com.jingdong.app.mall:id/personal_click_for_login")); login.clickAndWaitForNewWindow(); UiObject username = new UiObject(new UiSelector().resourceId("com.jingdong.app.mall:id/login_input_name")); username.setText("18824238317"); UiObject password = new UiObject(new UiSelector().resourceId("com.jingdong.app.mall:id/login_input_password")); password.setText("ztemt123"); UiObject loginbtn = new UiObject(new UiSelector().resourceId("com.jingdong.app.mall:id/login_comfirm_button")); loginbtn.click(); }
/** * 点击搜索框 * * @throws UiObjectNotFoundException */ public void clickSearchTextV() throws UiObjectNotFoundException { UiObject textV = new UiObject(new UiSelector().resourceId("com.mappn.gfan:id/tv_search")); textV.click(); }