Can not drag widget while I am writing UI test for Android widget with UIAutomator

25 Views Asked by At

Hello I am trying to write UI test for widgets first time I found one library named UIAutomator, everything was good until UI test was trying to drag my widget to home screen. I managed to open widgets menu and select the correct app however I am not able to drag my widget to home screen.

class WidgetUITest {

private lateinit var device: UiDevice

@Before
fun setup() {
    device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
}

@Test
fun testWidgetFunctionality() {
    closeApp()

    // Open the Widgets pane and scroll to the widget in the widgets menu
    openWidgetsPaneAndScrollToWidget()

    // Drag the widget onto the screen
    dragWidgetOntoScreen()

    // Interact with the widget to conduct tests
    Espresso.onView(ViewMatchers.withText("Hello"))
        .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))

    // Remove the widget from the screen
    removeWidget()
}

private fun closeApp() {
    device.pressHome()
}

private fun openWidgetsPaneAndScrollToWidget() {
    // Long press the center of the screen to open the desktop popup
    val centerPoint = Point(device.displayWidth / 2, device.displayHeight / 2)
    device.swipe(arrayOf(centerPoint, centerPoint), 150)

    // Tap on the widgets button to open the widgets menu
    tapBySelector(By.text("Widgets"))

    // Scroll to the widget in the widgets menu
    scrollToObjectWithText()
}


private fun dragWidgetOntoScreen() {
    fun findObjectsWithRegex(regex: String): MutableList<UiObject2> {
        return findObjectsBySelector(By.res(Pattern.compile(regex)))
    }
    val widgetNames = findObjectsWithRegex(".*\\:id/widget_name")
    val centerPoint = Point(device.displayWidth / 2, device.displayHeight / 2)

    for (widgetName in widgetNames) {
        if (widgetName.text == "new") {
  
            widgetName.drag(centerPoint, 999) 
            //  Thread.sleep(5000)
            break
        }
    }
}



private fun removeWidget() {
    // Press home to close the app
    device.pressHome()

    // Find the bounds of the widget
    val widgetBounds = findObjectWithRegex(".*\\:id/widget_name").visibleBounds

    // Click the center point of the widget
    device.click(widgetBounds.centerX(), widgetBounds.centerY())

    // Tap on the remove text at the top of the screen to remove the widget
    tapBySelector(By.text("Remove"))
}

private fun tapBySelector(by: BySelector) {
    val obj = findBySelector(by)
    obj.click()
}

private fun findBySelector(by: BySelector): UiObject2 {
    return device.wait(Until.findObject(by), 15000.toLong())
        ?: throw UiObjectNotFoundException("Component not found - $by")
}

private fun scrollToObjectWithText() {
    // Create a UiScrollable for scrolling within the entire screen
    val textScroll = UiScrollable(UiSelector().scrollable(true)).setSwipeDeadZonePercentage(0.8)

    // Scroll into view the object with the specified text
    textScroll.scrollIntoView(UiSelector().text("My Applicationjk"))
    tapBySelector(By.text("My Applicationjk"))

}

private fun findObjectsBySelector(by: BySelector): MutableList<UiObject2> {
    return device.findObjects(by)
}

private fun findObjectWithRegex(regex: String): UiObject2 {
    val bySelector = By.res(Pattern.compile(regex))
    return device.wait(Until.findObject(bySelector), 15000.toLong())
        ?: throw UiObjectNotFoundException("Component not found - $bySelector")
}

I have problem with "dragWidgetOntoScreen" method, also if statement is true so it is not related to if statement. Thank you in advance

0

There are 0 best solutions below