How to use build method of io.appium.java_client.touch.LongPressOptions class

Best io.appium code snippet using io.appium.java_client.touch.LongPressOptions.build

Run io.appium automation tests on LambdaTest cloud grid

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

AppiumTest3.java

Source: AppiumTest3.java Github

copy
1package Appium;
2
3import java.io.File;
4import java.net.MalformedURLException;
5import java.net.URL;
6import java.time.Duration;
7import java.util.List;
8import java.util.concurrent.TimeUnit;
9
10import org.openqa.selenium.By;
11import org.openqa.selenium.WebDriver;
12import org.openqa.selenium.interactions.Actions;
13
14import org.openqa.selenium.remote.DesiredCapabilities;
15
16import io.appium.java_client.AppiumDriver;
17import io.appium.java_client.MobileBy;
18import io.appium.java_client.TouchAction;
19import io.appium.java_client.android.AndroidDriver;
20import io.appium.java_client.android.AndroidElement;
21import io.appium.java_client.android.AndroidTouchAction;
22import io.appium.java_client.android.nativekey.AndroidKey;
23import io.appium.java_client.android.nativekey.KeyEvent;
24import io.appium.java_client.remote.AndroidMobileCapabilityType;
25import io.appium.java_client.remote.MobileCapabilityType;
26import io.appium.java_client.service.local.AppiumDriverLocalService;
27import io.appium.java_client.service.local.AppiumServiceBuilder;
28import io.appium.java_client.service.local.flags.GeneralServerFlag;
29import io.appium.java_client.touch.LongPressOptions;
30import io.appium.java_client.touch.TapOptions;
31import io.appium.java_client.touch.WaitOptions;
32import io.appium.java_client.touch.offset.ElementOption;
33import io.appium.java_client.touch.offset.PointOption;
34
35import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
36
37public class AppiumTest3 {
38
39	public static void main(String[] args) throws MalformedURLException, InterruptedException {
40		// TODO Auto-generated method stub
41		
42		
43		
44		AppiumDriverLocalService  service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
45											.usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))
46											.withAppiumJS(new File("C:\\Users\\Sanjay\\AppData\\Local\\Programs\\Appium\\resources\\app\\node_modules\\appium\\build\\lib\\main.js"))
47											.withLogFile(new File("C:\\Users\\Sanjay\\Appium\\eclipse-workspace\\AppiumTest1\\src\\test\\resources\\Logs\\AppiumLogs\\Logs.log"))
48											.withArgument(GeneralServerFlag.LOCAL_TIMEZONE));
49		
50		service.start();
51		
52		DesiredCapabilities dc = new DesiredCapabilities();
53		dc.setCapability("deviceName","65f651f31e00");
54		dc.setCapability("platformName","Android");
55		dc.setCapability("automationName", "UiAutomator2");
56		dc.setCapability("appPackage","in.amazon.mShop.android.shopping");
57		dc.setCapability("appActivity","com.amazon.mShop.home.HomeActivity");
58		
59		//AppiumDriver<AndroidElement> driver = new AppiumDriver<AndroidElement>(new URL("https://137.0.0.1:4723/wd/hub"),dc);
60		AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),dc);
61		
62		
63		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
64		driver.findElement(By.xpath("//android.widget.ImageView[@content-desc=\"Continue in English\"]")).click();
65		driver.findElement(By.id("in.amazon.mShop.android.shopping:id/skip_sign_in_button")).click();;
66		driver.findElement(By.id("in.amazon.mShop.android.shopping:id/rs_search_src_text")).sendKeys("Mobile");
67		driver.findElement(By.id("in.amazon.mShop.android.shopping:id/search_right_cam_img")).click();
68		
69		driver.pressKey(new KeyEvent(AndroidKey.BACK));
70	
71		
72		
73		driver.findElement(By.id("in.amazon.mShop.android.shopping:id/rs_search_src_text")).click();
74		Actions action = new Actions(driver);
75		action.sendKeys("Mobile Phones").perform();
76		
77		driver.pressKey(new KeyEvent(AndroidKey.BACK));
78		Thread.sleep(2000);
79		
80		//Using Mobile By
81		driver.findElement(MobileBy.id("in.amazon.mShop.android.shopping:id/glow_subnav_label")).click();
82		driver.pressKey(new KeyEvent(AndroidKey.BACK));
83		
84		
85		
86		AndroidElement element= driver.findElement(By.xpath("//android.view.View[@content-desc='Fresh Fresh']"));
87		System.out.println(element.getAttribute("enabled"));
88		element.click();
89		Thread.sleep(3000);
90		driver.quit();
91		
92		
93		dc.setCapability("appPackage","com.google.android.dialer");
94		dc.setCapability("appActivity","com.google.android.dialer.extensions.GoogleDialtactsActivity");
95		driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),dc);
96		
97		Thread.sleep(1000);
98		driver.findElement(By.id("com.google.android.dialer:id/dialpad_fab")).click();
99		Thread.sleep(1000);
100			
101		//AndroidElement dialerFrame = driver.findElement(By.className("android.widget.FrameLayout"));
102		String phoneNumber ="9790594970";
103		String[] numbersText = {"zero","one","two","three","four","five","six","seven","eight","nine"};
104		
105		
106		for(int i=0;i<=9;i++)
107		{
108			
109			char digit = phoneNumber.charAt(i);
110			int digitInt = Integer.parseInt(Character.toString(digit));
111			System.out.println("com.google.android.dialer:id/"+numbersText[digitInt]);
112			driver.findElement(By.id("com.google.android.dialer:id/"+numbersText[digitInt])).click();
113			Thread.sleep(500);
114			
115		}
116		
117		//click Call button
118		//driver.findElement(By.id("com.google.android.dialer:id/dialpad_voice_call_button")).click();
119		
120		AndroidElement clearDiallerDigits = driver.findElement(By.id("com.google.android.dialer:id/deleteButton"));
121		
122		AndroidTouchAction touchAction = new AndroidTouchAction(driver);
123		touchAction.press( ElementOption.element(clearDiallerDigits)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))).moveTo(ElementOption.element(element));
124		
125		
126		
127		touchAction.longPress(LongPressOptions.longPressOptions().withElement(ElementOption.element(clearDiallerDigits)))
128		.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))).release().perform();
129		
130		/*
131		
132		AndroidTouchAction touchAction2 = new AndroidTouchAction(driver)
133				.longPress(LongPressOptions.longPressOptions().withElement(ElementOption.element(clearDiallerDigits)))
134				.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1))).perform();
135		
136		*/
137		
138		AndroidElement diapPadView = driver.findElement(By.id("com.google.android.dialer:id/dialpad_view"));
139		for(int i=0;i<=9;i++)
140		{
141			
142			char digit = phoneNumber.charAt(i);
143			int digitInt = Integer.parseInt(Character.toString(digit));
144			System.out.println("com.google.android.dialer:id/"+numbersText[digitInt]);
145			diapPadView.findElement(By.id("com.google.android.dialer:id/"+numbersText[digitInt])).click();
146			Thread.sleep(500);
147			
148		}
149		//driver.findElement(By.id("com.google.android.dialer:id/dialpad_voice_call_button")).click();
150		
151		//driver.pressKey(new KeyEvent(AndroidKey.BACK));
152		//driver.longPressKey(new KeyEvent(AndroidKey.FORWARD));
153		
154		TouchAction action1 = new TouchAction(driver);
155		action1.tap(PointOption.point(243,11)).perform();
156		action1.tap(TapOptions.tapOptions().withElement(ElementOption.element(driver.findElement(By.id("com.google.android.dialer:id/dialpad_voice_call_button"))))).perform();
157		
158		
159		
160		driver.quit();
161		service.stop();
162		
163	}
164
165}
166
Full Screen

