How to use moveTo method of io.appium.java_client.TouchAction class

Best io.appium code snippet using io.appium.java_client.TouchAction.moveTo

Run io.appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

AppiumUtils.java

Source: AppiumUtils.java Github

copy
1package com.appiumair.util;
2
3import io.appium.java_client.PerformsTouchActions;
4import io.appium.java_client.TouchAction;
5import io.appium.java_client.android.AndroidBatteryInfo;
6import io.appium.java_client.android.AndroidDriver;
7import io.appium.java_client.touch.WaitOptions;
8import io.appium.java_client.touch.offset.PointOption;
9import org.openqa.selenium.WebDriver;
10
11import java.text.DecimalFormat;
12import java.text.NumberFormat;
13import java.time.Duration;
14import java.util.ArrayList;
15import java.util.HashMap;
16import java.util.List;
17import java.util.Map;
18
19/**
20 * Appium各种操作工具类
21 */
22public class AppiumUtils {
23    public AppiumUtils() {}
24
25    /**
26     * @param driver
27     * @module 上滑操作
28     */
29    public static void swipeUp(WebDriver driver) {
30        int width = driver.manage().window().getSize().width;
31        int height = driver.manage().window().getSize().height;
32        TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
33        PointOption pointOption = PointOption.point(width / 2, height * 3 / 4);
34        touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width / 2, height / 4)).release().perform();
35    }
36
37    /**
38     * @param driver
39     * @module 下滑操作
40     */
41    public static void swipeDown(WebDriver driver) {
42        int width = driver.manage().window().getSize().width;
43        int height = driver.manage().window().getSize().height;
44        TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
45        PointOption pointOption = PointOption.point(width / 2, height / 4);
46        touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width / 2, height * 3 / 4)).release().perform();
47    }
48
49    /**
50     * @param driver
51     * @module 左滑操作
52     */
53    public static void swipeLeft(WebDriver driver) {
54        int width = driver.manage().window().getSize().width;
55        int height = driver.manage().window().getSize().height;
56        TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
57        PointOption pointOption = PointOption.point(width * 4 / 5, height / 2);
58        touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width / 5, height / 2)).release().perform();
59    }
60
61    /**
62     * @param driver
63     * @module 右滑操作
64     */
65    public static void swipeRight(WebDriver driver) {
66        int width = driver.manage().window().getSize().width;
67        int height = driver.manage().window().getSize().height;
68        TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
69        PointOption pointOption = PointOption.point(width / 5, height / 4);
70        touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width * 4 / 5, height / 2)).release().perform();
71    }
72
73    /**
74     * 执行测试前,检查各设备电量情况,电量不足发出警告
75     * @param driver
76     * @return 电量状态是否允许可继续执行
77     */
78    public static Double checkBattery(AndroidDriver driver) {
79        String message = "";
80        double batteryLevel = driver.getBatteryInfo().getLevel();
81        String batteryState = driver.getBatteryInfo().getState().name();
82        return batteryLevel;
83    }
84
85    /**
86     * 执行测试前,检查网络状态
87     * @param driver
88     * @return 网络连接状态
89     */
90    public static String checkNetwork(AndroidDriver driver) {
91        List<String> networkList = new ArrayList<>();
92        String message = "";
93        boolean wiFiEnabled = driver.getConnection().isWiFiEnabled();
94        if(!true == wiFiEnabled) {
95            message = "ERROR";
96        }
97        if (true == wiFiEnabled) {
98            message = "WIFI";
99        }
100        return message;
101    }
102}
103
Full Screen

ApolloUtils.java

Source: ApolloUtils.java Github

