2017-03-04 4 views
0

私はEditTextと(EditTextの下の)ボタンで画面を持っています。必要条件は、ショーソフトキーボードがボタンの下になければならないときです。それをチェックするエスプレッソユニットテスト(またはアノテアテスト)を書くことは可能ですか?Android、エスプレッソ。どのようにソフトキーボードが見えていますか?

+0

私の回答はあなたの問題を解決するのに役立ちましたか? – stamanuel

+0

それは役に立ちません。あなたのテストは、ボタンが見えるときに合格するからです。しかし、私は、ボタンが見える場合とボタンの下の境界がソフトキーボードの上にある場合のみ、パスをテストする必要があります。 – Alexei

+0

ok、キーボードがボタンの上にわずかながらもテストに合格することを意味しますか?これはアンドロイドエスプレッソが90%の可視性ルールを持っているからです(=ビューアイテムの90%が見える場合はテストに合格します)。あなたはこれを上書きすることができ、ボタンは100%可視性を持たなければならない – stamanuel

答えて

0

アンドロイドキーボードはあなたのアプリではなくシステムの一部です。エスプレッソでは十分ではありません。

私は私のテスト活動に次のレイアウト作成:あなただけのエスプレッソ汚れたソリューションを使用したい場合だろう

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.masta.testespressoapplication.MainActivity"> 

    <EditText 
     android:id="@+id/edittext" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="TEST" /> 

</RelativeLayout> 

を:

@Test 
public void checkButtonVisibilty2() throws Exception { 
    onView(withId(R.id.edittext)).perform(click()); 

    try { 
     onView(withId(R.id.button)).perform(click()); 
     throw new RuntimeException("Button was there! Test failed!"); 
    } catch (PerformException e) { 
    } 
} 

このテストでは、ボタンをクリックしようとするだろうこれは、実際にはソフトキーボードをクリックするので、PerformExceptionをスローします。これは許可されていません。 しかし、私はこの方法をお勧めしません、それはエスプレッソのフレームワークのかなりの誤用です。

ソリューション少し良く私見では、AndroidのUI Automatorのを使用して次のようになります。

@Test 
public void checkButtonVisibilty() throws Exception { 
    onView(allOf(withId(R.id.edittext), isDisplayed())).perform(click()); 

    UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 
    UiObject button = mDevice.findObject(new UiSelector().resourceId("com.example.masta.testespressoapplication:id/button")); 

    if (button.exists()) { 
     throw new RuntimeException("Button is visible! Test failed!"); 
    } 
} 

これは、ボタンのUI要素を取得し、それが現在の画面内に存在するかどうかを確認しようとするAndroidのUIのAutomatorを使用しています。

// Set this dependency to build and run UI Automator tests 
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' 
androidTestCompile 'com.android.support:support-annotations:25.2.0' 

一般的な思考:テストのこの種はそうuは、この追加Gradleの輸入を必要とするAndroidのUIのAutomatorのために

(あなたのケースのためのもので呼び出す「RESOURCEID」のパッケージとidを置き換えます)あなたがソフトキーボードを実際に制御することができず、それがどのように見えるかということから、非常にエラーが発生しやすいので、注意して使用します。

関連する問題