使用Appium上传/下载文件(push文件、pull文件)
1 package com.lx.class1; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.net.URL; 6 import java.util.concurrent.TimeUnit; 7 8 import org.apache.commons.codec.binary.Base64; 9 import org.apache.commons.io.FileUtils; 10 import org.openqa.selenium.remote.DesiredCapabilities; 11 12 import io.appium.java_client.android.AndroidDriver; 13 14 public class Demo4 { 15 public static void main(String[] args) throws Exception { 16 AndroidDriver driver; 17 DesiredCapabilities cap=new DesiredCapabilities(); 18 cap.setCapability("automationName", "Appium");//appium做自动化 19 // cap.setCapability("app", "C:\\software\\jrtt.apk");//安装apk 20 // cap.setCapability("browserName", "chrome");//设置HTML5的自动化,打开谷歌浏览器 21 cap.setCapability("deviceName", "S4");//设备名称 22 cap.setCapability("platformName", "Android"); //安卓自动化还是IOS自动化 23 cap.setCapability("platformVersion", "4.4"); //安卓操作系统版本 24 cap.setCapability("udid", "192.168.56.101:5555"); //设备的udid (adb devices 查看到的) 25 cap.setCapability("appPackage","com.cyanogenmod.filemanager");//被测app的包名 26 cap.setCapability("appActivity",".activities.NavigationActivity");//被测app的入口Activity名称 27 cap.setCapability("unicodeKeyboard", "True"); //支持中文输入 28 cap.setCapability("resetKeyboard", "True"); //支持中文输入,必须两条都配置 29 cap.setCapability("noSign", "True"); //不重新签名apk 30 cap.setCapability("newCommandTimeout", "30"); //没有新命令,appium30秒退出 31 32 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),cap);//把以上配置传到appium服务端并连接手机 33 //隐式等待 34 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 35 Thread.sleep(3000); 36 //上传文件 37 File file = new File("C:\\test.txt"); 38 String content = null; 39 try { 40 content = FileUtils.readFileToString(file); 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 byte[] data = Base64.encodeBase64(content.getBytes()); 45 driver.pushFile("sdcard/test.txt", data); 46 //下载文件举例: 47 byte[] resultDate = driver.pullFile("sdcard/test.txt"); 48 System.out.println(new String(Base64.decodeBase64(resultDate))); 49 50 //下载文件夹: 51 driver.pullFolder("tmp/"); 52 //把android的tmp目录拷贝到临时文件夹 53 System.out.println(driver.pullFolder("tmp/")); 54 55 } 56 57 }