Como escrever testes de IU Android

Olå. Nós do Projeto Avokado continuamos falando sobre autotesting no Android. Este artigo é uma visão geral e uma comparação das ferramentas existentes para escrever testes de IU.



Vamos começar lembrando como o processo de teste geralmente se parece. Vamos chamar de cliente a entidade que interage com o aplicativo. Para interagir com a aplicação, o cliente costuma ter vĂĄrias interfaces disponĂ­veis: API, API REST, CLI, GUI, etc. E se, por exemplo, APIs sĂŁo usados ​​por programas clientes, entĂŁo a GUI Ă© usada por humanos.



As expectativas do comportamento do aplicativo são descritas na especificação. O objetivo do teste é verificar se o comportamento do aplicativo estå de acordo com a especificação.





Para resolver este problema, um conjunto de casos de uso ou casos de teste Ă© registrado. SĂŁo sequĂȘncias de etapas que levam o produto ao estado desejado. Portanto, o processo de teste de um aplicativo Ă© o processo de aprovação em todos os casos de teste.



  - . ,   .     -.



(JUnit, Cucumber).  :



  • — (assertions).
  •    .    â€” ,   .
  •   .


  :



  • ;
  •  ;
  • ;
  • ;
  •   ;
  • .


,   , . ,   Android-, (Avito test runner, Marathon, Spoon, Fork) ,   .



API ,  GUI  â€” .





,  GUI-, UI-.    unit-,     .



GUI- . , . GUI- (Espresso, UiAutomator) «  Â», ,  ,    .



API   :



  • :   , , , .
  • API .
  •   .


, , Kakao Kaspresso.     .   â€” ,  â€” , .





  ,  .





Android- :



  • Espresso.
  • UiAutomator.
  • Robotium.
  • Selendroid.


 Android Instrumentation Framework â€” API Android    .  â€” Espresso UiAutomator. Google.   . .



UiAutomator





UiAutomator  Android SDK  16 API. GUI-     : , , , , .   ,    â€” Ui Automator Viewer.



UiAutomator   (black-box).       . ,     ,   .   , , :



  • ;
  • ;
  • ;
  • , ;
  • .


UiAutomator   AccessibilityService,     . AccessibilityService ,       .   ,   AccessibilityNodeInfo.   View: , .





View AccessibilityEventSource Binder IPC AccessibilityManagerService   ,  , ,   . AccessibilityManagerService     AccessibilityService ,   , UiAutomator, .      AccessibilityService’ UiAutomator’a    . View,       .



  UiAutomator   “UI Automator deep diving”.



UiAutomator:



  • , Binder IPC, .  , UiAutomator ,   . ,     .   ,     .
  • , View   OpenGL Unity,  UiAutomator’a,   . , ,      UiAutomator’a .


,   UiAutomator   ,   .     Espresso.



Espresso





 UI-  Google,     (white-box). Espresso Android API  10 , .   .



Espresso  .  ,    .



Espresso  â€”     (ViewMatcher),   (ViewAction) (ViewAssertion).





Espresso   Hamcrest.    â€” ,  ,   Espresso â€”   .    ,   , .  , .



@Test
public void espressoTest() {
    onView(allOf(allOf(withId(R.id.label_bf_hotelname), 
        isDescendantOfA(withId(R.id.custom_view_trip_review))), 
        isDescendantOfA(withId(R.id.contentView))))
        .check(matches(
            withEffectiveVisibility(Visibility.VISIBLE)
        ));
}


 Espresso  â€” Espresso   ,   .   .  ,   (idle).





,  ,  , , check ViewInteraction. ViewAssertion   idle’.



public ViewInteraction check(ViewAssertion viewAssertion) {
    // (...)
    runSynchronouslyOnUiThread(new Callable<Void>() {
        @Override
        public Void call() {
            uiController.loopMainThreadUntilIdle();
            // (...)
            viewAssertion.check(...)
        }
    });
}


  Espresso:



  •   .
  •     . , , .   Espresso .     UiAutomator.
  • API Espresso , .   .
  • Espresso        .


,   , Espresso â€” , .



Robotium Selendroid



Robotium Selendroid   Espresso UiAutomator. C , .   ,     .



Robotium    Android API 8+, (  WebView , ,  API 15),    Java. Robotium Recorder  IntelliJ IDEA Android Studio.



Selendroid API â€”  10  19. WebDriver, Inspector   record-and-playback-.



 , Robotium Selendroid.



Robolectric



 , Robolectric   , . ,   Android UI-. Robolectric unit-   JUnit Android API.



Android SDK, shadow-. Robolectric   , inflate view, , , -  Android-. Robolectric ,  Android,   ,   JVM. , .





,     . , :     - . -  API,   . ,     . ,     , ,  Espresso UiAutomator.



.  API   ,   .



, ,  .



.  UI-  ,   .     ( . , ).   ,   . .   , .



. ,   , ,   .     , ,   UI-.



.    ,   , .    black-box . . ,   , black-box , ,   .       white-box, black-box .



.   ,   . , . , ,     .



. , . : , , ,  â€”   ,  , . ,   , , ,     .   stacktrace’  , , , , .



. . ,     . ,   .



adb.    adb: / , ,     .



API.  â€” , .   , , ,  â€”   ,   .   ,   ,     .   ,  .



  .



Appium





Appium — - open source   Android iOS. Appium  Selenium WebDriver,  web-.     . Appium-  ,  .   Appium   API.





.  , Appium .   HTTP-  Node.JS, WebDriver-,    .  ,  ,   .   ,  , Appium .



. Appium  ,   . Android     UiAutomator,     Espresso.  ,  ,     . . Espresso UiAutomator.