ThirdTest.java

Source: ThirdTest.java Github

copy
1package com.classFirstTest;
2
3import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
4import static io.appium.java_client.touch.TapOptions.tapOptions;
5import static io.appium.java_client.touch.offset.ElementOption.element;
6import static io.appium.java_client.touch.offset.PointOption.point;
7
8import java.net.MalformedURLException;
9import java.net.URL;
10import java.time.Duration;
11
12import org.openqa.selenium.By;
13import org.openqa.selenium.Dimension;
14import org.openqa.selenium.remote.DesiredCapabilities;
15import org.testng.annotations.Test;
16
17import io.appium.java_client.TouchAction;
18import io.appium.java_client.android.AndroidDriver;
19import io.appium.java_client.android.AndroidElement;
20import io.appium.java_client.android.AndroidTouchAction;
21import io.appium.java_client.touch.WaitOptions;
22import io.appium.java_client.touch.offset.PointOption;
23
24public class ThirdTest {
25
26	static AndroidDriver<AndroidElement> driver;
27
28
29
30	@Test
31	public void mySecondTest() throws InterruptedException, MalformedURLException {
32
33		DesiredCapabilities caps = new DesiredCapabilities();
34
35		// Set your access credentials
36		caps.setCapability("browserstack.user", "sathishkumar93");
37		caps.setCapability("browserstack.key", "s14LDSWxcnzPK3LqoHzs");
38
39		// Set URL of the application under test
40
41		
42		caps.setCapability("app", "bs://c282c3e0c822c7bc2fbea4f890790cb9610410d5");
43
44
45
46
47		// Specify device and os_version for testing
48		caps.setCapability("device", "Google Pixel 3");
49		caps.setCapability("os_version", "9.0");
50
51		// Set other BrowserStack capabilities
52		caps.setCapability("project", "First Java Project");
53		caps.setCapability("build", "Java Android");
54		caps.setCapability("name", "Drag_Drop_Test");
55
56
57		// Initialise the remote Webdriver using BrowserStack remote URL
58		// and desired capabilities defined above
59
60		//AppiumDriver<MobileElement> mdriver = new IOSDriver<MobileElement>(new URL("http://hub.browserstack.com/wd/hub"), caps);
61		AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
62				new URL("http://hub.browserstack.com/wd/hub"), caps);
63
64
65//		AndroidElement animationelemt = driver.findElementByXPath("//android.widget.TextView[@content-desc=\"Animation\"]");
66//		tapAnElement(driver,animationelemt);
67		driver.findElement(By.xpath("//android.widget.TextView[@content-desc=\"Views\"]")).click();
68//		driver.findElement(By.xpath("//android.widget.TextView[@content-desc=\"Drag and Drop\"]")).click();
69//		AndroidElement source =driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1"));
70//		AndroidElement destine =driver.findElement(By.id("io.appium.android.apis:id/drag_dot_2"));
71//		DragAndDrop(driver, source, destine);
72		
73		ScrollAndClick(driver, "Radio Group");
74		driver.findElement(By.xpath("//*[@content-desc=\"Radio Group\"]")).click();
75		
76		Thread.sleep(20000);
77		driver.quit();
78
79	}
80
81
82
83	public void tapAnElement(AndroidDriver<AndroidElement> driver, AndroidElement elementparam) {
84
85		TouchAction ta = new AndroidTouchAction(driver);
86		ta.tap(tapOptions().withElement(element(elementparam))).perform();
87		
88	}
89
90	
91	public void SwipeAnElement(AndroidDriver driver, AndroidElement start, AndroidElement end) {
92
93		TouchAction ta = new AndroidTouchAction(driver);
94		ta.longPress(longPressOptions().withElement(element(start)).withDuration(Duration.ofSeconds(5))).moveTo(element(end)).release().perform();
95        
96
97	}
98
99	public void tapOnCoordinates(AndroidDriver driver, int x, int y) {
100
101		TouchAction ta = new AndroidTouchAction(driver);
102		ta.tap(point(x, y)).perform();
103
104
105	}
106
107	public void longPress(AndroidDriver driver, AndroidElement elementparam) {
108
109
110		TouchAction ta = new AndroidTouchAction(driver);
111		ta.longPress(longPressOptions().withElement(element(elementparam)).withDuration(Duration.ofSeconds(5))).perform();
112
113	}
114
115	public void DragAndDrop(AndroidDriver driver, AndroidElement source, AndroidElement destine) {
116
117		TouchAction ta = new AndroidTouchAction(driver);
118		ta.longPress(longPressOptions().withElement(element(source))).moveTo(element(destine)).release().perform();
119
120	}
121
122	public void ScrollAndClick(AndroidDriver driver, String textvalue) {
123
124		driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+textvalue+"\").instance(0))");
125
126	}
127
128	public void ScrollFindTheMobileElementAndClick(By by) {
129
130		while(driver.findElements(by).size()==0) 
131		{
132			Dimension dimensions = driver.manage().window().getSize();
133			Double screenHeightStart = dimensions.getHeight() * 0.5;
134			int scrollStart = screenHeightStart.intValue();
135			Double screenHeightEnd = dimensions.getHeight() * 0.2;
136			int scrollEnd = screenHeightEnd.intValue();
137			int center = (int)(dimensions.width*0.5);
138			TouchAction ta = new AndroidTouchAction(driver);
139			ta.press(PointOption.point(center, scrollStart)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(center, scrollEnd)).release().perform();
140
141		}
142
143
144	}
145}
Full Screen

