2

機能テストでバグを再現しようとして、Robotiumで問題が発生しました。サインイン中にユーザー名フィールドからパスワードフィールドに移動するときに、アクティビティが「次へ」キーを適切に処理するように設定されていませんでした。少しグーグルしていて、解決策を思い付くことができませんでした。GalaxyNexusでこれを試しました。

    solo.clearEditText(0);
    solo.enterText(0, Constants.TEST_ACCOUNT_1.getUsername());
    solo.clickOnEditText(0);
    solo.clickOnScreen(672,1132);
    solo.clickOnEditText(0);
    solo.sleep(15000);
    solo.enterText(1, Constants.TEST_ACCOUNT_1.getPassword());

テキストフィールドをクリックしてキーパッドを上げてから次のボタンをクリックしようとしますが、テキスト編集フィールドをクリックしてもキーパッドは上がりません。また、Enterキーを送信しようとしましたが、FLAG_EDITOR_ACTIONを使用してEnterキーを送信しようとしましたが、どちらも「次の」キーをシミュレートしていません。ヘルプ!

4

2 に答える 2

6

Robotiumには、キーボードを表示する方法がないようです。clickOnText()さらに、ソフト キーボード ボタンが表示されていても押さないため、Robotium が操作できる範囲外でキーボードが実行されているように見えます。したがって、この答えは少しハックになるでしょう。

このソリューションには 2 つの重要な部分があります。まず、dispatchKeyEvent他のキーボード ボタンのように IME Next ボタンを直接クリックすることはできませんが、 を使用してそのコールバックをトリガーできますEditText.onEditorAction(EditorInfo.IME_ACTION_NEXT)。これにより、次の EditText にスキップできます。次に、このコールバックのトリガーは「UI との対話」のカテゴリに分類されるため、呼び出しを行うには、Robotium を実行しているスレッドからメイン スレッドに戻る必要があります。Activity.runOnUiThread()これを達成するために使用します。

これが私にとってどのように機能したかの例です:

public void testImeNext() throws Exception
{
    //Grab a reference to your EditText.  This code grabs the first Edit Text in the Activity
    //Alternatively, you can get the EditText by resource id or using a method like getCurrentEditTexts()
    //Make sure it's final, we'll need it in a nested block of code.
    final EditText editText = solo.getEditText(0);

    //Create a runnable which triggers the onEditorAction callback
    Runnable runnable = new Runnable() 
    {
        @Override
        public void run() 
        {
            editText.onEditorAction(EditorInfo.IME_ACTION_NEXT);
        }
    };

    //Use Solo to get the current activity, and pass our runnable to the UI thread.
    solo.getCurrentActivity().runOnUiThread(runnable);
}
于 2013-01-14T18:49:55.200 に答える
4

他の答えに基づいて、IME ボタンの押下をシミュレートし、UI スレッドの要求が完了するまで返らないメソッドを作成しました。

/**
 * This will send the NEXT action to simulate pressing next on the keyboard.
 * Because it has to be run on the UI thread we use a barrier to block and
 * stop this request returning until the action is complete.
 **/
private void sendIMENext(final EditText editText) throws Exception {
    final CyclicBarrier barrier = new CyclicBarrier(2);

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            editText.onEditorAction(EditorInfo.IME_ACTION_NEXT);

            try {
                barrier.await();
            }
            catch (Exception e) {
                Log.e("MainActivityTest", "Interupted on UI thread pressing IME next", e);
            }
        }
    };

    //Use Solo to get the current activity, and pass our runnable to the UI thread.
    solo.getCurrentActivity().runOnUiThread(runnable);
    // Waits until the barrier is met in the runnable
    barrier.await();
}

これは常に返されるはずですが、必要に応じて 2 番目のawaitにタイムアウトを追加できます。

于 2013-10-08T17:04:00.733 に答える