How can I get the element ID for performing swipe actions on an element

1,767 views
Skip to first unread message

Rajas

unread,
Jun 3, 2013, 6:43:57 AM6/3/13
to appium-...@googlegroups.com
Hi,
Iam currently testing an Android App using appium with python. A certain screen of the app contains a list of items and swiping each item gives a pop-up menu. Iam locating each item using "driver.find_element_tag_name('TextView')" but the gestures.md in docs folder says that element ID is needed to be passed:
* `POST session:/sessionId/touch/swipe` - perform a swipe/drag on the screen or an element
    * URL Parameter: sessionId of session to route to
    * JSON parameters:
        * `touchCount` (optional, default `1`): how many fingers to flick with
        * `startX` (optional, default `0.5`): x coordinate where swipe begins (in pixels or relative units)
        * `startY` (optional, default `0.5`): y coordinate where swipe begins (in pixels or relative units)
        * `endX` (required): x coordinate where swipe ends (in pixels or relative units)
        * `endY` (required): y coordinate where swipe ends (in pixels or relative units)
        * `duration` (optional, default `0.8`): time (in seconds) to spend performing the swipe/drag
        * `element` (optional): ID of element to scope this command to

How shall I get the element ID  of the TextView element

Al Villaflor

unread,
Jun 3, 2013, 9:11:11 AM6/3/13
to appium-...@googlegroups.com
driver.find_element_tag_name('TextView').getID()

Jonathan Lipps

unread,
Jun 3, 2013, 2:14:29 PM6/3/13
to Rajas, appium-...@googlegroups.com
Given element el in python, id is el.id

--
http://appium.io
---
You received this message because you are subscribed to the Google Groups "Appium-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to appium-discus...@googlegroups.com.
Visit this group at http://groups.google.com/group/appium-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Vic Wong

unread,
Jun 19, 2013, 8:03:59 PM6/19/13
to appium-...@googlegroups.com, Rajas
I need some clarification about the "element" scope. My understanding from the documentation is that the swipe will only take place within the graphical dimensions of the specified element. What if I need to swipe beyond the boundaries of this element?

For example, say I have a graphical button that I need to slide upward 50 pixels to reveal a hidden menu. Do I need to know the absolute coordinates of the element to make it work? If so, how do I get the coordinates of a given element?

bootstrap online

unread,
Jun 19, 2013, 9:17:04 PM6/19/13
to Vic Wong, appium-...@googlegroups.com, Rajas
It's possible to calculate the relative position of an element. If you
use pixel values then that doesn't work when the screen changes.
Here's an example in Ruby.
https://github.com/appium/ruby_lib/blob/b4e592cc3d101a4ac9f7fa374ae8c75fcdc5218a/lib/appium_lib/common/patch.rb#L50
> Visit this group at http://groups.google.com/group/appium-discuss.

Vic Wong

unread,
Jun 20, 2013, 2:21:19 PM6/20/13
to appium-...@googlegroups.com, Vic Wong, Rajas
Thanks for the tips. I used WebElement.getLocation() to grab the coordinates of the element, then swipe based on that.

I tend to think of swiping in terms of swiping a given element in a specified direction rather than specific coordinates on the screen, so I came up with this helper method in case it's useful for anyone else:

    /**
     * Swipes an element a specified offset distance
     * X/Y Coordinates: distance in number of pixels, values < 1 specify relative screen offset distance 
     * (example: .75 swipes 75 percent of the width of screen)
     * @param elementDef - By object defining the element
     * @param touchCount - amount of fingers in touch
     * @param xDistance - positive:right, negative:left 
     * @param yDistance - positive:up, negative:down, 
     * @param duration - amount of seconds to swipe for
     */
    public void swipeElement(WebElement element, double touchCount, double xDistance, double yDistance, double duration) {
   
    // get location of element
    Point coords = element.getLocation();
    double startX = coords.x;
    double startY = coords.y;
   
    // get window size
    Dimension size = driver.manage().window().getSize();
   
    //calculate end distance
    double endX;
    double endY;
   
    //if the distance is a decimal value < 1, it's a relative screen distance
    if (Math.abs(xDistance) < 1) {
    endX = startX + (size.width * Math.abs(xDistance));
    }
    else {
    endX = startX + xDistance;
    }
    if (Math.abs(yDistance) < 1) {
    endY = startY - (size.width * Math.abs(yDistance));
    }
    else {
    endY = startY - yDistance;
    }
   
    Point endCoords = new Point((int)endX, (int)endY);
   
    util.log("Swiping from: " + coords + " To: " + endCoords);
   
    //set up arguments to mobile: swipe
   HashMap<String, Double> flickObject = new HashMap<String, Double>();
   flickObject.put("touchCount", touchCount);
   flickObject.put("startX", startX);
   flickObject.put("startY", startY);
   flickObject.put("endX", endX);
   flickObject.put("endY", endY);
   flickObject.put("duration", duration);

   driver.executeScript("mobile: swipe", flickObject);
    }

