Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LongClick does not seem to work #313

Open
dah-fari7009 opened this issue Mar 4, 2022 · 2 comments
Open

LongClick does not seem to work #313

dah-fari7009 opened this issue Mar 4, 2022 · 2 comments

Comments

@dah-fari7009
Copy link

dah-fari7009 commented Mar 4, 2022

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
Copy link
Author

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

@unofficialdxnny
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants