Discover Our Blog

How to Automate Testing & Reading of QR Code in Your Ruby On Rails Application

How to Automate Testing & Reading of QR Code in Your Ruby On Rails Application


QR Code: Why they are more helpful than a standard standardized identification? Is that they can store more information, including URL connections, content and so on.


Here, I am automating - I mean to say scanning/reading QR code without any mobile or without any scanner.


To make this work, please follow the steps given below:


Step 1:


Locate the existing QR code path in your local storage location.


In below screenshot, I have shown the location of QR code from my local storage location.


This location/path is used to convert into a URL and put into the selenium script.


Below QR_Code is in image format which is in .png format.



Software Testing in Ruby on Rails Application


Figure 1: QR Code in Image Format


Step 2:


This image should be converted into a URL path. It is too simple. Please follow the instruction given below.

  1. Open Google Chrome.
  2. Pick your QR_Code image and drop into google chrome.
  3. Now, the image is converted into the URL. (Please check below image)
  4. Copy URL and put into selenium script.


QR Code Automation - Software Testing in Ruby on Rails Development


Figure 2: QR Image converted into URL


Step 3:


Now, open eclipse


Create Java project


Import required library as follows,


1. Selenium-server-standalone

2. Testng

3. Zxing

4. Zxing-1.7-javase

(Please refer the image given below)



Ruby on Rails Development and Software Testing


Step 4:


Write a script in Eclipse editor.


Import org.openqa.selenium.webdriver;

import org.testng.annotation.Test;

  public class QRAutomation {


        public void testQRCode()


System.setProperty("", "chromedriver.exe path");

WebDriver driver = new chromeDriver();


driver.get(" C:\\Users\\Prateek\\Desktop\\QR_Code_Updated.png");

String qrCodeFileUrl =         driver.findElement(By.tagName("img")).getAttribute("src");

System.out.println("QR Code Image URL is : " +qrCodeFileUrl);

URL urlOfImage = new URL(qrCodeFileUrl);

BufferedImage bufferedImage =;

LuminanceSource luminanceSource = new           BufferedImageLuminanceSource(bufferedImage);

BinaryBitmap binaryBitmap = new BinaryBitmap(new          HybridBinarizer(LuminanceSource));

Result result = new MultiFormatReader().decode(binaryBitmap);

String textInQrCode = result.getText();

System.out.println("The Text in QR Code is : "+textInQrCode);



Step 5:


Understanding the code

  1. Bufferedimage: It is used to handle and manipulate the image data.


  1. To perform the image read-write operation we will import the ImageIO class.


  1. LuminanceSource: The examples are extracted from open source    Java projects.


  1. BufferedImageLuminanceSource: Always use the returned object, and ignore the length of the array.


  1. BinayBitmap: This class is the core bitmap class used by ZXing to represent 1-bit data. Reader objects accept a BinaryBitmap and attempt to decode it.


  1. HybridBinarizer: It is designed for high scale images of QR code with black data on white backgrounds. This Binarizer is the default for the unit tests and the recommended class for library users.


  1.  MultiFormatReader(): By default, it attempts to decode all QR Code formats that the library supports. Optionally, you can provide a hints object to request different behavior, for example only decoding QR codes.

Step 6:


Run the script in Eclipse.


Click on “Run” button





Step 7:


Display the result in a console of the eclipse


After clicking on the Run button, the result/output is displaying on the console of the eclipse. (Please refer the image given below.)



Ruby on Rails Web Application Development with Automated TestingAbove image is used to display the output in console panel.



QR Code Automation Output - Software Testing in ROR Application


Above image display, the actual data stored into the QR code and as same output are printed in eclipse console.


  • Advantages of QR_Code testing:


  1. This is used to test that the actual data matches with expected data.


  1. Security and Authorization is the base of all applications. Hope, the above way of automated testing helps you to test security and confidentiality of data in a better way.


Leave a comment: