How to scroll horizontally knowing that the required tab has no unique ID (Appium using Java)



  • 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?



    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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2