【问题标题】:How to swipe Left to Right in Appium?如何在 Appium 中从左向右滑动?
【发布时间】:2022-04-05 19:21:04
【问题描述】:

由于 swipe() 已被弃用,我无法从左向右滑动屏幕。我的应用中有 4 个横幅,我想滑动查看所有横幅。

【问题讨论】:

  • 使用触摸操作

标签: appium appium-android


【解决方案1】:

这适用于所有方向:

枚举:

public enum DIRECTION {
    DOWN, UP, LEFT, RIGHT;
}

实际代码:

public static void swipe(MobileDriver driver, DIRECTION direction, long duration) {
    Dimension size = driver.manage().window().getSize();

    int startX = 0;
    int endX = 0;
    int startY = 0;
    int endY = 0;

    switch (direction) {
        case RIGHT:
            startY = (int) (size.height / 2);
            startX = (int) (size.width * 0.90);
            endX = (int) (size.width * 0.05);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(endX, startY)
                    .release()
                    .perform();
            break;

        case LEFT:
            startY = (int) (size.height / 2);
            startX = (int) (size.width * 0.05);
            endX = (int) (size.width * 0.90);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(endX, startY)
                    .release()
                    .perform();

            break;

        case UP:
            endY = (int) (size.height * 0.70);
            startY = (int) (size.height * 0.30);
            startX = (size.width / 2);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(startX, endY)
                    .release()
                    .perform();
            break;


        case DOWN:
            startY = (int) (size.height * 0.70);
            endY = (int) (size.height * 0.30);
            startX = (size.width / 2);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(startX, endY)
                    .release()
                    .perform();

            break;

    }
}

用法:

swipe(driver,DIRECTION.RIGHT);

希望这会有所帮助,

【讨论】:

  • 还没有测试,但是看起来UP和Down不太正确
  • 对于最新的 Appium java 客户端 API,TouchAction.press() 方法只接受 PointerOption 而不是 ints。如果您在那里遇到问题,请查看@Harjeet 的答案。这对我有用。
【解决方案2】:

试试下面的方法。它适用于 Appium 1.16.0 版本。 我创建了这个方法来根据屏幕上的特定元素位置向左或向右滑动。它需要3个参数 Element X:需要滑动触摸的元素的X坐标。 元素Y:元素的Y坐标。 方向:左/右

    //method to left and right swipe on the screen based on coordinates
public void swipeAction(int Xcoordinate, int Ycoordinate, String direction) {
    //get device width and height
    Dimension dimension = driver.manage().window().getSize();
    int deviceHeight = dimension.getHeight();
    int deviceWidth = dimension.getWidth();
    System.out.println("Height x Width of device is: " + deviceHeight + " x " + deviceWidth);

    switch (direction) {
        case "Left":
            System.out.println("Swipe Right to Left");
            //define starting and ending X and Y coordinates
            int startX=deviceWidth - Xcoordinate;
            int startY=Ycoordinate; // (int) (height * 0.2);
            int endX=Xcoordinate;
            int endY=Ycoordinate;
            //perform swipe from right to left
            new TouchAction((AppiumDriver) driver).longPress(PointOption.point(startX, startY)).moveTo(PointOption.point(endX, endY)).release().perform();
            break;

        case "Right":
            System.out.println("Swipe Left to Right");
            //define starting X and Y coordinates
            startX=Xcoordinate;
            startY=Ycoordinate;
            endX=deviceWidth - Xcoordinate;
            endY=Ycoordinate;
            //perform swipe from left to right
            new TouchAction((AppiumDriver) driver).longPress(PointOption.point(startX, startY)).moveTo(PointOption.point(endX, endY)).release().perform();
            break;
    }
}

获取元素 X,Y 坐标。试试下面的方法

int elementX= driver.findElement(elementLocator).getLocation().getX();
int elementY= driver.findElement(elementLocator).getLocation().getY();

