私は、AndroidのCalculatorに '33'を入力するために、UiAutomatorを使用して次のスクリプトを用意しました。しかし、最初の「3」だけが受け入れられ、2番目のプレスは完全に無視されます。Google UiAutomatorを使用してボタンを2回押す方法を教えてください。
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秒間睡眠を追加しようとしました:
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
...しかし、それは何も変更しませんでした。
私はまた、すなわち、2「3年代の間に、別のボタンをクリックしてみました:
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)
編集、ラミの観察を以下、私はのために第2の要求に対して同じ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;
}
...しかし、それは一度だけ「[3]のための新しいオブジェクトを作成した」と言うので、それは、それは明らかに同じUIObjectクラスを毎回再利用されていても、動作しませんでした。しかし、これはどちらか私のために動作しませんでした
void click(UiObject target) throws UiObjectNotFoundException {
Rect rect = target.getBounds();
System.out.println("rect: " + rect);
getUiDevice().click(rect.centerX(), rect.centerY());
}
:
は、それから私は、関数を作成することによって、次のようにラミの観測以下、再び、「クリック」をUiDevice.click()「トリック」を試してみました最初の '3'だけが表示され、両方のクリックが明らかに同じ場所にあるにもかかわらず、2番目の文字は無視されます。rect:
の出力場所が同じであるためです。自分のデスクトップマウスを使用して手動で「3」を2回クリックすると、両方の3つが正常に表示されます。
また、クリック間に2秒追加してみましたが、それでも1つの「3」しか表示されませんでした。
これは素晴らしい回避策のように聞こえるが、私はちょうど今それを試してみました、そしてそれは私のために動作しませんです。 '3'ボタンを手動で2回クリックすると '33'が表示されますが、getUiDevice()。click()を使用して同じ場所で2回クリックするとUiAutomatorには1つの ' 3 '。これにより、2つのクリックの間に 'Thread.sleep(2000)'を追加します。 –
私は 'setUpDigit()'メソッドを作成しました。ここで私は自分のUIobjectを設定しました。 'UiObject zero = new UiObject(new UiSelector()。text(" 0 "));'そして私は、同じメソッドsetUpDigit() 'Rzero = zero.getBounds();'次に、グローバル値Rzeroを使用し、右ボタン 'getUiDevice()をクリックするスイッチがあります。click(Rzero.centerX()、Rzero.centerY ()); '難しいのはsetUpDigit()を呼び出すときです。電卓を持っているときに呼び出される必要があります。 –