How to use verify method of io.appium.java_client.touch.offset.ElementOption class

Best io.appium code snippet using io.appium.java_client.touch.offset.ElementOption.verify

Run io.appium automation tests on LambdaTest cloud grid

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

apprunner.java

Source: apprunner.java Github

copy
1package com.demoApp.runner;
2
3import java.net.URL;
4
5import java.util.concurrent.TimeUnit;
6
7import org.openqa.selenium.Dimension;
8import org.openqa.selenium.remote.DesiredCapabilities;
9import org.openqa.selenium.support.ui.WebDriverWait;
10import org.testng.Assert;
11import org.testng.annotations.AfterSuite;
12//import org.testng.annotations.AfterSuite;
13import org.testng.annotations.AfterTest;
14import org.testng.annotations.BeforeSuite;
15//import org.testng.annotations.BeforeSuite;
16//import org.testng.annotations.BeforeSuite;
17import org.testng.annotations.BeforeTest;
18import org.testng.annotations.Test;
19
20
21import io.appium.java_client.touch.TapOptions;
22import io.appium.java_client.touch.offset.ElementOption;
23import org.openqa.selenium.By;
24
25
26import io.appium.java_client.AppiumDriver;
27import io.appium.java_client.MobileElement;
28import io.appium.java_client.TouchAction;
29//import io.appium.java_client.TouchAction;
30import io.appium.java_client.android.AndroidDriver;
31import io.appium.java_client.pagefactory.AndroidFindBy;
32import io.appium.java_client.service.local.AppiumDriverLocalService;
33
34import static io.appium.java_client.touch.TapOptions.tapOptions;
35import static io.appium.java_client.touch.offset.ElementOption.element;
36import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
37import static io.appium.java_client.touch.offset.PointOption.point;
38import static io.appium.java_client.touch.WaitOptions.waitOptions;
39import static java.time.Duration.ofMillis;
40
41public class apprunner {
42	public AppiumDriver<MobileElement> wd = null;
43	static AppiumDriverLocalService service;
44	
45	@BeforeSuite
46	public void startAppium() {
47		service = AppiumDriverLocalService.buildDefaultService();
48		service.start();
49	}
50	
51	@BeforeTest
52	public void setup() {
53		DesiredCapabilities capabilities = new DesiredCapabilities();
54		capabilities.setCapability("platformName", "Android");
55		capabilities.setCapability("platformVersion", "8.1");
56		capabilities.setCapability("appPackage", "io.appium.android.apis");
57		capabilities.setCapability("appActivity", ".ApiDemos");
58		capabilities.setCapability("deviceName", "Pixel 2");
59		capabilities.setCapability("app", "C:\\Users\\rahulmishra01\\Downloads\\ApiDemos-debug.apk");
60		
61		try {
62			wd = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
63		}catch(Exception e) {
64			e.printStackTrace();
65		}
66	}
67	
68	public MobileElement waitForElement(MobileElement element) {
69		try {
70			WebDriverWait wait=new WebDriverWait(wd, 60);
71		} catch(Exception e) {
72			System.out.println(element+"is not displayed on the web page");
73		}
74		return element;
75		
76	}
77	
78	//@Test
79	/*public void apidemoapp() throws Throwable {
80		wd.findElementByXPath("//android.widget.TextView[@content-desc=\"Graphics\"]").click();
81		Thread.sleep(5000);
82		
83		//Tap functionality
84		
85		TouchAction touch=new TouchAction(wd);
86		MobileElement Element=wd.findElementByAccessibilityId("Arcs");
87		touch.tap(tapOptions().withElement(element(Element))).release().perform();
88		Thread.sleep(5000);
89		
90		//Drag and Drop
91		/*TouchAction touch=new TouchAction(wd);
92		MobileElement Element1=wd.findElementByAccessibilityId("ColorFilters");
93		MobileElement Element2=wd.findElementByAccessibilityId("Arcs");
94		touch.longPress(longPressOptions().withElement(element(Element1))).moveTo(element(Element2)).release().perform();
95		Thread.sleep(5000);
96		*/
97		
98		//swipe
99		/*verticalswipeByPercentage(0.70,0.10,0.50);
100	}
101	
102	public void verticalswipeByPercentage(double startpercentage, double endpercentage, double anchorpercentage) throws Throwable {
103		Dimension size = wd.manage().window().getSize();
104		int anchor = (int)(size.width * anchorpercentage);
105		int startpoint = (int)(size.height * startpercentage);
106		int endpoint = (int)(size.height * endpercentage);
107		new TouchAction(wd).press(point(anchor,startpoint)).waitAction(waitOptions(ofMillis(1000))).moveTo(point(anchor,endpoint)).release().perform();
108		Thread.sleep(5000);
109		
110		
111	}*/
112	
113	/*
114	@AndroidFindBy(xpath = "//*[@text='Animation']")
115    public MobileElement lnkAnimation;
116
117    @AndroidFindBy(xpath = "//*[@text='App']")
118    public MobileElement lnkApp;
119    
120
121    @AndroidFindBy(xpath = "//*[@text='Animation/Default Layout Animations']")
122    public MobileElement lbldefaultlayoutanimations;
123
124    @AndroidFindBy(xpath = "//*[@text='ADD BUTTON']")
125    public MobileElement btnAddButton;
126
127	public boolean verifyAnimationicon() {
128        boolean displayAnimationflag = false;
129        try{
130            lnkAnimation.isDisplayed();
131            displayAnimationflag = true;
132        } catch (Exception e) {
133            System.out.println(e + ": Animation icon is not displayed!!");
134        }
135        return displayAnimationflag;
136    }
137	
138	public boolean verifyDefaultLaoyoutAnimationScreen() {
139        boolean defaultanimationscreenflag = false;
140        try{
141            lbldefaultlayoutanimations.isDisplayed();
142            defaultanimationscreenflag = true;
143        } catch (Exception e) {
144            System.out.println(e + ": Default Layout Animations screen is not displayed!!");
145        }
146        return defaultanimationscreenflag;
147    }
148	
149	/* public void iVerifyButtonDisplay(int buttons) {
150	        Assert.assertEquals(getbuttons(), buttons, "The no. of buttons displayed is invalid!!");
151	        //Log.info("The button " + buttons + " is displayed successfully!!");
152
153	    }*/
154	
155
156	   /* public int getbuttons() {
157	        return wd.findElements(By.xpath("//*[@resource-id='io.appium.android.apis:id/gridContainer']//android.widget.Button")).size();
158	    }
159	    */
160	
161	
162	@Test
163	public void animation_screen() {
164		
165		//Assert.assertTrue(verifyAnimationicon(), "Animation icon is not displayed!!");
166		
167		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Animation\"]"))))).perform();
168		
169		//Assert.assertTrue(verifyDefaultLaoyoutAnimationScreen(), "Default Layout Animations screen is not displayed!!");
170		
171		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Default Layout Animations\"]"))))).perform();
172		
173		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.Button[@content-desc=\"Add Button\"]"))))).perform();
174		
175		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.id("io.appium.android.apis:id/addNewButton"))))).perform();
176		
177		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.Button[@content-desc=\"Add Button\"]"))))).perform();
178		
179		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.id("io.appium.android.apis:id/addNewButton"))))).perform();
180	    
181		
182		//Assert.assertEquals(getbuttons(), buttons, "The no. of buttons displayed is invalid!!");
183		
184		wd.quit();
185	
186	}
187	
188	@Test
189	public void content_screen() {
190		
191		
192		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Content\"]"))))).perform();
193		
194		//new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.id("io.appium.android.apis:id/addNewButton"))))).perform();
195		
196		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Resources\"]"))))).perform();
197		
198		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Layout Reference\"]"))))).perform();
199		
200		wd.quit();
201		
202	}
203	
204	//@Test
205	public void content_screen_asset() {
206		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Content\"]"))))).perform();
207		
208		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Assets\"]"))))).perform();
209		
210		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Read Asset\"]"))))).perform();
211		
212		wd.quit();
213	}
214	
215	
216	@Test
217	public void preference_screen() {
218		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Preference\"]"))))).perform();
219		
220		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"4. Default values\"]"))))).perform();
221	
222		new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Preference\"]"))))).perform();
223		
224		
225		//new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//*[@text='Checkbox preference']"))))).perform();
226		
227		//new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.id("io.appium.android.apis:id/checkbox"))))).perform();
228		
229		
230		
231		//Edit Preferences
232		
233		//new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//*[@text='Edit text preference']"))))).perform();
234		
235		//new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.TextView[@content-desc=\"Edit Text Preference\"]"))))).perform();
236		
237		
238		//new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.id("io.appium.android.apis:id/edit"))))).perform();
239		
240		//new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.EditText[@text=\"Default value\"]"))))).clear();
241		//new TouchAction(wd).tap(TapOptions.tapOptions().withElement(ElementOption.element((wd).findElement(By.xpath("//android.widget.EditText[@text=\"Default value\"]"))))).perform();
242		
243		//wd.findElement(By.id("io.appium.android.apis:id/edit")).sendKeys("Horse");
244		
245	
246	}
247	
248	/*@AfterTest
249	public void stopappium(){
250		wd.quit();
251		}
252		*/
253	
254	
255	@AfterSuite
256	public void closeappium() {
257		service.stop();
258}
259}
260
Full Screen

