如何preSS使用谷歌UiAutomator两次按钮? [英] How to press a button twice using Google UiAutomator?
问题描述
我有以下脚本输入33到计算器,在Android中,使用UiAutomator。但是,只有第一个3被接受,第二preSS被完全忽略。
I have the following script for typing '33' into the Calculator, in Android, using UiAutomator. However, only the first '3' is accepted, the second press is entirely ignored.
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();
}
}
我已经尝试添加一个睡眠2秒后,第一次点击,这样做:
I've tried adding a sleep for 2 seconds after the first click, by doing:
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
...但是,这并没有改变任何东西。
... but that didn't change anything.
我也试过2之间点击不同的按钮,在3的,即:
I also tried clicking on a different button, in between the 2 '3's, ie:
new UiObject(new UiSelector().text("3")).click();
new UiObject(new UiSelector().className("android.widget.EditText")).click();
new UiObject(new UiSelector().text("3")).click();
...但是,这也不能工作。
... but that didn't work either.
想法?
(注:采用Android 4.1.2,在AVD;在Ubuntu Linux上运行12.04)
(Note: using Android 4.1.2, in an AVD; running on Ubuntu linux 12.04)
编辑,下面拉米的意见,我尝试以下,重复使用相同的UIObject的对象第二次请求相同的说明:
Edit, following Rami's observations, I tried the following, to reuse the same UiObject object for a second request for the same description:
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]一次。
... but it didn't work, even though it is clearly reusing the same UiObject each time, because it only says 'Created new object for [3]' once.
然后我尝试了UiDevice.click()'把戏',通过创建功能点击如下,再次,下面拉米的看法:
Then I tried the UiDevice.click() 'trick', by creating a function 'click' as follows, again, following Rami's observations:
void click(UiObject target ) throws UiObjectNotFoundException {
Rect rect = target.getBounds();
System.out.println("rect: " + rect );
getUiDevice().click(rect.centerX(), rect.centerY());
}
无论是
不过,这并没有为我工作:只有第一个3出现,第二个被忽略,即使两者的点击都清楚地在同一个地方,因为 RECT:
输出位置是相同的。如果我点击'3'两次手工,用我自己的桌面鼠标,那么这两个三分球出现ok了。
However, this didn't work for me either: only the first '3' appears, and the second is ignored, even though both clicks are clearly in the same place, because the rect:
output locations are identical. If I click '3' twice manually, using my own desktop mouse, then both 3s appear ok.
我也尝试添加两秒钟的视频下载()
的点击之间,仍然只有一个3出现了我。
I also tried adding a two second Thread.sleep()
between the clicks, and still only a single '3' appeared for me.
推荐答案
而不是通过文字只是在寻找,试图通过文字和类搜索。 下面是这样做的一个抽样的方法。
Instead of just searching by text, try searching by the text and the class. Here is a sample method for doing so.
Uiobject getByTextAndClass(String text, String className) {
return new UiObject(new UiSelector().text(text).className(className));
}
然后,如果你试图调用此方法的计算器按钮3号就可以了:
And then if you are trying to call this method for the Calculator button with number 3 on it:
getByTextAndClass("3", android.widget.Button.class.getName()).click();
您可以使用UiAutomatorViewer工具:{Android的SDK} /tools/uiautomator.bat检查类名和不同UiObjects的其他属性。
You can use the UiAutomatorViewer tool: {android-sdk}/tools/uiautomator.bat to check the classnames and other attributes of different UiObjects.
这工作对我的4.2.2的设备,但我下载4.1.2对有作为对其进行测试。
This works on my 4.2.2 devices, but I am downloading 4.1.2 to test it on there as well.
我试了一下在4.1.2 AVD和它的作品在我的Windows机器上。
I tried it on a 4.1.2 AVD and it works on my Windows machine.
这篇关于如何preSS使用谷歌UiAutomator两次按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!