Best io.appium code snippet using io.appium.java_client.AppiumBy.image
AndroidTouchTest.java
Source: AndroidTouchTest.java
...138 @Test public void horizontalSwipingTest() {139 Activity activity = new Activity("io.appium.android.apis", ".view.Gallery1");140 driver.startActivity(activity);141 WebElement gallery = driver.findElement(By.id("io.appium.android.apis:id/gallery"));142 List<WebElement> images = gallery.findElements(AppiumBy.className("android.widget.ImageView"));143 int originalImageCount = images.size();144 Point location = gallery.getLocation();145 Point center = getCenter(gallery);146 TouchAction swipe = new TouchAction(driver)147 .press(element(images.get(2),-10, center.y - location.y))148 .waitAction(waitOptions(ofSeconds(2)))149 .moveTo(element(gallery,10,center.y - location.y))150 .release();151 swipe.perform();152 assertNotEquals(originalImageCount,153 gallery.findElements(AppiumBy.className("android.widget.ImageView")).size());154 }155 @Test public void multiTouchTest() {156 Activity activity = new Activity("io.appium.android.apis", ".view.Buttons1");157 driver.startActivity(activity);158 TouchAction press = new TouchAction(driver)159 .press(element(driver.findElement(By.id("io.appium.android.apis:id/button_toggle"))))160 .waitAction(waitOptions(ofSeconds(1)))161 .release();...
PageHelper.java
Source: PageHelper.java
...14public class PageHelper {15 protected AndroidDriver driver;16 private WebDriverWait wait;17 By moreActions = By.id("buttonMore");18 By audiobookIcon = By.id("imageAudioBook");19 By ebookIcon = By.id("imageEbook");20 By minimalizedPlayer = By.id("v_player_container");21 By minimalizedRewind = By.id("img_rewind");22 By minimalizedPlayOrPause = By.id("img_play");23 By minimalizedBookTitle = AppiumBy.androidUIAutomator("new UiSelector().resourceId(" +24 "\"com.bokus.play:id/constraint_parent\").childSelector(new UiSelector().resourceId(" +25 "\"com.bokus.play:id/txt_title\"))");26 //By unlockBook = By.xpath("//android.widget.Button[@resource-id='android:id/button1' and @text='LÃ
S UPP BOKEN']");27 By unlockBook = By.id("button1");28 By sortButton = By.id("buttonSort");29 By sortFilterButton = By.id("btn_see_result");30 By filterButton = By.id("buttonFilter");31 By filterResultButton = By.id("btn_see_result");32 By moreActionsSheet = By.id("bottom_sheet");33 By addToFavorite = By.id("btn_favorite");...
sampleTestAOSapp_Android.java
Source: sampleTestAOSapp_Android.java
...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();6869 }7071}7273
...
BookshelfAllPage.java
Source: BookshelfAllPage.java
...32 public void clickFilterButtonBookshelfPage() { driver.findElement(filterButton).click(); }33 public void clickBookTitleMoreActions(String bookTitle) {34 driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(" +35 "\"android.view.ViewGroup\").resourceId(\"com.bokus.play:id/itemView\").childSelector(new UiSelector()"+36 ".text(\"" + bookTitle + "\").fromParent(new UiSelector().resourceId(\"com.bokus.play:id/imageMenu\")))"37 )).click();38 Assert.assertTrue(moreActionsDisplayed());39 }40 public void clickBookTitleDownloadIcon(String bookTitle) {41 driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(" +42 "\"android.view.ViewGroup\").resourceId(\"com.bokus.play:id/itemView\").childSelector(new UiSelector()"+43 ".textContains(\"" + bookTitle + "\").fromParent(new UiSelector().resourceId(" +44 "\"com.bokus.play:id/layoutDownloadIndicator\")))")).click();45 Assert.assertTrue(webDriverWaitHelper(2, downloadingText).isDisplayed());46 }47 public String getFirstBookTitle() {48 return driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().resourceId(" +49 "\"com.bokus.play:id/itemView\").index(1).childSelector(new UiSelector().resourceId(" +50 "\"com.bokus.play:id/textBookTitle\"))")).getText();...
SwagLabsLogin.java
Source: SwagLabsLogin.java
1import io.appium.java_client.AppiumBy;2import io.appium.java_client.AppiumDriver;3import io.appium.java_client.MobileBy;4import io.appium.java_client.TouchAction;5import io.appium.java_client.android.AndroidDriver;6import io.appium.java_client.touch.WaitOptions;7import io.appium.java_client.touch.offset.ElementOption;8import io.appium.java_client.touch.offset.PointOption;9import org.openqa.selenium.By;10import org.openqa.selenium.WebDriver;11import org.openqa.selenium.remote.DesiredCapabilities;12//import org.openqa.selenium.remote.RemoteWebDriver;13import org.testng.annotations.Test;14import java.net.MalformedURLException;15import java.net.URL;16import java.time.Duration;17public class SwagLabsLogin{18@Test19 public void login() throws MalformedURLException, InterruptedException {20 DesiredCapabilities capabilities=new DesiredCapabilities();21 capabilities.setCapability("platformName", "Android");22 capabilities.setCapability("deviceName", "Pixel 3");23 capabilities.setCapability("automationName", "UiAutomator2");24 capabilities.setCapability("udid", "emulator-5554");25 capabilities.setCapability("app","/Users/gowthamipv/IdeaProjects/LoginPage/src/test/resources/app/Android.SauceLabs.Mobile.Sample.app.2.7.1.apk");26 capabilities.setCapability("appPackage", "com.swaglabsmobileapp");27 capabilities.setCapability("appActivity", "com.swaglabsmobileapp.MainActivity");28 AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);29 Thread.sleep(3000);30 driver.findElement(By.xpath("//android.widget.EditText[@content-desc=\"test-Username\"]")).sendKeys("standard_user");31 Thread.sleep(3000);32 driver.findElement(By.xpath("//android.widget.EditText[@content-desc=\"test-Password\"]")).sendKeys("secret_sauce");33 Thread.sleep(3000);34 driver.findElement(By.xpath("//android.view.ViewGroup[@content-desc=\"test-LOGIN\"]")).click();35 Thread.sleep(3000);36 driver.findElement(By.xpath("(//android.view.ViewGroup[@content-desc=\"test-ADD TO CART\"])[1]")).click();37 Thread.sleep(3000);38 driver.findElement(By.xpath("//android.view.ViewGroup[@content-desc=\"test-Cart\"]/android.view.ViewGroup/android.widget.ImageView")).click();39 Thread.sleep(3000);40 TouchAction t = new TouchAction<>(driver);41 t.tap(PointOption.point(509,1808)).perform();42 Thread.sleep(3000);43 driver.findElement(By.xpath("//android.widget.EditText[@content-desc=\"test-First Name\"]")).sendKeys("Gowthami");44 Thread.sleep(3000);45 driver.findElement(By.xpath("//android.widget.EditText[@content-desc=\"test-Last Name\"]")).sendKeys("PV");46 Thread.sleep(3000);47 driver.findElement(By.xpath("//android.widget.EditText[@content-desc=\"test-Zip/Postal Code\"]")).sendKeys("563122");48 Thread.sleep(3000);49 t.tap(PointOption.point(521,1563)).perform();50 Thread.sleep(3000);51 t.press(PointOption.point(597,1883)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(3000))).moveTo(PointOption.point(573,559)).release().perform();52 Thread.sleep(3000);53 driver.findElement(By.xpath("//android.view.ViewGroup[@content-desc=\"test-FINISH\"]")).click();54 Thread.sleep(3000);55 driver.findElement(By.xpath("//android.view.ViewGroup[@content-desc=\"test-BACK HOME\"]")).click();56 Thread.sleep(3000);57 driver.quit();58}59}...
AndroidAbilityToUseSupplierTest.java
...15public class AndroidAbilityToUseSupplierTest extends BaseAndroidTest {16 private final ActionSupplier<AndroidTouchAction> horizontalSwipe = () -> {17 driver.findElement(By.id("io.appium.android.apis:id/gallery"));18 WebElement gallery = driver.findElement(By.id("io.appium.android.apis:id/gallery"));19 List<WebElement> images = gallery.findElements(AppiumBy.className("android.widget.ImageView"));20 Point location = gallery.getLocation();21 Point center = getCenter(gallery, location);22 ElementOption pressOption = element(images.get(2),-10,center.y - location.y);23 ElementOption moveOption = element(gallery, 10,center.y - location.y);24 return new AndroidTouchAction(driver)25 .press(pressOption)26 .waitAction(waitOptions(ofSeconds(2)))27 .moveTo(moveOption)28 .release();29 };30 private final ActionSupplier<AndroidTouchAction> verticalSwiping = () ->31 new AndroidTouchAction(driver)32 .press(element(driver.findElement(AppiumBy.accessibilityId("Gallery"))))33 .waitAction(waitOptions(ofSeconds(2)))34 .moveTo(element(driver.findElement(AppiumBy.accessibilityId("Auto Complete"))))35 .release();36 @Test public void horizontalSwipingWithSupplier() {37 Activity activity = new Activity("io.appium.android.apis", ".view.Gallery1");38 driver.startActivity(activity);39 WebElement gallery = driver.findElement(By.id("io.appium.android.apis:id/gallery"));40 List<WebElement> images = gallery.findElements(AppiumBy.className("android.widget.ImageView"));41 int originalImageCount = images.size();42 horizontalSwipe.get().perform();43 assertNotEquals(originalImageCount,44 gallery.findElements(AppiumBy.className("android.widget.ImageView")).size());45 }46 @Test public void verticalSwipingWithSupplier() throws Exception {47 driver.resetApp();48 driver.findElement(AppiumBy.accessibilityId("Views")).click();49 Point originalLocation = driver.findElement(AppiumBy.accessibilityId("Gallery")).getLocation();50 verticalSwiping.get().perform();51 Thread.sleep(5000);52 assertNotEquals(originalLocation, driver.findElement(AppiumBy.accessibilityId("Gallery")).getLocation());53 }54}...
BookshelfOfflinePage.java
Source: BookshelfOfflinePage.java
...8 By allTab = AppiumBy.accessibilityId("Alla");9 By favoritesTab = AppiumBy.accessibilityId("Favoriter");10 By offlineTab = AppiumBy.accessibilityId("Offline");11 By listsTab = AppiumBy.accessibilityId("Listor");12 By downloadedIcon = By.id("imageOffline");13 By downloadedText = By.id("textDownloaded");14 By offlineTabTitleText = By.id("textInfo");15 public BookshelfOfflinePage(AndroidDriver driver) { super(driver); }16 public void clickRemoveDownloadedBookIcon(String bookTitle) {17 driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(" +18 "\"android.view.ViewGroup\").resourceId(\"com.bokus.play:id/itemView\").childSelector(new UiSelector()"+19 ".text(\"" + bookTitle + "\").fromParent(new UiSelector().className(\"android.widget.FrameLayout\")" +20 ".resourceId(\"com.bokus.play:id/layoutDownloadIndicator\").childSelector(new UiSelector().resourceId("+21 "\"com.bokus.play:id/downloadIndicator\"))))")).click();22 driver.switchTo().alert().accept();23 }24 public boolean bookshelfOfflinePageSelected() { return waitForElementToAppear(offlineTab).isSelected(); }25 public void clickAllTab() { driver.findElement(allTab).click(); }26 public void clickFavoritesTab() { driver.findElement(favoritesTab).click(); }...
BookshelfFavoritesPage.java
Source: BookshelfFavoritesPage.java
...30 }31 public void clickBookTitleMoreActions(String bookTitle) {32 driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().resourceId(" +33 "\"com.bokus.play:id/itemView\").fromParent(new UiSelector().text(\"" + bookTitle + "\")" +34 ".fromParent(new UiSelector().resourceId(\"com.bokus.play:id/imageMenu\")))")).click();35 Assert.assertTrue(moreActionsDisplayed());36 }37 public void clickBookTitleDownloadIcon(String bookTitle) {38 driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().resourceId(" +39 "\"com.bokus.play:id/itemView\").fromParent(new UiSelector().text(\"" + bookTitle + "\")" +40 ".fromParent(new UiSelector().resourceId(\"com.bokus.play:id/layoutDownloadIndicator\")))")).click();41 Assert.assertTrue(webDriverWaitHelper(2, downloadingText).isDisplayed());42 }43}...
image
Using AI Code Generation
1import io.appium.java_client.AppiumDriver;2import io.appium.java_client.MobileElement;3import io.appium.java_client.android.AndroidDriver;4import io.appium.java_client.android.AndroidElement;5import io.appium.java_client.ios.IOSDriver;6import io.appium.java_client.ios.IOSElement;7import org.openqa.selenium.By;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.remote.DesiredCapabilities;10import org.testng.annotations.Test;11import java.io.File;12import java.io.IOException;13import java.net.MalformedURLException;14import java.net.URL;15import java.util.List;16public class AppiumTest {17 public static AppiumDriver driver;18 public void test() throws IOException {19 File app = new File("C:\\Users\\saurabh\\Downloads\\appium\\ApiDemos-debug.apk");20 DesiredCapabilities capabilities = new DesiredCapabilities();21 capabilities.setCapability("deviceName", "saurabh");22 capabilities.setCapability("platformName", "Android");23 capabilities.setCapability("app", app.getAbsolutePath());
image
Using AI Code Generation
1import java.io.File;2import java.io.IOException;3import java.net.MalformedURLException;4import java.net.URL;5import java.util.concurrent.TimeUnit;6import org.openqa.selenium.remote.DesiredCapabilities;7import org.openqa.selenium.support.ui.ExpectedConditions;8import org.openqa.selenium.support.ui.WebDriverWait;9import io.appium.java_client.AppiumDriver;10import io.appium.java_client.MobileElement;11import io.appium.java_client.android.AndroidDriver;12public class AppiumImageTest {13 public static AppiumDriver<MobileElement> driver;14 public static void main(String[] args) throws MalformedURLException, InterruptedException, IOException {15 DesiredCapabilities caps = new DesiredCapabilities();16 caps.setCapability("deviceName", "Pixel 2 API 28");17 caps.setCapability("platformName", "Android");18 caps.setCapability("platformVersion", "9.0");19 caps.setCapability("noReset", "true");20 caps.setCapability("appPackage", "com.android.calculator2");21 caps.setCapability("appActivity", "com.android.calculator2.Calculator");
image
Using AI Code Generation
1#from appium.webdriver.common.mobileby import MobileBy2#image = driver.find_element(MobileBy.Image("path to image"))3#from selenium.webdriver.common.by import By4#image = driver.find_element(By.Image("path to image"))5#image = driver.find_element(Appium::Core::Element::Mobile::MobileBy.Image("path to image"))6#image = driver.find_element(Selenium::WebDriver::By.Image("path to image"))7#image = driver.find_element(Appium::Core::Element::Mobile::MobileBy.Image("path to image"))8#image = driver.find_element(Selenium::WebDriver::By.Image("path to image"))
Android emulator not started. always throwing error for browserName while not include in caps
Appium test login in browser and back to app
How to automate Home,back,up,down,Menu button at bottom of Android phone using appium java client 6.1.0?
Appium error with testdroid: java.lang.NoClassDefFoundError: org/openqa/selenium/internal/HasIdentity
Chrome browser fails to launch in Appium using Java
Is there anyway to mix ios automation and web automation in a single script using selenium
Appium Android How to tap using the TEXT value
appium : webview : selenium : How to automate touchend event on a div?
Appium: autoAcceptAlert doesn't work to allow access to all photos on IOS 14
How to disable save password on ChromeDriver use Appium and AndroidStudio
Here you can find the platform configurator. Sauce Labs supply it to help with getting the right caps which is what you need. Based on your configuration, the correct caps will look like this for web testing:
DesiredCapabilities caps = DesiredCapabilities.android();
caps.setCapability("appiumVersion", "1.6.4");
caps.setCapability("deviceName","Android Emulator");
caps.setCapability("deviceOrientation", "portrait");
caps.setCapability("browserName", "Browser");
caps.setCapability("platformVersion", "4.4");
caps.setCapability("platformName","Android");
for Hybrid or App testing, the browserName
will be set as:
caps.setCapability("browserName", "");
Check out the latest blogs from LambdaTest on this topic:
Building a website is all about keeping the user experience in mind. Ultimately, it’s about providing visitors with a mind-blowing experience so they’ll keep coming back. One way to ensure visitors have a great time on your site is to add some eye-catching text or image animations.
Sometimes, in our test code, we need to handle actions that apparently could not be done automatically. For example, some mouse actions such as context click, double click, drag and drop, mouse movements, and some special key down and key up actions. These specific actions could be crucial depending on the project context.
People love to watch, read and interact with quality content — especially video content. Whether it is sports, news, TV shows, or videos captured on smartphones, people crave digital content. The emergence of OTT platforms has already shaped the way people consume content. Viewers can now enjoy their favorite shows whenever they want rather than at pre-set times. Thus, the OTT platform’s concept of viewing anything, anytime, anywhere has hit the right chord.
In an ideal world, you can test your web application in the same test environment and return the same results every time. The reality can be difficult sometimes when you have flaky tests, which may be due to the complexity of the web elements you are trying to perform an action on your test case.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!