How to use name method of io.appium.java_client.AppiumBy class

Best io.appium code snippet using io.appium.java_client.AppiumBy.name

Run io.appium automation tests on LambdaTest cloud grid

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

GestureTest.java

Source: GestureTest.java Github

copy
1package com.example.gestures;
2
3import com.google.common.collect.ImmutableMap;
4import io.appium.java_client.AppiumBy;
5import io.appium.java_client.AppiumDriver;
6import io.appium.java_client.android.AndroidDriver;
7import io.appium.java_client.android.options.UiAutomator2Options;
8import org.openqa.selenium.Point;
9import org.openqa.selenium.Rectangle;
10import org.openqa.selenium.WebElement;
11import org.openqa.selenium.interactions.Pause;
12import org.openqa.selenium.interactions.PointerInput;
13import org.openqa.selenium.interactions.Sequence;
14import org.openqa.selenium.logging.LogType;
15import org.openqa.selenium.logging.LoggingPreferences;
16import org.openqa.selenium.remote.RemoteWebElement;
17import org.openqa.selenium.remote.http.HttpMethod;
18import org.openqa.selenium.support.ui.ExpectedConditions;
19import org.openqa.selenium.support.ui.WebDriverWait;
20import org.testng.annotations.BeforeClass;
21import org.testng.annotations.Test;
22
23import java.net.URL;
24import java.time.Duration;
25import java.util.logging.Level;
26
27import static java.time.Duration.*;
28import static java.time.Duration.ofMillis;
29import static java.util.Collections.singletonList;
30import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;
31import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
32
33public class GestureTest {
34
35    public AppiumDriver driver;
36    public WebDriverWait wait;
37
38    @BeforeClass
39    public void beforeClass() throws Exception {
40        UiAutomator2Options options = new UiAutomator2Options()
41                .setDeviceName("Android Emulator")
42                .setApp(System.getProperty("user.dir") + "/apps/VodQA.apk");
43        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), options);
44        wait = new WebDriverWait(driver, ofSeconds(30));
45    }
46
47    @Test
48    public void dragAndDropTest() {
49        wait.until(elementToBeClickable(AppiumBy.accessibilityId("login"))).click();
50        wait.until(elementToBeClickable(AppiumBy.accessibilityId("dragAndDrop"))).click();
51        final WebElement dragMe = wait.until(presenceOfElementLocated(AppiumBy.accessibilityId("dragMe")));
52        final WebElement dropzone = wait.until(elementToBeClickable(AppiumBy.accessibilityId("dropzone")));
53
54        final Point point = dragMe.getRect().getPoint();
55        final Rectangle rect = dropzone.getRect();
56        final int targetX = rect.getX() + rect.getWidth() / 2;
57        final int targetY = rect.getY() + rect.getHeight() / 2;
58
59        PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
60        Sequence dragAndDrop = new Sequence(finger, 1);
61        // pointerMove, pointerDown, pause, pointerMove, pointerUp
62        dragAndDrop.addAction(finger.createPointerMove(Duration.ofMillis(0), PointerInput.Origin.viewport(),
63                point.getX(), point.getY()));
64        dragAndDrop.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
65        dragAndDrop.addAction(new Pause(finger, Duration.ofMillis(600)));
66        dragAndDrop.addAction(finger.createPointerMove(Duration.ofMillis(600), PointerInput.Origin.viewport(),
67                targetX, targetY));
68        dragAndDrop.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
69
70        driver.perform(singletonList(dragAndDrop));
71    }
72
73    @Test
74    public void dragAndDropUsingAppiumPluginTest() {
75        wait.until(elementToBeClickable(AppiumBy.accessibilityId("login"))).click();
76        wait.until(elementToBeClickable(AppiumBy.accessibilityId("dragAndDrop"))).click();
77        final WebElement dragMe = wait.until(presenceOfElementLocated(AppiumBy.accessibilityId("dragMe")));
78        final WebElement dropzone = wait.until(elementToBeClickable(AppiumBy.accessibilityId("dropzone")));
79
80        driver.addCommand(HttpMethod.POST, String.format("/session/%s/plugin/actions/dragAndDrop",
81                driver.getSessionId()), "dragAndDrop");
82
83        driver.execute("dragAndDrop", ImmutableMap.of("sourceId", ((RemoteWebElement) dragMe).getId(),
84                "destinationId", ((RemoteWebElement) dropzone).getId()));
85    }
86}
87
Full Screen

sampleTestAOSapp_iOS.java

Source: sampleTestAOSapp_iOS.java Github

