我想用指定的时间长按指定的点.不幸的是,没有像在课堂上长按一样的方法:UiDevice 我可能会编写自己的方法,如下所示:
private void longClick(int x, int y, long time) {
android.graphics.Point point = new android.graphics.Point(x, y);
android.graphics.Point[] points = new android.graphics.Point[2];
points[0] = point;
points[1] = point;
getUiDevice().swipe(points, time / 5); // according to documentation, each step lasts 5ms
}
Run Code Online (Sandbox Code Playgroud)
或使用反射并调用longTap:
private void longClick(int x, int y) {
Field mUiAutomationBridgeField = getUiDevice().getClass().getDeclaredField("mUiAutomationBridge");
mUiAutomationBridgeField.setAccessible(true);
Object mUiAutomationBridge = mUiAutomationBridgeField.get(getUiDevice());
Field mInteractionControllerField = mUiAutomationBridge.getClass().getDeclaredField("mInteractionController");
mInteractionControllerField.setAccessible(true);
Object mInteractionController = mInteractionControllerField.get(mUiAutomationBridge);
Method longTap = mInteractionController.getClass().getDeclaredMethod("longTap", int.class, int.class);
longTap.setAccessible(true);
longTap.invoke(mInteractionController, x, y);
}
Run Code Online (Sandbox Code Playgroud)
然而,它不是满意的解决方案,任何想法如何做得更好的方式?他们为什么错过这种方法?
我有一个Android应用程序,它使用uiautomator单击列表视图中的选项.ListView看起来像这样:

