【问题标题】:How to simulate taking picture and give input to the app?如何模拟拍照并给应用程序输入?
【发布时间】:2019-02-10 13:27:00
【问题描述】:

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

【问题讨论】:

  • 如果您扫描了测试“二维码图像”,则可以将其推送到应用程序可以读取的设备。您可以向开发人员询问应用程序读取扫描图像的路径,您可以在同一路径上推送测试图像。如果您需要有关如何将图像文件推送到设备的代码,请告诉我。
  • 如果您能提供示例代码,我将不胜感激。
  • 开发团队正在使用Zxing库扫描二维码。他们不知道 zxing 库使用的位置。是否有任何替代方法来扫描 QR 码。我找到了一个关于解码二维码的教程。但我在向相机应用程序提供屏幕截图输入时遇到问题。

标签: appium appium-ios appium-android


【解决方案1】:

如果您已扫描测试“二维码图像”,则可以将其推送到应用程序可以读取的设备。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 2017-02-24
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多