【讨论】:

    【解决方案3】:

    假设您创建了AndroidDriverdriver 实例,您可以向左滑动:

        // Get location of element you want to swipe
        WebElement banner = driver.findElement(<your_locator>);
        Point bannerPoint = banner.getLocation();
        // Get size of device screen
        Dimension screenSize = driver.manage().window().getSize();
        // Get start and end coordinates for horizontal swipe
        int startX = Math.toIntExact(Math.round(screenSize.getWidth() * 0.8));
        int endX = 0;
    
        TouchAction action = new TouchAction(driver);
        action
                .press(PointOption.point(startX, bannerPoint.getY()))
                .waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
                .moveTo(PointOption.point(endX, bannerPoint.getY()))
                .release();
        driver.performTouchAction(action);
    

    使用最新的appium-java-client 6.1.0 和 Appium 1.8.x 服务器

    【讨论】:

      【解决方案4】:

      这应该可行,

       Dimension size = driver.manage().window().getSize();
                 System.out.println(size.height+"height");
                 System.out.println(size.width+"width");
                  System.out.println(size);
                  int startPoint = (int) (size.width * 0.99);
                  int endPoint = (int) (size.width * 0.15);
                  int ScreenPlace =(int) (size.height*0.40);          
                  int y=(int)size.height*20;
      
                 TouchAction ts = new TouchAction(driver);
                 //for(int i=0;i<=3;i++) {
                 ts.press(PointOption.point(startPoint,ScreenPlace ))
                .waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
                .moveTo(PointOption.point(endPoint,ScreenPlace )).release().perform();
      

      【讨论】:

        【解决方案5】:

        这适用于 iOS 移动:

        //Here i am trying to swipe list of images from right to left 
        //First i am getting parent element (table/cell)  id
        //Then using predicatestring am searching for the element present or not then trying to click
        
        List<MobileElement> ele = getMobileElement(listBtnQuickLink).findElements(By.xpath(".//XCUIElementTypeButton"));
        		
        		for(int i=1 ;i<=20;i++) {
        			MobileElement ele1 = ele.get(i);
        			
        			String parentID = getMobileElement(listBtnQuickLink).getId();
        			HashMap<String, String> scrollObject = new HashMap<String, String>();
        			scrollObject.put("element", parentID); //This is parent element id (not same element)
        			 
        			scrollObject.put("predicateString", "label == '"+ele1.getText()+"'");
        			scrollObject.put("direction", "left");
        			driver.executeScript("mobile:swipe", scrollObject);  // scroll to the target element
        			 
        			System.out.println("Element is visible : "+ele1.isDisplayed());
        			
        	
        			
        		}

        【讨论】:

          【解决方案6】:

          不幸的是,我注意到 TouchAction 在带有 Selenium 4 的 Android 11 上不起作用。所以如果你使用 Selenide 和 Appium,你可以试试这个:

          public class SwipeToLeft implements Command<SelenideElement> {
          @Nullable
          @Override
          public SelenideElement execute(SelenideElement proxy, WebElementSource locator, @Nullable Object[] args) throws IOException {
              Selenide.sleep(2000);
              var driver = WebDriverRunner.getWebDriver();
              var element = proxy.getWrappedElement();
              ((JavascriptExecutor) driver).executeScript("mobile: swipeGesture", ImmutableMap.of(
                      "elementId", ((RemoteWebElement) element).getId(),
                      "direction", "left",
                      "percent", 0.75
              ));
              return proxy;
          }
          

          }

          然后你可以使用:

          $('your seleniumLocator').shouldBe(visible).execute(new SwipeToLeft());
          

          【讨论】:

            猜你喜欢
            • 2019-10-14
            • 2010-10-10
            • 1970-01-01
            • 1970-01-01
            • 2019-05-26
            • 2013-03-17
            • 1970-01-01
            • 2011-03-12
            • 1970-01-01
            相关资源
            最近更新 更多