API.   Appium   API,  WebDriver. Appium  ,   .



.   , ,   , , ( ),  Appium-, , , ,   .   , .  â€” .



.   , .



.   , .



adb. -  Appium’  ,   adb-. , , .



. Appium .  â€” Espresso . -   ,      .



, Appium â€” Android- . ,   Kotlin,    Java. -  Appium   . ,   .   : «  . , , », «  Â».



Kakao





Kakao â€” Kotlin DSL  Espresso.  Espresso .



API.   Kakao â€”   boilerplate-     :



  • KView â€” Kakao-  ,     . , Kakao , KEditText, KButton .. .
  • Screen â€”   - PageObject. Screen â€” ,   stateless- KView   . Screen   , , , . Screen’   ,       .


, ,   Espresso. :



@Test
public void espressoTest() {
    onView(allOf(allOf(withId(R.id.label_bf_hotelname),
        isDescendantOfA(withId(R.id.custom_view_trip_review))),
        isDescendantOfA(withId(R.id.contentView))))
        .check(matches(
            withEffectiveVisibility(Visibility.VISIBLE)
        ));
}


:



object MainScreen : Screen<MainScreen>() {
    val hotelName = KTextView { withId(R.id.label_bf_hotelname) }
}

@Test
fun kakaoTest() {
    MainScreen { 
        hotelName { 
            isVisible() 
        } 
    }
}


. Kakao —  , Appium,     . - . Espresso  , ,  â€”   , ..



. Kakao Espresso,       .



.    Kakao .   API  , Espresso-.  , , . - - .



.   ,  Espresso.   .



.   . .



adb. ,  Espresso.



. , Kakao â€” DSL    Espresso.   UiAutomator, API .     , ,   .



Barista





Barista â€” ,    Espresso.



API. Kakao, Espresso. Barista — ,   Espresso-.  :



  • helper-     ,   Espresso . ,     onView(withId(R.id.button)).perform(click())   Espresso clickOn(R.id.button).     clickListItem(R.id.list, 4).
  • Android- Assertions API.
  •   ,  .
  •    runtime permission’.
  • test rule’, ,   flaky-     shared preferences, ,  .


Barista  NestedScrollView,    Espresso,   . DSL ,    .



. FlakyTestRule . .       , .



. -  Espresso.



.     ,   Espresso . Espresso.



. ,   .



. .



adb. ,  Espresso.



. Barista    .  ,      , .         .



Kaspresso





-  UI-,  ,        .



API.   Kakao. Kaspresso Kakao DSL â€” Kotlin DSL   , Kakao   Screen’ KView.



@RunWith(AndroidJUnit4::class)
class OpenHomeScreenTest : TestCase() {

    @Test
    fun kaspressoTest() {
        before { ... }
        .after { ... }
        .run {
            step("1. Open Home screen") { 
                MainScreen {
                    openHomeScreenBtn.click()
                }
            }
            step("2. Check Home title") { 
                HomeScreen {
                    title.isVisible()
                }
            }
            step("3. Do some stuff") { ... }
        }
    }
}


Kaspresso API   ,   .   , (. How to write autotests).



. Kaspresso -   API  Espresso  UiAutomator. ,   , , . ,      ,   . Kaspresso Kautomator â€” Kakao-like  UiAutomator,    Espresso  UiAutomator .



object KakaoScreen : Screen<KakaoScreen>() {
    val title = KTextView { withText(titleText) }
    val btn = KButton { withId(R.id.button1) }
}

object KautomatorScreen : UiScreen<KautomatorScreen>() {
    val title = UiTextView { withText(titleText) }
    val btn = UiButton { withId(pkgName, R.id.button2) }
}

@Test
fun kaspressoTest() {
    KakaoScreen { 
        title.isVisible()
        btn.click()
    }
    KautomatorScreen {
        title.isVisible()
        btn.click()
    }
}


  device. ,   :



  • ;
  • ;
  •   ;
  • , , ;
  •   ;
  • ;
  • ;
  •   : back, home, recents.


Kaspresso   , .



. Kaspresso , .     , ,   ViewAction ViewAssertion,  , . ,   , , (. Configurator).



. Kaspresso   Espresso UiAutomator , ,   ScrollView, , . helper-. ,       flakySafely:



MainScreen {
    flakySafely {
        btn.click()
    }
}


.   ,   , .



.       . , Kaspresso , .   :



device.screenshots.take("MainScreen_step_1")


. Localization autotests.



adb. Kaspresso AdbServer â€” HTTP-,   adb-.  ,   ,   .      ,   adb-   . AdbServer   ,   device. API   adb-:



adbServer.performAdb("emu sms send +79111111111 $smsText")
adbServer.perfromShell("rm -f $filePath")


AdbServer , ,    adb.





Appium Kakao Barista Kaspresso
API − + ± +
+ − − +
− + − +
− ± ± +
− ± − +
− − − +
adb + − − +
A partir desta tabela, a resposta deve ser Ăłbvia. Se vocĂȘ estĂĄ começando a mergulhar no mundo do autoteste, o lugar mais fĂĄcil para começar Ă© com o Kaspresso. Ele resolverĂĄ para vocĂȘ muitos problemas que vocĂȘ certamente enfrentarĂĄ. Ele fornece uma API conveniente que inclui e estende Kakao, que oculta chamadas para Espresso, UiAutomator e AdbServer, o que expande muito seus recursos. Por Ășltimo, mas nĂŁo menos importante, ele contĂ©m pontos de extensĂŁo especiais para que vocĂȘ possa obter o comportamento desejado.



All Articles