Skip to content

Instantly share code, notes, and snippets.

@cozyloon
Created March 29, 2024 13:54
Show Gist options
  • Save cozyloon/22ca890fc202e6afbabfa76657fc19de to your computer and use it in GitHub Desktop.
Save cozyloon/22ca890fc202e6afbabfa76657fc19de to your computer and use it in GitHub Desktop.
QR code Reader Selenium Java
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>selenium</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.18.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.3</version>
</dependency>
</dependencies>
</project>
import com.google.zxing.BinaryBitmap;
import com.google.zxing.NotFoundException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
public class QR {
@Test
void daily20() throws IOException, NotFoundException {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://qaplayground.dev/apps/qr-code-generator/");
driver.findElement(By.tagName("input")).sendKeys("I am an Automation QA");
driver.findElement(By.tagName("button")).click();
String qrCodeImageUrl = driver.findElement(By.xpath("//div[@class='qr-code']//img")).getAttribute("src");
BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeImageUrl));
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(qrCodeImage)));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);
String decodedText = qrCodeResult.getText();
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(decodedText, "I am an Automation QA");
softAssert.assertAll();
driver.quit();
}
@Test
void daily20LocalSaveQR() throws IOException, NotFoundException {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://qaplayground.dev/apps/qr-code-generator/");
driver.findElement(By.tagName("input")).sendKeys("I am an Automation QA");
driver.findElement(By.tagName("button")).click();
WebElement qrCodeImageElement = driver.findElement(By.xpath("//div[@class='qr-code']//img"));
File screenshot = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.visibilityOf(qrCodeImageElement)).getScreenshotAs(OutputType.FILE);
BufferedImage fullImage = ImageIO.read(screenshot);
File qrCodeFile = new File("qr_code.png");
FileUtils.copyFile(screenshot, qrCodeFile);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(fullImage)));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);
String decodedText = qrCodeResult.getText();
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(decodedText, "I am an Automation QA");
softAssert.assertAll();
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment