From 67ffaaa29af8bab165509af58796a63451daf4bb Mon Sep 17 00:00:00 2001 From: Eyal Yovel Date: Tue, 29 Oct 2024 15:10:27 +0000 Subject: [PATCH] amend the upload and download file example --- .../simple_example/AndroidNativeAppTest.java | 7 +++++-- .../DownloadImageFromAndroidRealDevice.java | 18 ++++++++++------ .../up_download_file/SamsungGallery.java | 21 +++++++++++-------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/appium/appium-app/appium-app-examples/src/test/java/com/examples/simple_example/AndroidNativeAppTest.java b/appium/appium-app/appium-app-examples/src/test/java/com/examples/simple_example/AndroidNativeAppTest.java index c0e7dda0..cff5ad37 100644 --- a/appium/appium-app/appium-app-examples/src/test/java/com/examples/simple_example/AndroidNativeAppTest.java +++ b/appium/appium-app/appium-app-examples/src/test/java/com/examples/simple_example/AndroidNativeAppTest.java @@ -2,6 +2,7 @@ import com.helpers.SauceAppiumTestWatcher; import io.appium.java_client.AppiumBy; +import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.AndroidDriver; import org.junit.Before; import org.junit.Rule; @@ -76,10 +77,10 @@ public void setup() throws MalformedURLException { else { capabilities.setCapability("appium:deviceName", "Android GoogleAPI Emulator"); capabilities.setCapability("appium:appWaitActivity", ".view.activities.MainActivity"); - sauceOptions.setCapability("appiumVersion", "2.11.0"); + sauceOptions.setCapability("appiumVersion", "latest"); } capabilities.setCapability("appium:platformVersion", "12"); - String appName = "SauceLabs-Demo-App.apk"; + String appName = "mda-2.1.0-24.apk"; capabilities.setCapability("appium:app", "storage:filename=" +appName); // Sauce capabilities @@ -100,6 +101,8 @@ public void setup() throws MalformedURLException { System.out.println("Error to create Android Driver: " + e.getMessage()); return; } + + System.out.println("Job ID is: " + driver.getCapabilities().getCapability("appium:jobUuid")); //Setting the driver so that we can report results resultReportingTestWatcher.setDriver(driver); } diff --git a/appium/appium-app/appium-app-examples/src/test/java/com/examples/up_download_file/DownloadImageFromAndroidRealDevice.java b/appium/appium-app/appium-app-examples/src/test/java/com/examples/up_download_file/DownloadImageFromAndroidRealDevice.java index b3213373..424b1c6d 100644 --- a/appium/appium-app/appium-app-examples/src/test/java/com/examples/up_download_file/DownloadImageFromAndroidRealDevice.java +++ b/appium/appium-app/appium-app-examples/src/test/java/com/examples/up_download_file/DownloadImageFromAndroidRealDevice.java @@ -32,7 +32,7 @@ public class DownloadImageFromAndroidRealDevice { int currentPhotos = 0; String deviceFilePath = "/storage/self/primary/sauce-bot-coding.png"; - String downloadFolder = "src/test/java/com/appium_app/up_download_file/samsung_real_device"; + String downloadFolder = "src/test/java/com/examples/up_download_file/samsung_real_device"; //This rule allows us to set test status with Junit @Rule @@ -58,8 +58,8 @@ public void setup() throws IOException { capabilities.setCapability("platformName", "Android"); capabilities.setCapability("appium:automationName", "UiAutomator2"); - capabilities.setCapability("appium:deviceName", "Samsung Galaxy S9"); - capabilities.setCapability("appium:platformVersion", "10"); + capabilities.setCapability("appium:deviceName", "Samsung Galaxy S23"); + capabilities.setCapability("appium:platformVersion", "13"); capabilities.setCapability("appium:newCommandTimeout", 240); capabilities.setCapability("appium:browserName", "chrome"); capabilities.setCapability("appium:autoGrantPermissions", true); @@ -71,6 +71,7 @@ public void setup() throws IOException { sauceOptions.setCapability("tags", tags); sauceOptions.setCapability("username", System.getenv("SAUCE_USERNAME")); sauceOptions.setCapability("accessKey", System.getenv("SAUCE_ACCESS_KEY")); + sauceOptions.setCapability("appiumVersion", "stable"); capabilities.setCapability("sauce:options", sauceOptions); @@ -98,12 +99,17 @@ public void downloadFileFromRealDevice() throws InterruptedException, IOExceptio System.out.println("Sauce - number of photos before upload: " + currentPhotos ); // The file we want to upload - String codingBot = "src/test/java/com/appium_app/up_download_file/sauce-bot-coding.png"; + String codingBot = "src/test/java/com/examples/up_download_file/sauce-bot-coding.png"; File codingBotFile = new File(codingBot); // Push the file to the device // This is the `tricky` part, you need to know the file structure of the device and where you can download the file from. // We checked this structure with the VUSB offering of Sauce Labs for private devices. - driver.pushFile(deviceFilePath, codingBotFile); + try { + driver.pushFile(deviceFilePath, codingBotFile); + } catch(Exception ex) + { + System.out.println("Error: " + ex.getMessage()); + } // wait till it is uploaded boolean bPhotoUpload = samsungGallery.waitUploadPhoto(currentPhotos, 5); @@ -125,7 +131,7 @@ public void downloadFileFromRealDevice() throws InterruptedException, IOExceptio ImageIO.write(image, "png", f); // Now verify that the file does exist locally - assertThat(f.exists()).as("The file we downloaded from the device, doesm't exist locally").isTrue(); + assertThat(f.exists()).as("The file we downloaded from the device, doesn't exist locally").isTrue(); // This is not need only for the video waiting(2); } diff --git a/appium/appium-app/appium-app-examples/src/test/java/com/examples/up_download_file/SamsungGallery.java b/appium/appium-app/appium-app-examples/src/test/java/com/examples/up_download_file/SamsungGallery.java index 6d275136..0d6b7bf0 100644 --- a/appium/appium-app/appium-app-examples/src/test/java/com/examples/up_download_file/SamsungGallery.java +++ b/appium/appium-app/appium-app-examples/src/test/java/com/examples/up_download_file/SamsungGallery.java @@ -1,6 +1,7 @@ package com.examples.up_download_file; import com.google.common.collect.ImmutableMap; +import io.appium.java_client.AppiumBy; import io.appium.java_client.android.Activity; import io.appium.java_client.android.AndroidDriver; import org.openqa.selenium.By; @@ -16,26 +17,27 @@ public class SamsungGallery { private AndroidDriver driver; - private String photos = "com.sec.android.gallery3d:id/recycler_view_item"; - private String deleteButton ="com.sec.android.gallery3d:id/btn_delete"; - private String confirmDeleteButton = "com.sec.android.gallery3d:id/button1"; + private String photos = "//android.widget.FrameLayout[@content-desc='Button']"; + + private String deleteButton ="Delete"; + private String confirmDeleteButton = "android:id/button1"; public SamsungGallery(AndroidDriver driver) { this.driver = driver; } public void open() { - driver.executeScript("mobile: startActivity", ImmutableMap.of( - "appPackage", "com.sec.android.gallery3d", - "appActivity", "com.samsung.android.gallery.app.activity.GalleryActivity" + driver.executeScript("mobile: activateApp", ImmutableMap.of( + "appId", "com.sec.android.gallery3d" )); + } public void openPhoto(String which){ int whichPhoto = (which == "first" ? 0 : this.amountOfPhotos()-1); // open the photo - List photosList = driver.findElements(By.id(photos)); + List photosList = driver.findElements(By.xpath(photos)); photosList.get(whichPhoto).click(); } @@ -49,14 +51,15 @@ public void deletePhoto(String which){ WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); - final WebElement deleteImgBtn = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(deleteButton))); + final WebElement deleteImgBtn = wait.until(ExpectedConditions.visibilityOfElementLocated(new AppiumBy.ByAccessibilityId(deleteButton))); deleteImgBtn.click(); driver.findElement(By.id(confirmDeleteButton)).click(); } public int amountOfPhotos(){ - return (driver.findElements(By.id(photos)).size()); + return (driver.findElements(By.xpath(photos)).size()); + } /**