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"))
Appium for Mac OS: Is there a way to send the Enter key properly in the Messages app?
Appium unable to get "content-desc" attribute data
how to add explicit wait in drop down in selenium which is dependent on another dropdown?
Need to play a video for automation on safari mobile using appium
Appium findElement used twice in one row not working
Appium Error : A new session could not be created. (Original error: Did not get session redirect from Chromedriver)
How to close/kill an app on android device using appium?
How to scroll down on Appium when you have multi plages to go down?
Connection reset(WebDriver Exception) in appium
error: Logcat capture failed: spawn ENOENT
Import actionchains
and try something like below:
WebElement webElement = appiumForMacDriver.findElement(ELEMENT_LOCATOR);
ActionChains(appiumForMacDriver).move_to_element(webElement).click().perform()
Actionchains(appiumForMacDriver).sendKeys(message).perform();
Check out the latest blogs from LambdaTest on this topic:
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Mobile App Testing Tutorial.
The best agile teams are built from people who work together as one unit, where each team member has both the technical and the personal skills to allow the team to become self-organized, cross-functional, and self-motivated. These are all big words that I hear in almost every agile project. Still, the criteria to make a fantastic agile team are practically impossible to achieve without one major factor: motivation towards a common goal.
When working on web automation with Selenium, I encountered scenarios where I needed to refresh pages from time to time. When does this happen? One scenario is that I needed to refresh the page to check that the data I expected to see was still available even after refreshing. Another possibility is to clear form data without going through each input individually.
Did you know that according to Statista, the number of smartphone users will reach 18.22 billion by 2025? Let’s face it, digital transformation is skyrocketing and will continue to do so. This swamps the mobile app development market with various options and gives rise to the need for the best mobile app testing tools
It’s strange to hear someone declare, “This can’t be tested.” In reply, I contend that everything can be tested. However, one must be pleased with the outcome of testing, which might include failure, financial loss, or personal injury. Could anything be tested when a claim is made with this understanding?
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!!