CalculatorApp.java

Source: CalculatorApp.java Github

copy
1import POM.CalcPage;
2import Utilities.DriverUtilities;
3import com.google.common.collect.ImmutableMap;
4import io.appium.java_client.*;
5import io.appium.java_client.android.*;
6import io.appium.java_client.android.nativekey.AndroidKey;
7import io.appium.java_client.android.nativekey.KeyEvent;
8import io.appium.java_client.android.nativekey.KeyEventFlag;
9import io.appium.java_client.touch.TapOptions;
10import io.appium.java_client.touch.offset.ElementOption;
11import io.appium.java_client.touch.offset.PointOption;
12import org.openqa.selenium.By;
13import org.openqa.selenium.Point;
14import org.openqa.selenium.WebDriver;
15import org.openqa.selenium.WebElement;
16import org.openqa.selenium.interactions.touch.TouchActions;
17import org.testng.Assert;
18import org.testng.Reporter;
19import org.testng.annotations.*;
20import org.openqa.selenium.interactions.Actions;
21
22import java.net.MalformedURLException;
23import java.time.Duration;
24import java.util.List;
25
26import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
27import static io.appium.java_client.touch.WaitOptions.waitOptions;
28import static io.appium.java_client.touch.offset.ElementOption.element;
29import static io.appium.java_client.touch.offset.PointOption.point;
30import static java.time.Duration.ofSeconds;
31import static org.testng.Assert.assertEquals;
32
33@Listeners(Utilities.ListenerTest.class)
34/**
35 * The Main test file thats mapped in testng.xml
36 */
37public class CalculatorApp extends CalcPage {
38
39    public AppiumDriver driver;
40    public CalcPage calcPage;
41
42    public CalculatorApp() throws MalformedURLException {
43        driver = DriverUtilities.driver;
44    }
45
46    @BeforeClass
47    public void setUp() throws MalformedURLException {
48        calcPage = new CalcPage();
49    }
50
51    @BeforeMethod
52    public void startApp(){
53        Activity activity = new Activity("com.android2.calculator3","com.xlythe.calculator.material.Theme.Orange");
54        ((AndroidDriver) driver).startActivity(activity);
55}
56
57    @Test(priority = 0, description = "Verify addition of two numbers")
58    public void verifyAddFunctionality() throws InterruptedException {
59        calcPage.enterTwoNumbersToAdd("2","4");
60        Assert.assertEquals(calcPage.result(),"6");
61        Reporter.log("Numbers have been added successfully");
62    }
63
64    @AfterMethod
65    public void stopApp() {
66        ((AndroidDriver) driver).closeApp();
67    }
68}
69
Full Screen

