Read a QR Code content with Appium and zxing

Easily read a QR Code and use it in your test

Elias Nogueira
assert(QA)
Published in
3 min readJun 11, 2018

--

There is a post called Read a QR Code with Selenium and zxing if may you wan to do the same for web automation.

The problem

Having some QR Code to scan from an app is getting common like add your contact in another phone, distribute your contact or even validate a financial transaction.

To guarantee an end-to-end (e2e) testing through the application that has a QR Code, and you need to read it, an approach to get the image is necessary.

Different from a web page, where you can save it or even convert it in a Base64 String, you cannot get the image resource (in an easily way) from any platform (Android or iOS).

The Solution

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 some tool or API to read the QR Code content.

How to read the QRCode Content?

The ZXing (“zebra crossing”) 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.

The first thing we need to do is add the ZXing library to our project classpath. Bellow the snippet in a pom.xml file (Maven)

<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>

With ZXing we can read the QR Code content in different ways. In this solution, we gonna do this by a BufferedImage.

  1. Pass the BufferedImage to BufferedImageLuminanceSource Zxing class
  2. Instantiate a MultiFormatReader calling the decode method passing the BinaryBitmap. The MultiFormatReader is a convenience class and will attempt to decode all barcode formats that the library supports. The result is a Result class that encapsulates the result of decoding a barcode within an image

How about Appium?

With Appium (a mobile test automation framework) you can apply the solution: take a device screenshot, get the element, use some code to crop the image and read the QR Code content.

To do this 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.

The code below does that and return a BufferedImage.

How to implement this solution?

We have almost everything: the code to read a QRCode content and a code to crop the QR Code inside an app.

Now we just want to find the element on the device and call the auxiliary methods.

Full code example

You can access the GitHub repo to:

  • Have an app to run the example
  • Have the fully functional code

You can do this approach with any other mobile testing framework using Java.

--

--

Elias Nogueira
assert(QA)

Lead Software Engineer in Test

Recommended from Medium

Lists

See more recommendations