0

What I want to do is scrolling horizontally in a tab that doesn't have a unique ID. I have code how to scroll using id and textmatches like this example:

driver.findElement (MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()
.resourceId(" + Container +"))
.setAsHorizontalList().scrollIntoView("+ "new UiSelector()
.textMatches(" + Textmatch + ").instance(0))"));

So what should I do if I don't have the resource-id and textMatches?

2 Answers 2

0
  1. You can use mobile:shell command to swipe to the given direction like:

    int starX=0;
    int endX=0;
    int startY=0; 
    int endY=0;
    //change the above values according to your swipe direction
    Map<String, Object> args = new HashMap<>();
    args.put("command", "input");
    args.put("args", Lists.newArrayList("swipe", startX,
    startY, endX, endY));
    
    while (driver.findElements(By.xpath("your element locator here")).size() == 0) {
        driver.executeScript("mobile: shell", args);
    }
    
  2. You can use mobile:swipe command like:

    JavascriptExecutor js = (JavascriptExecutor) driver;
    Map<String, Object> params = new HashMap<>();
    params.put("direction", "down");
    params.put("element", yourElement.getId());
    js.executeScript("mobile: swipe", params);
    
  3. You can go for SwipeWhileNotFound command (it's provided by SeeTest - Appium Extension) like:

    seetest.swipeWhileNotFound("Down", 0, 2000, "NATIVE", "seetest.swipeWhileNotFound("Down", 0, 2000, "NATIVE", "xpath=your_element_locator", 0, 1000, 5, true)", 0, 1000, 5, true)
    

Going forward consider adding to your question essential information regarding source and target views, preferably via Layout Inspector hierarchy dumps - this way we'll be able to come up with more precise answers instead of generic ones.

0

In addition to Dmitri's methods, you can also use TouchActions, as I do in my code:

/**
 * This method scrolls based upon the passed parameters
 * @author Bill Hileman
 * @param int startx - the starting x position
 * @param int starty - the starting y position
 * @param int endx - the ending x position
 * @param int endy - the ending y position
 */
@SuppressWarnings("rawtypes")
public void scroll(int startx, int starty, int endx, int endy) {

    TouchAction touchAction = new TouchAction(driver);

    touchAction.longPress(PointOption.point(startx, starty))
               .moveTo(PointOption.point(endx, endy))
               .release()
               .perform();

}

/**
 * This method does a swipe upwards
 * @author Bill Hileman
 */
public void scrollDown() {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting y location set to 80% of the height (near bottom)
    int starty = (int) (size.height * 0.80);
    //Ending y location set to 20% of the height (near top)
    int endy = (int) (size.height * 0.20);
    //x position set to mid-screen horizontally
    int startx = (int) size.width / 2;

    scroll(startx, starty, startx, endy);

}

/**
 * This method does a swipe left
 * @author Bill Hileman
 */
public void swipeLeft() {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting x location set to 95% of the width (near right)
    int startx = (int) (size.width * 0.95);
    //Ending x location set to 5% of the width (near left)
    int endx = (int) (size.width * 0.05);
    //y position set to mid-screen vertically
    int starty = size.height / 2;

    scroll(startx, starty, endx, starty);

}

/**
 * This method does a swipe right
 * @author Bill Hileman
 */
public void swipeRight() {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting x location set to 5% of the width (near left)
    int startx = (int) (size.width * 0.05);
    //Ending x location set to 95% of the width (near right)
    int endx = (int) (size.width * 0.95);
    //y position set to mid-screen vertically
    int starty = size.height / 2;

    scroll(startx, starty, endx, starty);

}

/**
 * This method does a swipe downwards
 * @author Bill Hileman
 */
public void scrollUp() {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting y location set to 20% of the height (near bottom)
    int starty = (int) (size.height * 0.20);
    //Ending y location set to 80% of the height (near top)
    int endy = (int) (size.height * 0.80);
    //x position set to mid-screen horizontally
    int startx = size.width / 2;

    scroll(startx, starty, startx, endy);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.