Gestures.java

Source: Gestures.java Github

copy
1package com.techtorial.Intro.appium;
2
3import com.techtorial.utils.AndroidDriverUtils;
4import io.appium.java_client.TouchAction;
5import io.appium.java_client.android.AndroidDriver;
6import io.appium.java_client.android.AndroidElement;
7import io.appium.java_client.touch.LongPressOptions;
8import io.appium.java_client.touch.TapOptions;
9import io.appium.java_client.touch.offset.ElementOption;
10import org.junit.Assert;
11import org.junit.Before;
12import org.junit.Test;
13import org.openqa.selenium.By;
14import org.openqa.selenium.remote.DesiredCapabilities;
15import java.net.MalformedURLException;
16import java.time.Duration;
17import java.util.List;
18
19
20public class Gestures {
21    AndroidDriver<AndroidElement> driver;
22
23    @Before
24    public void setup() throws MalformedURLException {
25        DesiredCapabilities caps = AndroidDriverUtils.setupDesiredCapabilities("ApiDemos-debug");
26        driver = AndroidDriverUtils.getAndroidDriver(caps);
27    }
28
29
30    @Test
31    public void tapGesture() {
32
33        AndroidElement views = driver.findElementByAndroidUIAutomator("text(\"Views\")");
34        TouchAction touchAction = new TouchAction(driver);
35        touchAction.tap(TapOptions.tapOptions().withElement(ElementOption.element(views))).perform();
36
37        AndroidElement expendableList = driver.findElementByAndroidUIAutomator("text(\"Expandable Lists\")");
38        touchAction.tap(TapOptions.tapOptions().withElement(ElementOption.element(expendableList))).perform();
39
40        AndroidElement customAdapter = driver.findElementByAccessibilityId("1. Custom Adapter");
41        touchAction.tap(TapOptions.tapOptions().withElement(ElementOption.element(customAdapter))).perform();
42
43        AndroidElement catNames = driver.findElement(By.xpath("//android.widget.TextView[@text='Cat Names']"));
44        Assert.assertEquals("Cat Names", catNames.getText());
45
46    }
47
48
49    @Test
50    public void longPress() {
51
52        AndroidElement views = driver.findElementByAndroidUIAutomator("text(\"Views\")");
53        TouchAction touchAction = new TouchAction(driver);
54        touchAction.tap(TapOptions.tapOptions().withElement(ElementOption.element(views))).perform();
55
56        AndroidElement expendableList = driver.findElementByAndroidUIAutomator("text(\"Expandable Lists\")");
57        touchAction.tap(TapOptions.tapOptions().withElement(ElementOption.element(expendableList))).perform();
58
59        AndroidElement customAdapter = driver.findElementByAccessibilityId("1. Custom Adapter");
60        touchAction.tap(TapOptions.tapOptions().withElement(ElementOption.element(customAdapter))).perform();
61
62        AndroidElement peopleNames = driver.findElementByAndroidUIAutomator("text(\"People Names\")");
63        touchAction.longPress(LongPressOptions.longPressOptions().withDuration(Duration.ofSeconds(2)).withElement(ElementOption.element(peopleNames))).perform();
64
65        AndroidElement sampleMenu = driver.findElementByAndroidUIAutomator("text(\"Sample menu\")");
66        Assert.assertEquals("Failed to verify Sample Menu text", "Sample menu", sampleMenu.getText());
67        Assert.assertTrue("Failed to verify Sample Menu is nit displayed", sampleMenu.isDisplayed());
68
69    }
70
71
72    @Test
73    public void longPress2() {
74
75        AndroidElement views = driver.findElementByAndroidUIAutomator("text(\"Views\")");
76        TouchAction touchAction = new TouchAction(driver);
77        touchAction.tap(TapOptions.tapOptions().withElement(ElementOption.element(views))).perform();
78
79        AndroidElement expendableList = driver.findElementByAndroidUIAutomator("text(\"Expandable Lists\")");
80        touchAction.tap(TapOptions.tapOptions().withElement(ElementOption.element(expendableList))).perform();
81
82        AndroidElement customAdapter = driver.findElementByAccessibilityId("1. Custom Adapter");
83        touchAction.tap(TapOptions.tapOptions().withElement(ElementOption.element(customAdapter))).perform();
84
85        AndroidElement fishNames = driver.findElementByAndroidUIAutomator("text(\"Fish Names\")");
86        touchAction.tap(TapOptions.tapOptions().withElement(ElementOption.element(fishNames))).perform();
87
88        List<AndroidElement> fishNameList = driver.findElements("//+[@text='Goldy']", "//+[@text='Bubbles']");
89
90        Assert.assertEquals(2, fishNameList.size());
91
92        for (AndroidElement element : fishNameList) {
93            Assert.assertTrue(element.isDisplayed());
94        }
95        touchAction.longPress(LongPressOptions.longPressOptions()
96                .withDuration(Duration.ofSeconds(2)).withElement(ElementOption.element(fishNames))).perform();
97
98        AndroidElement sampleMenu = driver.findElementByAndroidUIAutomator("text(\"Sample menu\")");
99        Assert.assertTrue(sampleMenu.isDisplayed());
100
101    }
102
103
104}
105
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 ElementOption

Trigger verify code on LambdaTest Cloud Grid

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