Search code examples
androidiosautomationappiumqr-code

Is there any way to automate scan QR code by Appium for IOS and Android?


I have an application runs in Android and IOS but one of the main functionalities depends on scan QR code through mobile camera. So is there any way to do this with Appium?


Solution

  • I don’t think so appium have native functionality to do this. We can use Zxing external library to achieve it.

    Zxing is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. One supported 2D format is the QR code.

    An easy solution is to take a screenshot from the device screen, get the points (width and height) from the element on the device, and crop the image to the element size, so you have an image with just the QR code. Now, you can use Zxing to read the QR code content.

    1. Add Zxing maven dependency to Pom.xml

    <dependency>
    
        <groupId>com.google.zxing</groupId>
    
        <artifactId>core</artifactId>
    
        <version>3.3.0</version>
    
    </dependency>
    
    <dependency>
    
        <groupId>com.google.zxing</groupId>
    
        <artifactId>javase</artifactId>
    
        <version>3.3.0</version>
    
    </dependency>
    

    2. Get QR code image from App using Appium

    private BufferedImage generateImage( MobileElement element, File screenshot) throws IOException {
    
        BufferedImage fullImage = ImageIO.read(screenshot);
    
        Point imageLocation = element.getLocation();
    
    
    
        int qrCodeImageWidth = element.getSize().getWidth();
    
        int qrCodeImageHeight = element.getSize().getHeight();
    
    
    
        int pointXPosition = imageLocation.getX();
    
        int pointYPosition = imageLocation.getY();
    
    
    
        BufferedImage qrCodeImage = fullImage.getSubimage(pointXPosition, pointYPosition, qrCodeImageWidth, qrCodeImageHeight);
    
        ImageIO.write(qrCodeImage, "png", screenshot);
    
    
    
        return qrCodeImage;
    
    }
    

    3. Decode QR code from Image generated using above function

    private static String decodeQRCode(BufferedImage qrCodeImage) throws NotFoundException {
            LuminanceSource source = new BufferedImageLuminanceSource(qrCodeImage);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    
            Result result = new MultiFormatReader().decode(bitmap);
            return result.getText();
        }
    

    4. How to utilise generateImage() and readQRCode()

    public void readQRCode() throws IOException, NotFoundException {
    
       MobileElement qrCodeElement = driver.findElement(By.id("com.eliasnogueira.qr_code:id/qrcode"));
    
       File screenshot = driver.getScreenshotAs(OutputType.FILE);
    
    
    
       String content = decodeQRCode(generateImage(qrCodeElement, screenshot));
    
       System.out.println("content = " + content);
    
    }
    

    So in content is the information have fetched from QR code.

    Reference: step by step guide is here and sample code is here