copy
1package com.apollo.cradle.util;
2
3import java.time.Duration;
4
5import org.openqa.selenium.Dimension;
6import org.openqa.selenium.WebElement;
7import io.appium.java_client.AppiumDriver;
8import io.appium.java_client.TouchAction;
9import io.appium.java_client.touch.WaitOptions;
10import io.appium.java_client.touch.offset.PointOption;
11
12public class ApolloUtils<ScrollAmount> {
13	//this method works on scrollup(means scroll upwards to downwards)	
14	public static void verticalScroll(AppiumDriver<WebElement> driver) {
15		Dimension dimension = driver.manage().window().getSize();
16		Double startHeight = dimension.getHeight() * 0.5;
17		int scrollStart = startHeight.intValue();
18
19		Double endHeight = dimension.getHeight() * 0.2;
20		int scrollEnd = endHeight.intValue();
21
22		TouchAction action = new TouchAction(driver);
23		action.press(PointOption.point(0, scrollStart)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
24				.moveTo(PointOption.point(0, scrollEnd)).release().perform();
25	}
26
27	public static void verticalScrollMenu(AppiumDriver<WebElement> driver) {
28		Dimension dimension = driver.manage().window().getSize();
29		Double startHeight = dimension.getHeight() * 0.5;
30		int scrollStart = startHeight.intValue();
31
32		Double endHeight = dimension.getHeight() * 0.2;
33		int scrollEnd = endHeight.intValue();
34
35		Double width=dimension.width*0.75;
36		int widthE=width.intValue();
37		
38		TouchAction action = new TouchAction(driver);
39		action.press(PointOption.point(widthE, scrollStart)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
40				.moveTo(PointOption.point(widthE, scrollEnd)).release().perform();
41	}
42//this method works on scrollup(means scroll downwards to upwards)	
43	public static void verticalScrollup(AppiumDriver<WebElement> driver) {
44		Dimension dimension = driver.manage().window().getSize();
45		Double startHeight = dimension.getHeight() * 0.3;
46		int scrollStart = startHeight.intValue();
47
48		Double endHeight = (dimension.getHeight() * 0.9) - startHeight;
49		int scrollEnd = endHeight.intValue();
50		System.out.println("scrollStart:" + scrollStart);
51		System.out.println("endHeight:" + endHeight);
52		TouchAction action = new TouchAction(driver);
53		action.press(PointOption.point(0, scrollStart)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
54				.moveTo(PointOption.point(0, scrollEnd)).release().perform();
55	}
56	
57	@SuppressWarnings("hiding")
58	public class ApolloUtilswidth<ScrollAmount> {
59		public void verticalScrollwidth(AppiumDriver<WebElement> driver) {
60			Dimension dimension = driver.manage().window().getSize();
61			Double startwidth = dimension.getWidth() * 0.5;
62			int scrollStart = startwidth.intValue();
63
64			Double endwidth = dimension.getWidth() * 0.2;
65			int scrollEnd = endwidth.intValue();
66
67			TouchAction action = new TouchAction(driver);
68			action.press(PointOption.point(0, scrollStart)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
69					.moveTo(PointOption.point(0, scrollEnd)).release().perform();
70		}
71	}
72}
73
Full Screen

Term_and_Conditon.java

Source: Term_and_Conditon.java Github

copy
1package pages;
2
3import io.appium.java_client.AppiumDriver;
4import io.appium.java_client.MobileElement;
5import io.appium.java_client.TouchAction;
6import io.appium.java_client.android.AndroidElement;
7import io.appium.java_client.pagefactory.AndroidFindBy;
8import io.appium.java_client.pagefactory.AppiumFieldDecorator;
9import io.appium.java_client.touch.offset.PointOption;
10import org.openqa.selenium.support.PageFactory;
11
12public class Term_and_Conditon {
13    private AppiumDriver<MobileElement> driver;
14
15    public Term_and_Conditon(AppiumDriver<MobileElement> driver) {
16        this.driver = driver;
17        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
18
19    }
20
21    @AndroidFindBy(xpath = "(//android.widget.LinearLayout)[4]/android.widget.Button[2]")
22    private AndroidElement accept;
23//    @AndroidFindBy(xpath = "(//android.widget.LinearLayout)[4]/android.widget.SeekBar")
24//    private AndroidElement bar
25
26    public void accept_button() {
27
28        accept.click();
29
30    }
31
32}
33
34//    public  void seekbar(){
35//
36//        int start=bar.getLocation().getX();
37//        int y=bar.getLocation().getY();
38//        int moveTo=(int) (start+(bar.getSize().getWidth())*0.30);
39//        TouchAction action=new TouchAction(driver);
40//        action.press(PointOption.point(start,y)).moveTo(PointOption.point(moveTo,y)).release().perform();
41
42
43
44
45
46
47
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger moveTo code on LambdaTest Cloud Grid

Execute automation tests with moveTo on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)