QR codes have become a popular way to share information quickly and efficiently. In this article, we will explore how to read the content of a QR code using a combination of Selenium for web automation, TestNG , and ZXing for decoding the QR code.
Reading a QR Code
ZXing (“zebra crossing”) is an open-source, multi-format 1D/2D barcode image processing library implemented in Java.
First step is to add a library into our pom.xml file
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.3</version>
</dependency>
Steps you have to follow :-
- Find the locator of the QR code.
- Take the value of the src tag using getAttribute(“src”).
- Read URL through ImageIO and pass it to a BufferedImage.
- Pass the BufferedImage to BufferedImageLuminanceSource Zxing class.
- Instantiate the class HybridBinarizer, that is responsible to read the QR Code, with BufferedImageLuminanceSource as parameter and create a new BinaryBitmap, to prepare to decode.
- Instantiate a MultiFormatReader calling the decode method passing the BinaryBitmap class,The class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects accept a BinaryBitmap and attempt to decode it.
- The MultiFormatReader is a class which will attempts to decode barcode. The result is a Result class that encapsulates the result of decoding a barcode within an image.
Code Example
package readQR;
import static org.testng.Assert.assertEquals;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
public class readQRCode {
private static WebDriver driver;
BufferedImage bufferImage;
LuminanceSource source;
BinaryBitmap bitmap;
Result result;
String qrInputText = "Read QR code";
@BeforeSuite
public void set_up() {
driver = new ChromeDriver();
driver.get("https://qaplayground.dev/apps/qr-code-generator/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@Test
public void read_qr_code() throws MalformedURLException, IOException, NotFoundException {
driver.findElement(By.cssSelector("input[type=\"text\"]")).sendKeys(qrInputText);
driver.findElement(By.tagName("button")).click();
String qrImageURL = driver.findElement(By.cssSelector(".qr-code > img")).getAttribute("src");
String qrResult = decode_QR_image(qrImageURL);
assertEquals(qrResult, qrInputText);
}
@AfterSuite
public void tear_down() {
driver.quit();
}
private String decode_QR_image(String qrImageURL) throws MalformedURLException, IOException, NotFoundException {
bufferImage = ImageIO.read(new URL(qrImageURL));
source = new BufferedImageLuminanceSource(bufferImage);
bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = new MultiFormatReader().decode(bitmap);
System.out.println("Decoded result: " + result);
return result.getText();
}
}
Explanation of the Code
- Imports: The necessary libraries for Selenium, TestNG, and ZXing are imported.
- WebDriver Setup: The
set_up
method initializes the ChromeDriver and navigates to a QR code generator website. - Test Method: The
read_qr_code
method performs the following actions: Inputs a predefined text into the QR code generator. Clicks the button to generate the QR code. Retrieves the generated QR code image URL. Decodes the QR code image using thedecode_QR_image
method and asserts that the decoded text matches the input text. - Tear Down: The
tear_down
method ensures that the browser is closed after the tests are completed. - Decoding Logic: The
decode_QR_image
method reads the image from the URL, converts it to a format suitable for ZXing, and decodes the QR code to extract the text.
The github example
https://github.com/deepshah201/read-qr-code-selenium
References
https://github.com/zxing/zxing/wiki/Getting-Started-Developing
Conclusion
In this article, we demonstrated how to read the content of a QR code using Selenium, TestNG, and ZXing. This approach can be useful in various applications, such as testing QR code functionality in web applications or automating QR code generation and reading processes.
As always, feel free to share your opinion in the comments! See you next time.
Be sure to clap and follow the writer ️👏️️
Connect