I am using Robotium to automate testing of an application. Is there a way I can display an alert box while executing a particular test case.
Thanks.
I am using Robotium to automate testing of an application. Is there a way I can display an alert box while executing a particular test case.
Thanks.
可能です、ほぼすべて可能ですが、答えを出す前に、これを行う正当な理由はありますか? テストで警告ボックスを開く正当な理由は簡単にはわかりませんが、あなたが最もよく知っているかもしれません。
Robotiumにはその方法があります。
solo.getCurrentActivity();
これを使用すると、Activity Context を取得でき、そのようなものを使用して、Android アクティビティで実行できるほとんどすべてのことを実行できます。ページhttp://developer.android.com/guide/topics/ui/dialogs.htmlは、ダイアログを作成する方法を教えてくれます。最初の行は currentActivity を取得するメソッドを呼び出していることに気付くでしょう。上記のロボティウムメソッド。
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(solo.getCurrentActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
これによりダイアログが作成され、dialogs .show() メソッドを呼び出して画面に表示します。
Robotium のテストは UI スレッドでは実行されないため、テスト メソッド内のコードは、最良の場合でも機能せず、最悪の場合にはスローやエラーが発生してテストが失敗します。
テスト メソッド内から UI を操作するには、UI スレッドでコードを実行する必要があります。これは、そのコードを Runnable 内に記述し、その Runnable をrunOnUiThread()
現在のアクティビティのメソッドに送信することで実行できます。Robotium の Solo クラスには、getCurrentActivity()
この実行を可能にするメソッドがあります。この手法を使用してトーストを表示する方法の例を次に示します。
public void testDisplayToastInActivity() throws Exception
{
Runnable runnable = new Runnable
{
@Override
public void run()
{
Toast.makeText(solo.getCurrentActivity(), "Hello World", Toast.LENGTH_LONG).show();
}
}
solo.getCurrentActivity().runOnUiThread(runnable);
}
runOnUiThread()
トースト以外の何かが必要な場合は、アラート ダイアログの作成など、アクティビティと対話する他の多くのアクションを実行するために使用できます。ただし、できるとしても、これを行うことはお勧めしません。Robotium やその他のテスト フレームワークは、アプリケーション コードの実行の正確性を判断するためのものであり、ユーザーが行う方法でアプリケーションと対話する以上のロジックや UI 変更動作を挿入するべきではありません。テストから出力を取得し、それらを Logcat またはファイルに記録すると、テストがよりクリーンになります。