3

I have a app in which i need to scan the QR code. Taking the picture from the app is not feasible as i need to run the app in multiple devices at once and it require human presence. How can i provide the QR code image/data to the app without scanning? Is there any way possible to simulate taking of picture and give store image as input to app?

Suban Dhyako
  • 2,436
  • 4
  • 16
  • 38
  • 1
    If you have scanned test "QR Code image" then you can push it to the device from where app can read it. You can ask dev about the path from where app is reading the scanned image, at same path you can push the test image. If you need code for how to push image file to device then let me know. – Amit Jain Sep 06 '18 at 07:36
  • i will be grateful if you can provide me sample code. – Suban Dhyako Sep 06 '18 at 08:04
  • Developer team are using Zxing library to scan QR Code. They are not aware of the location use by zxing library. Is there any alternate approach to scan QR code. I found a tutorial about decoding the qr code. But I have problem giving screenshot input to the camera app. – Suban Dhyako Sep 12 '18 at 05:55

1 Answers1

1

If you have scanned test "QR Code image" then you can push it to the device from where app can read it.

You can ask dev team about the path from where app is reading the scanned image, and at same path you can push the test image.

Below is the code for how to push image file to device and other methods to push/pull different file formats

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");
            }
}
Amit Jain
  • 4,389
  • 2
  • 18
  • 21