Skip to content

Commit

Permalink
amend the upload and download file example
Browse files Browse the repository at this point in the history
  • Loading branch information
eyaly committed Oct 29, 2024
1 parent 07b920c commit 67ffaaa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<WebElement> photosList = driver.findElements(By.id(photos));
List<WebElement> photosList = driver.findElements(By.xpath(photos));
photosList.get(whichPhoto).click();
}

Expand All @@ -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());

}

/**
Expand Down

0 comments on commit 67ffaaa

Please sign in to comment.