gpt4 book ai didi

appium - 如何模拟拍照并给应用程序输入?

转载 作者:行者123 更新时间:2023-12-05 06:30:38 26 4
gpt4 key购买 nike

我有一个应用程序,我需要在其中扫描二维码。从应用程序中拍照是不可行的,因为我需要同时在多个设备上运行该应用程序,而且它需要有人在场。如何在不扫描的情况下将二维码图像/数据提供给应用程序?有没有什么方法可以模拟拍照并将商店图像作为应用程序的输入?

最佳答案

如果您扫描了测试“QR 代码图像”,那么您可以将其推送到应用程序可以读取的设备。

您可以向开发团队询问应用程序读取扫描图像的路径,并在同一路径上推送测试图像。

下面是如何将图像文件推送到设备的代码以及其他推送/拉取不同文件格式的方法

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");
}
}

关于appium - 如何模拟拍照并给应用程序输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52184473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com