我有以下脚本,用于在 Android 中使用 UiAutomator 将“33”输入计算器。然而,只有第一个“3”被接受,第二个按下被完全忽略。
import com.android.uiautomator.core.*;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class MyFirstUiAutomatorTest extends UiAutomatorTestCase {
UiObject getByDescription(String description) {
return new UiObject(new UiSelector().description(description));
}
UiObject getByText(String description) {
return new UiObject(new UiSelector().text(description));
}
UiObject scrollableGetByText(String text ) throws UiObjectNotFoundException {
UiScrollable uiScrollable = new UiScrollable(new UiSelector().scrollable(true));
uiScrollable.setAsHorizontalList();
return uiScrollable.getChildByText(new UiSelector().className(
android.widget.TextView.class.getName()),
text);
}
public void testStuff() throws UiObjectNotFoundException {
getUiDevice().pressHome();
getByDescription("Apps").clickAndWaitForNewWindow();
getByText("Apps").click();
scrollableGetByText("Calculator").clickAndWaitForNewWindow();
// pressing '+' and '=' effectively clears the previous input
getByText("+").click();
getByText("=").click();
getByText("3").click();
// this second '3' is ignored
getByText("3").click();
}
}
我尝试在第一次点击后添加 sleep 2 秒,方法是:
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
...但这并没有改变任何东西。
我也尝试点击不同的按钮,在 2'3's 之间,即:
new UiObject(new UiSelector().text("3")).click();
new UiObject(new UiSelector().className("android.widget.EditText")).click();
new UiObject(new UiSelector().text("3")).click();
...但这也不起作用。
想法?
(注意:在 AVD 中使用 Android 4.1.2;在 Ubuntu linux 12.04 上运行)
编辑,根据 Rami 的观察,我尝试了以下操作,以针对相同描述的第二个请求重用相同的 UiObject 对象:
HashMap<String,UiObject> objectByText = new HashMap<String,UiObject>();
UiObject getByText(String description) {
if( objectByText.containsKey(description)) {
System.out.println("" + objectByText.get(description) );
return objectByText.get(description);
}
System.out.println("Created new object for [" + description + "]");
UiObject object = new UiObject(new UiSelector().text(description));
objectByText.put(description, object );
System.out.println("" + object );
return object;
}
...但它没有用,即使它每次都明显重复使用相同的 UiObject,因为它只说了一次“为 [3] 创建了新对象”。
然后我尝试了 UiDevice.click()“技巧”,通过创建一个函数“click”,再次遵循 Rami 的观察:
void click(UiObject target ) throws UiObjectNotFoundException {
Rect rect = target.getBounds();
System.out.println("rect: " + rect );
getUiDevice().click(rect.centerX(), rect.centerY());
}
然而,这对我也不起作用:只出现第一个“3”,而第二个被忽略,即使两次点击显然在同一个地方,因为 rect:
输出位置相同。如果我使用自己的桌面鼠标手动点击“3”两次,那么两个 3 都显示正常。
我还尝试在点击之间添加两秒的 Thread.sleep()
,但仍然只出现一个“3”。
最佳答案
不要只按文本搜索,尝试按文本和类别搜索。 这是这样做的示例方法。
Uiobject getByTextAndClass(String text, String className) {
return new UiObject(new UiSelector().text(text).className(className));
}
然后,如果您尝试为上面带有数字 3 的计算器按钮调用此方法:
getByTextAndClass("3", android.widget.Button.class.getName()).click();
您可以使用 UiAutomatorViewer 工具:{android-sdk}/tools/uiautomator.bat 来检查不同 UiObject 的类名和其他属性。
这适用于我的 4.2.2 设备,但我正在下载 4.1.2 以在那里进行测试。
我在 4.1.2 AVD 上试过它,它可以在我的 Windows 机器上运行。
关于java - 如何使用 Google UiAutomator 按下按钮两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17396646/