SecondTest.java

Source: SecondTest.java Github

copy
1package com.classFirstTest;
2
3import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
4import static io.appium.java_client.touch.TapOptions.tapOptions;
5import static io.appium.java_client.touch.offset.ElementOption.element;
6
7import java.net.MalformedURLException;
8import java.net.URL;
9import java.time.Duration;
10
11import org.openqa.selenium.remote.DesiredCapabilities;
12import org.testng.annotations.AfterMethod;
13import org.testng.annotations.BeforeMethod;
14import org.testng.annotations.Test;
15
16import io.appium.java_client.TouchAction;
17import io.appium.java_client.android.AndroidDriver;
18import io.appium.java_client.android.AndroidElement;
19import io.appium.java_client.android.AndroidTouchAction;
20
21public class SecondTest {
22
23	AndroidDriver<AndroidElement> driver;
24
25	
26	
27
28
29
30	@Test
31	public void mySecondTest() throws InterruptedException, MalformedURLException {
32		
33		DesiredCapabilities caps = new DesiredCapabilities();
34
35		// Set your access credentials
36		caps.setCapability("browserstack.user", "sathishkumar93");
37		caps.setCapability("browserstack.key", "s14LDSWxcnzPK3LqoHzs");
38
39		// Set URL of the application under test
40
41		caps.setCapability("app", "bs://c282c3e0c822c7bc2fbea4f890790cb9610410d5");
42
43
44
45		// Specify device and os_version for testing
46		caps.setCapability("device", "Google Pixel 3");
47		caps.setCapability("os_version", "9.0");
48
49		// Set other BrowserStack capabilities
50		caps.setCapability("project", "First Java Project");
51		caps.setCapability("build", "Java Android");
52		caps.setCapability("name", "Second_test");
53
54
55		// Initialise the remote Webdriver using BrowserStack remote URL
56		// and desired capabilities defined above
57
58		//AppiumDriver<MobileElement> mdriver = new IOSDriver<MobileElement>(new URL("http://hub.browserstack.com/wd/hub"), caps);
59		AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
60				new URL("http://hub.browserstack.com/wd/hub"), caps);
61
62
63		AndroidElement animationelemt = driver.findElementByXPath("//android.widget.TextView[@content-desc=\"Animation\"]");
64		tapAnElement(driver,animationelemt);
65		Thread.sleep(30000);
66		driver.quit();
67
68	}
69
70
71
72	private void tapAnElement(AndroidDriver<AndroidElement> driver, AndroidElement elementparam) {
73
74		TouchAction ta = new AndroidTouchAction(driver);
75		ta.tap(tapOptions().withElement(element(elementparam))).perform();
76	}
77
78	private void SwipeAnElement(AndroidDriver driver, AndroidElement start, AndroidElement end) {
79
80		TouchAction ta = new AndroidTouchAction(driver);
81		ta.longPress(longPressOptions().withElement(element(start)).withDuration(Duration.ofSeconds(5))).moveTo(element(end)).release().perform();
82
83
84	}
85
86
87	
88	
89}
90
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

Most used method in LongPressOptions

Trigger build code on LambdaTest Cloud Grid

Execute automation tests with build 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)