我正在尝试单击"完整基准"列表项,但我的代码无法识别列表项.这就是我所拥有的:
UiScrollable listView = new UiScrollable(new UiSelector().scrollable(
true).className("android.widget.ListView"));
UiObject item1 = listView.getChildByText(new UiSelector()
.className(android.widget.TextView.class.getName()),
"Full Benchmark");
item1.click();
Run Code Online (Sandbox Code Playgroud)
我将不胜感激任何帮助!
有没有人知道如何
am start -a ACTIVITY从uiautomator代码中调用.
或者是否可以直接从junit代码开始活动.
我在Google的uiautomator方面拥有丰富的经验;但是,在将小部件添加到手机主屏幕时,我似乎很沮丧。现在,让它保持简单,并假设要添加小部件的屏幕为空。考虑的过程将是打开应用程序抽屉>单击窗口小部件选项卡>找到要添加的窗口小部件>长按并将窗口小部件拖动到主屏幕。似乎小部件不是“长期可点击的”。任何想法/建议/解决方案将不胜感激。我实现的代码如下。
@Override
protected void setUp() throws UiObjectNotFoundException {
getUiDevice().pressHome();
new UiObject(new UiSelector().className(TEXT_VIEW).description("Apps")).clickAndWaitForNewWindow();
new UiObject(new UiSelector().className(TEXT_VIEW).text("Widgets")).click();
UiScrollable widgets = new UiScrollable(new UiSelector().scrollable(true));
widgets.setAsHorizontalList();
widgets.flingToEnd(MAX_SWIPES);
UiObject widget = widgets.getChildByText(
new UiSelector().className(TEXT_VIEW).resourceId("com.android.launcher:id/widget_name"),
WIDGET_NAME
);
// Returns true
System.out.println("exists(): " + widget.exists());
// Returns false...
System.out.println("longClickable(): " + widget.isLongClickable());
widget.longClick();
// Also tried...
int startX = sonosWidget.getVisibleBounds().centerX();
int startY = sonosWidget.getVisibleBounds().centerY();
getUiDevice().drag(startX, startY, 0, 0, 3);
}
Run Code Online (Sandbox Code Playgroud) 我是Android新手,正在尝试探索AccessibilityService。我扩展了AccessibilityService类,该类获取AccessibilityEvents,并且能够使用这些事件。
运行“ uiautomator dump”时出现问题。我的AccessibilityService被销毁了,并且没有任何Accessibility事件。有没有解决此问题的方法?
任何帮助或建议表示赞赏。提前非常感谢您。
堆栈跟踪如下所示:
W/System.err( 3832): at com.example.myservice.MyAccessibilityService.onUnbind(MyAccessibilityService.java:185)
W/System.err( 3832): at android.app.ActivityThread.handleUnbindService(ActivityThread.java:2629)
W/System.err( 3832): at android.app.ActivityThread.access$1800(ActivityThread.java:141)
W/System.err( 3832): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)
W/System.err( 3832): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 3832): at android.os.Looper.loop(Looper.java:137)
W/System.err( 3832): at android.app.ActivityThread.main(ActivityThread.java:5103)
W/System.err( 3832): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 3832): at java.lang.reflect.Method.invoke(Method.java:525)
W/System.err( 3832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
W/System.err( 3832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
W/System.err( 3832): at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud) android android-service accessibilityservice android-uiautomator
我正在使用UiAutomator,但我无法使用UIAutomatorTestCase扩展我的类,我还添加了jar文件,即UIAutomator jar文件和JUnit3 lib.我在这个类中遇到错误而且我想知道如何为该应用程序构建测试用例以及如何以编程方式从我的应用程序运行测试用例?如果没有那么我怎么能运行这些测试用例.
package com.example.test;
import junit.framework.TestCase;
public class UiAutomatorTestCase extends TestCase {
private static final String DISABLE_IME = "disable_ime";
private static final String DUMMY_IME_PACKAGE = "com.android.testing.dummyime";
private UiDevice mUiDevice;
private Bundle mParams;
private IAutomationSupport mAutomationSupport;
private boolean mShouldDisableIme = false;
@Override
protected void setUp() throws Exception {
super.setUp();
mShouldDisableIme = "true".equals(mParams.getString(DISABLE_IME));
if (mShouldDisableIme) {
setDummyIme();
}
}
@Override
protected void tearDown() throws Exception {
if (mShouldDisableIme) {
restoreActiveIme();
}
super.tearDown();
}
/**
* Get current instance of {@link UiDevice}. …Run Code Online (Sandbox Code Playgroud) 我想在运行时获取android元素的资源ID和内容描述.
我试过这个:
myElement.getAttribute("resource-id")
myElement.getAttribute("content-desc")
Run Code Online (Sandbox Code Playgroud)
但得到错误为" this element does not have the resource-id attribute".
有没有办法得到这个?
我正在将Nexus 5和Cyanogen One plus设备与Lollipop android操作系统配合使用。我正在尝试测试某些应用程序的各种通知。我可以使用UiAutomator成功测试纸盘通知和锁定屏幕通知,但是使用抬头通知无法成功。我尝试了以下代码,但无法检测到它。
public void test_HeadsupTitle() throws InterruptedException, UiObjectNotFoundException, IOException
{
//some code to bring up headsup notification
UiObject maxHeadsUp = new UiObject(new UiSelector().packageName("com.android.systemui").resourceId("android:id/status_bar_latest_event_content"));
// code to add sleep so that it waits for heads up notification to show up
assertTrue(maxHeadsUp.exists());
}
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以将UiAutomator中的抬头通知检测为运行自动化时要查找的对象?
notifications android android-notifications android-uiautomator heads-up-notifications
我想知道我们可以在应用程序中使用UIAutomator吗?我想编写一个打开另一个应用程序的应用程序,在该应用程序EditText中编写一些文本,然后按一些按钮(例如打开hangout并在其上编写文本并发送消息)。你能建议我该怎么做吗?
我已经在网上搜索了,但我只能用UIAutomator测试我的UI,但是我不知道我可以用它编写一个android应用程序(不是jar文件)并在我的android设备上运行它来执行此操作。
我向WindowManager添加了一个浮动视图,并使其可以在屏幕上移动,我可以在单击此视图时执行单击事件,一切正常.
但是,我不知道如何在espresso或UIAutomator中访问此视图.
final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
type,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT
);
ImageView floatingView = new ImageView(mContext);
floatingView.setContentDescription("bugtags_fab_des_normal");
mWindowManager.addView(floatingView, layoutParams);
Run Code Online (Sandbox Code Playgroud)
rect中的白蓝色图标是我正在谈论的浮动视图.
浮动视图响应单击事件,并执行一些任务,现在我想在AndroidJunit测试中执行此操作.
我使用onView方法尝试Espresso,但测试用例:
onView(withContentDescription("bugtags_fab_des_normal")).perform(click());
Run Code Online (Sandbox Code Playgroud)
得到:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with content description: is "bugtags_fab_des_normal"
Run Code Online (Sandbox Code Playgroud)
我尝试UIAutomator Viewer,但我在视图层次结构中找不到floatingView.
如何在espresso或uiautomator中访问此视图并执行单击操作?
@Test
public void testInvoke() {
onView(withContentDescription("bugtags_fab_des_normal")).perform(click());
}
Run Code Online (Sandbox Code Playgroud)
实际上,我使用的是一个名为bugtags.com的sdk ,它是一个简单的app bug报告和崩溃分析工具.
android android-testing android-espresso android-uiautomator