copy
1import io.appium.java_client.AppiumBy;
2import io.appium.java_client.ios.options.XCUITestOptions;
3import io.appium.java_client.ios.IOSDriver;
4import org.junit.*;
5import org.openqa.selenium.WebElement;
6import org.openqa.selenium.support.ui.ExpectedConditions;
7import org.openqa.selenium.support.ui.WebDriverWait;
8import java.net.URL;
9import java.time.Duration;
10
11
12public class sampleTestAOSapp_iOS {
13    private IOSDriver driver;
14    private String SERVER = "http://uftm-server:8080";
15
16    @Before
17    public void setUp() throws Exception {
18        XCUITestOptions caps = new XCUITestOptions();
19        caps.setCapability("platformName", "iOS");
20        caps.setCapability("udid", "3ecdf8889a8b18cb8858410d1540f3e6b22a2cb1");
21        caps.setCapability("bundleId", "com.mf.iShopping");
22        caps.setCapability("uftm:oauthClientId", "[email protected]");
23        caps.setCapability("uftm:oauthClientSecret", "tcNG3V5YVdjbgJhPl3Vl");
24        caps.setCapability("uftm:tenantId", "999999999");
25        driver = new IOSDriver(new URL(SERVER + "/wd/hub"), caps);
26    }
27
28    @After
29    public void teardown(){
30        if (driver != null) {
31            driver.quit();
32        }
33    }
34
35    @Test
36    public void testAOSapp(){
37        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
38        driver.findElement(AppiumBy.xpath("//XCUIElementTypeStaticText[@name=\"LAPTOPS\"]")).click();
39        driver.findElement(AppiumBy.accessibilityId("HP ENVY x360 - 15t Laptop")).click();
40        wait.until(ExpectedConditions.presenceOfElementLocated(AppiumBy.accessibilityId("productDetailsButton"))).click();
41        driver.findElement(AppiumBy.accessibilityId("Close2")).click();
42        driver.findElement(AppiumBy.accessibilityId("quantityButtonId")).click();
43        driver.findElement(AppiumBy.accessibilityId("Plus")).click();
44        driver.findElement(AppiumBy.accessibilityId("APPLY")).click();
45        driver.findElement(AppiumBy.accessibilityId("ADD TO CART")).click();
46        driver.findElement(AppiumBy.accessibilityId("Ok")).click();
47        driver.findElement(AppiumBy.accessibilityId("userNameLabel")).sendKeys("Username");
48        driver.findElement(AppiumBy.accessibilityId("passwordLabel")).sendKeys("password");
49        driver.findElement(AppiumBy.accessibilityId("LOGIN")).click();
50        WebElement loginErrorText = wait.until(ExpectedConditions.presenceOfElementLocated(
51                AppiumBy.accessibilityId("Incorrect user name or password.")));
52        assert loginErrorText.isDisplayed() == true;
53        wait.until(ExpectedConditions.presenceOfElementLocated(AppiumBy.accessibilityId("Menu"))).click();
54        driver.findElement(AppiumBy.accessibilityId("HOME")).click();
55    }
56}
57
Full Screen

sampleTestAOSapp_Android.java

Source: sampleTestAOSapp_Android.java Github

copy
1import io.appium.java_client.AppiumBy;
2import io.appium.java_client.android.AndroidDriver;
3import io.appium.java_client.android.options.UiAutomator2Options;
4import org.junit.*;
5import org.openqa.selenium.support.ui.ExpectedConditions;
6import org.openqa.selenium.support.ui.WebDriverWait;
7
8import java.net.URL;
9import java.time.Duration;
10import java.util.concurrent.TimeUnit;
11
12public class sampleTestAOSapp_Android {
13    private AndroidDriver driver;
14    private String SERVER = "http://uftm-server:8080";
15
16    @Before
17    public void setUp() throws Exception {
18        UiAutomator2Options caps = new UiAutomator2Options();
19        caps.setCapability("platformName", "Android");
20        caps.setCapability("udid", "RF8N879MPYH");
21        caps.setCapability("automationName", "UiAutomator2");
22        caps.setCapability("appPackage", "com.Advantage.aShopping");
23        caps.setCapability("appActivity", "com.Advantage.aShopping.SplashActivity");
24        caps.setCapability("uftm:oauthClientId", "[email protected]");
25        caps.setCapability("uftm:oauthClientSecret", "tcNG3V5YVdjbgJhPl3Vl");
26        caps.setCapability("uftm:tenantId", "999999999");
27        driver = new AndroidDriver(new URL(SERVER + "/wd/hub"), caps);
28    }
29
30    @After
31    public void teardown(){
32        if (driver != null) {
33            driver.quit();
34        }
35    }
36
37    @Test
38    public void testAOSapp(){
39        //Implicit wait
40        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
41        driver.findElement(AppiumBy.xpath(
42                "//android.view.ViewGroup[@content-desc=\"Home Page\"]/android.widget.LinearLayout[2]/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.TextView"))
43                .click();
44        driver.findElement(AppiumBy.xpath(
45                "//android.widget.RelativeLayout[@content-desc=\"Laptops\"]/android.widget.LinearLayout/android.widget.GridView/android.widget.RelativeLayout[4]/android.widget.TextView[1]"))
46                .click();
47        driver.findElement(AppiumBy.id("com.Advantage.aShopping:id/linearLayoutProductDetails")).click();
48        driver.findElement(AppiumBy.id("com.Advantage.aShopping:id/imageViewProductDetailsClose")).click();
49        driver.findElement(AppiumBy.id("com.Advantage.aShopping:id/linearLayoutProductQuantity")).click();
50        driver.findElement(AppiumBy.xpath(
51                "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[2]/android.widget.ImageView[2]"))
52                .click();
53        driver.findElement(AppiumBy.id("com.Advantage.aShopping:id/textViewApply")).click();
54        driver.findElement(AppiumBy.id("com.Advantage.aShopping:id/buttonProductAddToCart")).click();
55        driver.findElement(AppiumBy.xpath(
56                "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[2]/android.widget.RelativeLayout[3]/android.widget.EditText"))
57                .sendKeys("Username");
58        driver.findElement(AppiumBy.xpath(
59                "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[2]/android.widget.RelativeLayout[4]/android.widget.EditText"))
60                .sendKeys("password");
61        driver.findElement(AppiumBy.id("com.Advantage.aShopping:id/buttonLogin")).click();
62        String ErrorMessage = driver.findElement(AppiumBy.id("com.Advantage.aShopping:id/textViewFailed")).getText();
63        assert ErrorMessage.contains("Incorrect user");
64        driver.navigate().back();
65        driver.navigate().back();
66        driver.findElement(AppiumBy.id("com.Advantage.aShopping:id/imageViewMenu")).click();
67        driver.findElement(AppiumBy.id("com.Advantage.aShopping:id/textViewMenuHome")).click();
68
69    }
70
71}
72
73
74
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 name code on LambdaTest Cloud Grid

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