Upload files to physical devices
TestingBot provides a feature that allows you to specify one or more media files that need to be uploaded to a physical iOS or Android device, prior to running your Appium test. This functionality allows you to run tests that require media files (photos and videos) from the iOS or Android photo gallery.
There are two options to use this feature, depending on your usecase. We suggest using the first option, which will use TestingBot's custom uploadMedia capability.
TestingBot Upload Capability
To use this functionality, you can use the uploadMedia capability, specifying an array of URLs to image files or video files.
You can specify URLs to publicly downloadable media files, or you can use TestingBot Storage to upload the files to TestingBot and use the tb:// URLs.
Upload files to TestingBot
curl -u api_key:api_secret \
-X POST "https://api.testingbot.com/v1/storage" \
-F "file=@/path/to/app/file/media.png"
The API response will include a tb:// URL, which you can use in the uploadMedia capability.
{
"app_url": "tb://edb4f4f3ed95a4f46714f3df"
}
Uploads to our TestingBot Storage are automatically deleted after 62 days.
Add the capability to your Appium script
Map<String, Object> tbOptions = new HashMap<>();
tbOptions.put("uploadMedia", new String[]{"tb://edb4f4f3ed95a4f46714f3df"});
options.setCapability("tb:options", tbOptions);
const capabilities = {
'tb:options': {
'uploadMedia': ['tb://edb4f4f3ed95a4f46714f3df']
}
}
capabilities = {
"tb:options" : {
"uploadMedia" : ["tb://edb4f4f3ed95a4f46714f3df"]
}
}
capabilities = {
"tb:options" => {
"uploadMedia" => ["tb://edb4f4f3ed95a4f46714f3df"]
}
}
AppiumOptions options = new AppiumOptions();
var tbOptions = new Dictionary<string, object>();
tbOptions.Add("uploadMedia", new string[] { "tb://edb4f4f3ed95a4f46714f3df" });
options.AddAdditionalAppiumOption("tb:options", tbOptions);
TestingBot will add these files to the Android or iOS device. The metadata of the gallery on the device will be updated automatically, which means the media file will immediately appear in the device's gallery.
-
Android: the media files will be added to the Default Gallery app.
/sdcard/Picturesfor images and/sdcard/Moviesfor videos. -
iOS: the media files will be added to the iOS Camera Roll:
com.apple.mobileslideshow
Appium Push and Pull
Appium provides both pushFile and pullFile functionality. This is supported since Appium Version 1.15.0.
With this approach, the file is pushed when the test has already started. Pushing media files to the iOS or Android device does not guarantee the updating of the metadata in the iOS or Android gallery, which means your media file might not appear (immediately).
Android Example
Push a file to the SD Card of the Android device:
// Push a file to the sdcard
driver.pushFile("/sdcard/Download/sample.png", new File("~/sample.png"));
// Pull file from sdcard
byte[] fileBase64 = driver.pullFile("/sdcard/Download/sample.png");
// Push file - WebdriverIO syntax
const data = Buffer.from("TestingBot").toString('base64');
await driver.pushFile('/sdcard/Download/sample.txt', data);
// Pull file WebdriverIO example
const fileData = await driver.pullFile('/sdcard/Download/sample.txt');
# Push a file
dest_path = '/sdcard/Download/sample.txt'
driver.push_file(dest_path, 'TestingBot'.encode("utf-8"))
# Pull file
file_base64 = driver.pull_file(dest_path)
# Push a file
driver.push_file('/sdcard/Download/sample.png', File.read('/Users/testingbot/Desktop/sample.png'))
# Pull file
driver.pull_file('/sdcard/Download/sample.png')
// Push a file
driver.PushFile("/sdcard/Download/sample.png", new FileInfo("/Users/testingbot/Desktop/sample.png"));
// Pull file
byte[] fileBase64 = driver.PullFile("/sdcard/Download/sample.png");
iOS Example
Before using the pull and push functionality, you'll need to ensure that:
-
Your iOS app must have set the
UIFileSharingEnabledkey totruein theInfo.plistfile, which enables file sharing. -
Alternatively, you can set the
LSSupportsOpeningDocumentsInPlacekey totruein theInfo.plistto expose your app's folder in the Files app on the iOS device.
The destination path to push a file is in the following format: @<app_bundle_id>:documents/<image_name>.<extension>
// Push an image file
driver.pushFile("@com.testingbot.Sample-App:documents/sample.png", new File("/Users/testingbot/Desktop/sample.png"));
// Pull file
byte[] fileBase64 = driver.pullFile("@com.testingbot.Sample-App:documents/sample.png");
// Push file WebdriverIO example
const data = Buffer.from("TestingBot").toString('base64');
await driver.pushFile('@com.testingbot.Sample-App:documents/sample.txt', data);
// Pull file WebdriverIO example
const fileData = await driver.pullFile('@com.testingbot.Sample-App:documents/sample.txt');
# Push an image file
driver.push_file('@com.testingbot.Sample-App:documents/sample.png', source_path='/Users/testingbot/Desktop/sample.png')
# Push a text file
dest_path = '@com.testingbot.Sample-App:documents/sample.txt'
driver.push_file(dest_path, 'TestingBot'.encode("utf-8"))
# Pull file
file_base64 = driver.pull_file(dest_path)
# Push an image file
driver.push_file('@com.testingbot.Sample-App:documents/sample.png', File.read('/Users/testingbot/Desktop/sample.png'))
# Pull file
driver.pull_file('@com.testingbot.Sample-App:documents/image.png')
// Push an image file
driver.PushFile("@com.testingbot.Sample-App:documents/image.jpg", new FileInfo("/Users/testingbot/Desktop/sample.png"))
// Pull file
byte[] fileBase64 = driver.PullFile("@com.testingbot.Sample-App:documents/sample.png");