Menu

Showing posts with label espresso. Show all posts
Showing posts with label espresso. Show all posts

Tuesday, August 11, 2020

[Android - Espresso] การกดปุ่ม Allow บนหน้าต่างขอ Permission โดยไม่จำเป็นต้องรู้ชื่อของ Permission
[Android - Espresso] How to Click Allow Permissions with No Need to Know Permission Name

การทำ UI test automation ด้วย Espresso บางครั้งเราต้องเปิดใช้งานกล้องถ่ายรูป ถ้าหากเปิดใช้งานจาก application ของเราเป็นครั้งแรก สิ่งหนึ่งที่เราต้องเจอแน่นอน คือ modal สำหรับขอ permission ให้ application ของเราสามารถใช้งานกล้องดังภาพข้างล่างนี้


ด้วยข้อจำกัดของ Espresso ที่ไม่สามารถทำ action กับ element ที่อยู่ภายนอก application ได้ เราจึงต้องใช้ UI automator ช่วยในการกดปุ่ม Allow แทน โค้ดข้างล่างนี้จะกดปุ่ม Allow ให้เราโดยอัตโนมัติ ถ้าหากปุ่มดังกล่าวปรากฏอยู่
private val device: UiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

fun allowPermissionsIfNeeded() {
    if (Build.VERSION.SDK_INT >= 23) {
        val allowPermissions = device.findObject(UiSelector().text("Allow"))
        if (allowPermissions.exists()) {
            try {
                allowPermissions.click()
            } catch (e: UiObjectNotFoundException) {

            }
        }
    }
}
เนื่องจากเป็นการกดปุ่ม Allow โดยไม่สนใจว่า เป็นการขอ permission ใด จึงสามารถนำไปใช้กับการขอ permission อื่นๆได้ เช่น การเลือกรูปจาก gallery, การใช้งาน location เป็นต้น

สำหรับข้อมูลเพิ่มเติม สามารถอ่านได้ที่ https://stackoverflow.com/questions/33929937/android-marshmallow-test-permissions-with-espresso

Monday, August 3, 2020

[Android - Espresso] การจัดการ System Animation ของ Emulator
[Android - Espresso] Managing Emulator System Animations

การรัน Espresso โดยเปิด system animation เอาไว้ จะทำให้เกิด exception ดังนี้
androidx.test.espresso.PerformException: Error performing 'single click' on view 'Animations or transitions are enabled on the target device.
วิธีแก้ไข exception นี้ คือ ต้องปิด system animation ก่อนรัน Espresso Test

วิธีที่ง่ายที่สุดสำหรับการปิด system animation คือ รันคำสั่งต่อไปนี้ใน Terminal (Powershell สำหรับ Windows)
adb shell settings put global window_animation_scale 0.0
adb shell settings put global transition_animation_scale 0.0
adb shell settings put global animator_duration_scale 0.0
นอกจากการปิด system animation แล้ว ยังมีการตั้งค่าอื่นสำหรับ UI test automation อีก สามารถอ่านเพิ่มเติมได้ที่ https://testyour.app/blog/emulator

หมายเหตุ กรณีที่ทดสอบใน device จริง สามารถใช้คำสั่งข้างต้นเพื่อปิด system animation ได้เช่นกัน แต่ต้องเชื่อมต่อ device เข้ากับเครื่องที่รัน Espresso เสียก่อน

[Android - Espresso] สิ่งที่เราสามารถทำกับ View ได้
[Android - Espresso] View Action

View หมายถึง UI component ที่แสดงอยู่บน Application ใน Android

เราสามารถใช้ Espresso ในการเขียนจำลอง action ที่เราจะทำกับ view ที่ต้องการได้ ดังนี้
onView({component_selector}).perform({action})
ในกรณีที่ต้องการทำ action หลายอย่างต่อเนื่องกัน เราสามารถใช้คำสั่งดังนี้
onView({component_selector}).perform({action1}, {action2}, ..., {actionN})
โดยที่
  • {component_selector} เป็นคำสั่งสำหรับเลือก component ที่เราจะทำ action ด้วย
  • {action} เป็น action ที่เราจะทำกับ component ที่เราเลือก 

Friday, July 31, 2020

[Android - Espresso] ความแตกต่างระหว่าง Espresso และ UI Automator Testing Framework
[Android - Espresso] What is Different between Espresso and UI Automator Testing Framework

Espresso และ UI Automator ทั้ง 2 ตัวเป็น Testing framework สำหรับ Android เพื่อใช้ทดสอบ User Interface (UI) เหมือนกัน

สิ่งที่แตกต่างกัน คือ ความสามารถในการทดสอบ Application

  • Espresso มีความสามารถในการทำ action ได้เหมือนกับที่เราทำกับ application ของเรา รวมถึงการตรวจสอบค่าของ component ต่างๆ  จึงเหมาะสำหรับการทดสอบ UI ของ application ที่เราเขียนขึ้นมาเอง แต่มีข้อจำกัด คือ ไม่สามารถจำลองการกระทำบน Application อื่นหรือ system ได้ 
  • UI Automator มีความสามารถในการทำ action ได้เหมือนกับที่เราทำกับ device ของเรา จึงเหมาะสำหรับการทดสอบ UI ที่มีการใช้งานข้ามไปมาระหว่าง system และ application อื่น แต่มีข้อจำกัดคือ ไม่เหมาะกับการตรวจสอบค่าของ component 
หมายเหตุ เราสามารถใช้ Espresso และ UI Automator ร่วมกันในการเขียนเทสได้ 

[Android - Espresso] โปรแกรมใน Android SDK ที่จำเป็นต้องใช้ในการเขียนเทสด้วย Espresso Framework
[Android - Espresso] Necessary Programs in Android SDK that Use to Write Test in Espresso Framework

โปรแกรมใน Android SDK ที่จำเป็นต้องใช้ในการเขียนเทส ประกอบด้วยโปรแกรมดังนี้
  • Android Debug Bridge (adb) เป็น command-line tool ที่ช่วยให้เราสามารถติดต่อสื่อสารกับอุปกรณ์ของเราได้ เช่น การรันคำสั่ง การติดตั้งแอพ เป็นต้น
    • Path ของ adb อยู่ที่ {android_sdk}/platform-tools/adb
    • คำสั่งที่ใช้บ่อยมีดังนี้
      • คำสั่งสำหรับแสดงรายการ devices ที่ connect อยู่กับเครื่องของเรา
        adb devices
      • คำสั่งสำหรับ map port จาก devices ที่ connect อยู่กับเครื่องของเรา
        adb reverse tcp:8081 tcp:8081
  • UI Automator viewer (uiautomatorviewer) เป็นเครื่องมือที่ช่วยสแกนและวิเคราะห์ UI component ที่แสดงอยู่บนหน้าจอ Device เราสามารถใช้เครื่องมือนี้ในการตรวจสอบ layout และ properties ของ component ที่เราต้องการได้
    • Path ของ uiautomatorviewer อยู่ที่ {android_sdk}/tools/bin/uiautomatorviewer
หมายเหตุ {android_sdk} บน Windows จะอยู่ที่ C:/Users/{username}/AppData/Local/Android/Sdk/
ตัวอย่างเช่น
C:\Users\puthipong\AppData\Local\Android\Sdk\platform-tools
C:\Users\puthipong\AppData\Local\Android\Sdk\tools\bin