Maria Machlowska

unread,
Dec 9, 2013, 7:14:30 AM12/9/13
to appium-...@googlegroups.com, Vic Wong, Rajas
Vesrsion for facebook WebDriver (test in php):

/**
     * Swipes an element a specified offset distance
     * X/Y Coordinates: distance in number of pixels, values < 1 specify relative screen offset distance 
     * (example: .75 swipes 75 percent of the width of screen)
     * @param elementDef - By object defining the element
     * @param touchCount - amount of fingers in touch
     * @param xDistance - positive:right, negative:left 
     * @param yDistance - positive:up, negative:down, 
     * @param duration - amount of seconds to swipe for
        * @param element -  is the xpath string to the element on a creen
     */
    public swipeElement(RemoteWebDriver $driver, $element, $touchCount, $xDistance, $yDistance, $duration) {
   
    // get location of element
    $driver->findElement(WebDriverBy::xpath($element))->getLocation();
    $startX = getX();
    $startY = getY();
   
    // get window size
    $elementSize = $driver->getSize();
   
    //calculate end distance
   
   
    //if the distance is a decimal value < 1, it's a relative screen distance
    if (abs(xDistance) < 1) {
    $endX = $startX + ($elementSize->getWidth() * abs(xDistance));
    }
    else {
    $endX = $startX + $xDistance;
    }
    if (abs(yDistance) < 1) {
    $endY = $startY - ($elementSize->getWidth() * abs(yDistance));
    }
    else {
    $endY = $startY - $yDistance;
    }
   
   
   
    //set up arguments to mobile: swipe
 
               $params = array($touchCount, $startX, $startY, $endX, $endY, $duration);

         $executor = $driver->getExecutor();
          $executor->execute('swipe',$params);
    }



W dniu czwartek, 20 czerwca 2013 20:21:19 UTC+2 użytkownik Vic Wong napisał:
Thanks for the tips. I used WebElement.getLocation() to grab the coordinates of the element, then swipe based on that.

I tend to think of swiping in terms of swiping a given element in a specified direction rather than specific coordinates on the screen, so I came up with this helper method in case it's useful for anyone else:

    /**
     * Swipes an element a specified offset distance
     * X/Y Coordinates: distance in number of pixels, values < 1 specify relative screen offset distance 
     * (example: .75 swipes 75 percent of the width of screen)
     * @param elementDef - By object defining the element
     * @param touchCount - amount of fingers in touch
     * @param xDistance - positive:right, negative:left 
     * @param yDistance - positive:up, negative:down, 
     * @param duration - amount of seconds to swipe for
        * @param element -  is the xpath string to the element on a creen
     */
    public swipeElement(RemoteWebDriver $driver, $element, $touchCount, $xDistance, $yDistance, $duration) {
   
    // get location of element
    $driver->findElement(WebDriverBy::xpath($element))->getLocation();
    $startX = getX();
    $startY = getY();
   
    // get window size
    $elementSize = $driver->getSize();
   
    //calculate end distance
   
   
    //if the distance is a decimal value < 1, it's a relative screen distance
    if (abs(xDistance) < 1) {
    $endX = $startX + ($elementSize->getWidth() * abs(xDistance));
    }
    else {
    $endX = $startX + $xDistance;
    }
    if (abs(yDistance) < 1) {
    $endY = $startY - ($elementSize->getWidth() * abs(yDistance));
    }
    else {
    $endY = $startY - $yDistance;
    }
   
   
   
    //set up arguments to mobile: swipe
 
               $params = array($touchCount, $startX, $startY, $endX, $endY, $duration);

         $executor = $driver->getExecutor();
          $executor->execute('swipe',$params);
    }
Reply all
Reply to author
Forward
0 new messages