Appium 如何模拟拍照并向应用程序提供输入?
我有一个应用程序,我需要扫描二维码。从应用程序中拍照是不可行的,因为我需要同时在多个设备中运行应用程序,并且需要人在场。如何在不扫描的情况下向应用程序提供二维码图像/数据?是否有任何方法可以模拟拍照并将商店图像作为应用程序的输入?如果您已扫描测试“二维码图像”,则可以将其推送到应用程序可以读取的设备上 您可以向开发团队询问应用程序读取扫描图像的路径,并在同一路径上推送测试图像 下面是如何将图像文件推送到设备的代码,以及推/拉不同文件格式的其他方法Appium 如何模拟拍照并向应用程序提供输入?,appium,appium-ios,appium-android,Appium,Appium Ios,Appium Android,我有一个应用程序,我需要扫描二维码。从应用程序中拍照是不可行的,因为我需要同时在多个设备中运行应用程序,并且需要人在场。如何在不扫描的情况下向应用程序提供二维码图像/数据?是否有任何方法可以模拟拍照并将商店图像作为应用程序的输入?如果您已扫描测试“二维码图像”,则可以将其推送到应用程序可以读取的设备上 您可以向开发团队询问应用程序读取扫描图像的路径,并在同一路径上推送测试图像 下面是如何将图像文件推送到设备的代码,以及推/拉不同文件格式的其他方法 import java.awt.image.Bu
import java.awt.image.BufferedImage;
import io.appium.java_client.android.AndroidDriver;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@Test
public class pushFileTest {
public static AndroidDriver<WebElement> _driver;
@BeforeClass
public void setUpAppium() throws InterruptedException, IOException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("platformVersion","5.1");
cap.setCapability("platformName","Android");
cap.setCapability("deviceName","ZX12222D");
cap.setCapability("appPackage","io.appium.android.apis");
cap.setCapability("appActivity","ApiDemos");
//System.out.println("Before calling appium");
_driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4725/wd/hub"), cap);
//System.out.println("After calling appium");
}
@Test
public void pullImageFileFromMobileSDCardTest() throws IOException {
byte[] returnData = _driver.pullFile("/storage/sdcard1/IMG_20140828_072840.jpg");
//System.out.println("Base 64 Converted String received from mobile :: " + returnData);
BufferedImage image=ImageIO.read(new ByteArrayInputStream(returnData));
ImageIO.write(image, "jpg", new File("C:\\eclipse","snap.jpg"));
}
/* Test Case to pull log file from mobile device*/
@Test
public void pullTextFileFromMobileSDCardTest() throws IOException {
byte[] returnData = _driver.pullFile("/storage/sdcard1/mili_log.txt");
//System.out.println(" Printing Text of File received from mobile :: " + new String(Base64.decodeBase64(returnData)));
File fs = new File("C:\\eclipse\\MobileFile.txt");
FileOutputStream fos = new FileOutputStream(fs);
fos.write(returnData);
fos.flush();
fos.close();
}
@Test
public void pushImageFileToMobileTest() throws IOException {
File fi = new File("C:\\eclipse\\img1.jpg");
byte[] fileContent = Files.readAllBytes(fi.toPath());
_driver.pushFile("/storage/sdcard1", fileContent);
}
@Test
public void pushTextFileToMobileTest() throws IOException {
File fi = new File("C:\\eclipse\\MobileFile.txt");
byte[] data = Files.readAllBytes(fi.toPath());
System.out.println("Base 64 Converted String sent to mobile :: " + data);
_driver.pushFile("/storage/sdcard1/appium.txt",data);
}
public void pullVideoFileFromMobileSDCardTest() throws IOException {
byte[] returnData = _driver.pullFile("/storage/sdcard1/VideoIconfile.mp4");
//System.out.println(" Printing Text of File received from mobile :: " + new String(Base64.decodeBase64(returnData)));
//File fs = new File("C:\\eclipse\\video.mp4");
FileOutputStream fos = new FileOutputStream("C:\\eclipse\\video.mp4");
fos.write(returnData);
fos.flush();
fos.close();
}
@AfterTest(alwaysRun= true)
public void tearDown(){
if (_driver!= null )
_driver.quit();
System.out.println("tearDown() :: driver.quit() executed");
}
}
导入java.awt.image.buffereImage;
导入io.appium.java_client.android.AndroidDriver;
导入java.io.ByteArrayInputStream;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.net.URL;
导入javax.imageio.imageio;
导入org.apache.commons.codec.binary.Base64;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.remote.CapabilityType;
导入org.openqa.selenium.remote.DesiredCapabilities;
导入org.testng.annotations.postest;
导入org.testng.annotations.BeforeClass;
导入org.testng.annotations.Test;
@试验
公共类pushFileTest{
公共静态AndroidDriver\u驱动程序;
@课前
public void setUpAppium()引发InterruptedException,IOException{
DesiredCapabilities=新DesiredCapabilities();
上限设定能力(“平台版”、“5.1”);
cap.setCapability(“平台名”、“安卓”);
cap.setCapability(“deviceName”、“ZX1222D”);
cap.setCapability(“appPackage”、“io.appium.android.api”);
cap.setCapability(“appActivity”、“ApiDemos”);
//System.out.println(“调用appium之前”);
_驱动程序=新的AndroidDriver(新的URL(“http://127.0.0.1:4725/wd/hub"(香港法例第六章),;
//System.out.println(“调用appium之后”);
}
@试验
public void pullImageFileFromMobileSDCardTest()引发IOException{
字节[]returnData=_driver.pullFile(“/storage/sdcard1/IMG_20140828_072840.jpg”);
//System.out.println(“从移动设备接收的Base 64转换字符串::”+returnData);
BuffereImage image=ImageIO.read(新的ByteArrayInputStream(返回数据));
write(图像,“jpg”,新文件(“C:\\eclipse”,“snap.jpg”);
}
/*从移动设备中提取日志文件的测试用例*/
@试验
public void pullTextFileFromMobileSDCardTest()引发IOException{
字节[]returnData=_driver.pullFile(“/storage/sdcard1/mili_log.txt”);
//System.out.println(“打印从手机接收的文件文本::”+新字符串(Base64.decodeBase64(returnData));
文件fs=新文件(“C:\\eclipse\\MobileFile.txt”);
FileOutputStream fos=新的FileOutputStream(fs);
fos.写入(返回数据);
fos.flush();
fos.close();
}
@试验
public void pushImageFileToMobileTest()引发IOException{
File fi=新文件(“C:\\eclipse\\img1.jpg”);
byte[]fileContent=Files.readAllBytes(fi.toPath());
_driver.pushFile(“/storage/sdcard1”,fileContent);
}
@试验
public void pushTextFileToMobileTest()引发IOException{
File fi=新文件(“C:\\eclipse\\MobileFile.txt”);
byte[]data=Files.readAllBytes(fi.toPath());
System.out.println(“发送到移动设备的Base 64转换字符串::”+数据);
_pushFile(“/storage/sdcard1/appium.txt”,数据);
}
public void pullVideoFileFromMobileSDCardTest()引发IOException{
字节[]returnData=_driver.pullFile(“/storage/sdcard1/VideoIconfile.mp4”);
//System.out.println(“打印从手机接收的文件文本::”+新字符串(Base64.decodeBase64(returnData));
//文件fs=新文件(“C:\\eclipse\\video.mp4”);
FileOutputStream fos=新的FileOutputStream(“C:\\eclipse\\video.mp4”);
fos.写入(返回数据);
fos.flush();
fos.close();
}
@后测(alwaysRun=true)
公共无效拆卸(){
如果(_driver!=null)
_driver.quit();
System.out.println(“tearDown()::driver.quit()已执行”);
}
}
如果您已扫描测试“二维码图像”,则可以将其推送到设备上,应用程序可以从中读取。您可以向dev询问应用程序读取扫描图像的路径,在相同的路径下,您可以推送测试图像。如果您需要如何将图像文件推送到设备的代码,请告诉我。如果您能提供示例代码,我将不胜感激。开发团队正在使用Zxing库扫描二维码。他们不知道中兴图书馆使用的位置。是否有其他方法扫描二维码。我找到了一本关于解码二维码的教程。但是我在给摄像头应用程序输入截图时遇到了问题。