Read and Verify QR code with Zxing and Appium
In every day life, we are witnessed about new technologies update. This is good for users as it helps to get every process easy and faster. QR code is now very common. Appium does not provide any support to handle QR code. For Automation, its not easy to handle such type of feature. Generally we should avoid to automate features that interact with hardware until we don't have a stable solution.
Here I am sharing solution for Read and Validating QR code with Appium and Zxing which is quite stable.
Approach:
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.Few Words about Zxing
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.Steps by step process
Here are all steps
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
you can create an auxiliary method that receives a MobileElement (the element that contains the QR code) and a File (the device screenshot) to get the points and crop the device screenshot to the element size. We can crop image with the help of X,Y coordinate of QR code with help of function getX() and getY().
private BufferedImage generateImage(MobileElement element, File screenshot) throws IOException { pointYPosition, qrCodeImageWidth, qrCodeImageHeight); This function return buffered image of QR code Now we have done with prerequisites i.e. cropped QR code's image. 3. Decode QR code from Cropped QR code using below functionNow we will pass cropped QR code's image and decodeQRCode function will return decoded informationbehind QR code in string format.
|