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

Cryptex Technologies
4 min readAug 1, 2018

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.

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.

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)

Step 4:

Write a script in Eclipse editor.

Import org.openqa.selenium.webdriver;import org.testng.annotation.Test;  public class QRAutomation {        @Test        public void testQRCode(){System.setProperty("webdriver.chrome.driver", "chromedriver.exe path");WebDriver driver = new chromeDriver();driver.manage().window().maximize();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 = ImageIO.read(urlOfImage);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.
  2. ImageIO.read: To perform the image read-write operation we will import the ImageIO class.
  3. LuminanceSource: The examples are extracted from open source Java projects.
  4. BufferedImageLuminanceSource: Always use the returned object, and ignore the length of the array.
  5. 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.
  6. 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.
  7. 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.

Step6:

Run the script in Eclipse.

Click on “Run” button

Step7:

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

Above image is used to display the output in console panel.

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

--

--

Cryptex Technologies

Cryptex specializes in developing open source web applications and software solutions across all domains and verticals using Ruby on Rails (ROR) technology