buttons

 

Appium Concepts :(Part-1)-How to Push/Pull Files during runtime(Complete Details)

.pushFile(file),.pullFile(act)

Get a step-by-step walkthrough in the video below!

InAppium, the pushFile command is used to transfer a file from your test machine to a device (Android o iOS).

This can be helpful for scenarios like uploading a file to test file input fields, media players, or importing data.

.pushFile(remotePath, file)
  • Purpose: Upload a file to the device
  • Use case: Push a local file (binary or string) to the device.
  • remotePath: Destination path on the device (e.g., /sdcard/Download/file.txt)
  • file: Can be a raw file buffer, string, or a path (converted to Base64 internally).

Example: Java (Appium Java Client)

  //.pushFile(remotepath, file)

driver.pushFile("/sdcard/download/pushed_sampleImage.png", new File(System.getProperty("user.dir")+"/sampleImage.png"));
System.out.println("Pushed the sampleImage.png to the test device");
.pullFile(remotePath)
  • Purpose: Download a file from the device
  • remotePath: Source path on the device (e.g., /sdcard/Download/file.txt)
  • Returns a Base64-encoded string of the file content.
  • You must decode it to get the original file.

Example: Java (Appium Java Client)

  //.pullFile(remotepath)

byte[] pulledfile=driver.pullFile("/sdcard/download/pushed_sampleImage.png");
Files.write(Paths.get("pulled_sampleImage.png"), pulledfile);
System.out.println("File pulled & saved as pulled_sampleImage.png");

Summary:

package com.appiumguide.appiumconcepts;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;

import java.io.File;
import java.io.IOException;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;

/**
* Script Details - Appium Concepts : Part-1 : How to Push/Pull Files during runtime(Complete Details) using methods .pushFile(file) & .pullFile(act)
*
* appium-java-client version: Latest
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/


public class PushPullFiles_Part1 {

private AndroidDriver driver;

@BeforeTest
public void setup() throws MalformedURLException {
UiAutomator2Options cap=new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("Pixel8");


driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(100));

}


@Test
public void test() throws InterruptedException, IOException {

//.pushFile(remotepath, file)

driver.pushFile("/sdcard/download/pushed_sampleImage.png", new File(System.getProperty("user.dir")+"/sampleImage.png"));
System.out.println("Pushed the sampleImage.png to the test device");

Thread.sleep(8000);

//.pullFile(remotepath)

byte[] pulledfile=driver.pullFile("/sdcard/download/pushed_sampleImage.png");
Files.write(Paths.get("pulled_sampleImage.png"), pulledfile);
System.out.println("File pulled & saved as pulled_sampleImage.png");

}


@AfterTest
public void teardown() {

if(driver!=null) {
driver.quit();
System.out.println("Test Completed");
}

}

}

GitHub Link:


No comments:

Post a Comment