xiaocong / uiautomator

Python wrapper of Android uiautomator test tool.
MIT License
2.04k stars 645 forks source link

LongClick does not seem to work #313

Open dah-fari7009 opened 2 years ago

dah-fari7009 commented 2 years ago

Hi,

I am using uiautomator to explore an Android app programmatically ( org.liberty.android.fantastischmemo)

I am trying to perform a long click on a ListView to make a context menu visible, however it seems a click a list view item is performed instead. No context menu shows up and it moves to the next screen (which happens when clicking on a menu item) Here's the code I am using:

from uiautomator import Device
import sys

# get the target device
d = Device(sys.argv[1])

# check whether the resourceId exists
if d(resourceId=sys.argv[2]).exists:
    d(resourceId=sys.argv[2]).long_click()

After the long click, the app transitions from image to image

instead of image

Thank you!

dah-fari7009 commented 2 years ago

Update: I went around this by using swipe(steps=100) instead

unofficialdxnny commented 3 months ago

since you have reopened this i will give you some code for scrolling with uiautomator

import uiautomator2 as u2

def scroll_screen(device, direction='down', steps=10, duration=0.1):
    """
    Scrolls the screen in the specified direction for a given number of steps.

    Args:
        device (uiautomator2.Device): The device object to interact with.
        direction (str): The direction to scroll ('down', 'up', 'left', 'right'). Defaults to 'down'.
        steps (int): The number of scroll steps to perform. Defaults to 10.
        duration (float): The duration of each swipe in seconds. Defaults to 0.1.
    """
    # Get the device's screen size
    width, height = device.window_size()

    # Define start and end coordinates based on direction
    coords = {
        'down':  (width // 2, height * 3 // 4, width // 2, height // 4),
        'up':    (width // 2, height // 4, width // 2, height * 3 // 4),
        'left':  (width * 3 // 4, height // 2, width // 4, height // 2),
        'right': (width // 4, height // 2, width * 3 // 4, height // 2)
    }

    start_x, start_y, end_x, end_y = coords.get(direction, coords['down'])

    # Perform the scroll action for the specified number of steps
    for _ in range(steps):
        device.swipe(start_x, start_y, end_x, end_y, duration=duration)
        print(f"Scrolled {direction} step {_ + 1}/{steps}")

# Example usage:
device = u2.connect()
scroll_screen(device, direction='down', steps=10, duration=0.1)