如何在Appium中从左向右滑动?

时间:2018-07-06 17:36:00

标签: appium appium-android

由于不建议使用swipe(),因此无法从左向右滑动屏幕。我的应用程序中有4个横幅,我想滑动查看所有横幅。

5 个答案:

答案 0 :(得分:2)

这适用于所有方向:

枚举:

format.js { render: 'some/path' } # renders app/views/some/path.js.erb

实际代码:

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(endX, startY)
                    .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;

    }
}

希望这会有所帮助,

答案 1 :(得分:1)

尝试以下方法。它适用于Appium 1.16.0版本。 我创建了此方法,以根据屏幕上特定的元素位置向左或向右滑动。它需要3个参数 元素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();

答案 2 :(得分:0)

假设您创建了class DashboardController extends AbstractController { /** * @Route("/", name="dashboard") * * @param EntityManagerInterface $em * @return \Symfony\Component\HttpFoundation\Response */ public function index(EntityManagerInterface $em) 的{​​{1}}实例,则可以向左滑动:

driver

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

答案 3 :(得分:0)

这应该有效,

 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();

答案 4 :(得分:0)

这是针对iOS Mobile的:

//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());
			
	
			
		}