Automate and Validate QR Code Details

     I had a scenario to automate and validate the QR code data. There are some contact details encoded within the QR code, I have to validate the contact details against the expected result. I got a solution from Google’s zxing library and implemented those in my script and validated the QR code data. I have achieved this with help of the following two libraries from Google,

  • Google zxing – core
  • Google zxing – javase

Following are the details of dependencies that I used to achieve this scenario:

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


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

We can use this capability in different ways. Here, I would like to share the following:

  • Validate QR code from the web application directly.
  • Validate QR code from the local system.
Validate QR code from the web application directly

     In this case, you have to get the QR code image from your application. Create an object of URL class, then pass the URL class object to store the file as an image. Once the image ready, then process it using Google zxing’s method and convert the image to a binary bitmap. Later capture the details of QR code using Google zxing’s decoding method and store it into a Result’s object. Finally, validate that decoded data against expected result using TestNG Assert. Below is the logic:

String QRCodeImage=”//img[@src=’https://assets.outlook.com/qrprod/-1603550372.png ‘] “;
String urlOfQRCode = driver.findElement(By.xpath(QRCodeImage)).getAttribute(“src”);
// Create an object of URL Class
URL url = new URL(urlOfQRCode);
// Pass the URL class object to store the file as image
BufferedImage bufferedimage = ImageIO.read(url);
// Process the image
LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedimage);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
// To Capture details of QR code
Result result = new MultiFormatReader().decode(binaryBitmap);
Assert.assertEquals(“Expected_Result”, result.getText());

Validate QR code from the local system

     In this case, you should have the QR code image ready in your local system path. Once the QR code ready, pass the QR code object to store the file as an image. Once the image ready, then process it using Google zxing’s method and convert the image to a binary bitmap. Later capture the details of the QR code using Google zxing’s decoding method and store it into a Result’s object. Finally, validate that decoded data against expected result using TestNG Assert. Below is the logic:

// Pass the QR code object to store the file as image
BufferedImage bufferedimage = ImageIO.read(new File(“your_QR_code_image_path”));
// Process the image
LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedimage);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
// To Capture details of QR code
Result result = new MultiFormatReader().decode(binaryBitmap);
Assert.assertEquals(“Expected_Result”, result.getText());

     Try to use the above logic in your code if you have any situation to validate the QR code details during automation execution.

make it perfect!