I need to scan QR Code in my Automation Web— Java

nadyop
2 min readJan 8, 2024

--

My Automation is Selenium Java based, and using chrome web driver. So, can I scan the QR Code on my automation?

Background

I have feature which need to scan the QR Code when user wanna add to cart. On the scenario has step for “When user scan the QR Code”. Googling, Found that can be done by using Fake Webcam on Chrome Webdriver. By using it, simply the process is to mock the web camera with proper video format (y4m/mjpeg).

A file with .mjpeg (Motion JPEG) extension is a video file that is created by compressing an individual video frame as a JPEG image.

Challenge

  • Scanning of QR code to get the data to be displayed on website and make the flow running on the automation with ease.
  • Using data (QR code) which can be changed according to user needs.

Prepare for the image saved on resources by mjpeg format.

Setup Chrome Driver

public class FakeWebCamSetup implements ChromeSetup {


public static String videoPath = "";
public static int permission = 0;


public static void setVideoPath(String videoPath){
FakeWebCamSetup.videoPath = videoPath;
}

public static void setPermission(int permission){
FakeWebCamSetup.permission = permission;
}

@Override
public void setup(ChromeOptions options) {
Map<String, Object> contentSettings = new HashMap<>();

contentSettings.put("profile.managed_default_content_settings.media_stream",permission);
options.setExperimentalOption("prefs", contentSettings);

String pathToVideo = System.getProperty("user.dir") + videoPath;

options.addArguments(
"--allow-file-access",
"--allow-file-access-from-files",
"--use-fake-ui-for-media-stream",
"--use-file-for-fake-video-capture=" + pathToVideo,
"--use-fake-device-for-media-stream");
}
}

Afterward, its accessible on cucumber step:

@Given("User scan the qr code")
public void userScanTheQrCode(DataTable dt) {
String url = dt.asMap().get("filename");
FakeWebCamSetup.setVideoPath("/src/test/resources/videos/QR/QRCode1.mjpeg");

SetupProperties.setProperties("com.nad.qa.chrome.ui.main.setups",
"FakeWebCamSetup");
}

On cucumber feature usable like this:

Given User scan the qr code
| filename | QRCode1 |

Conclusion

All done set to test the web applications uing a fake webcam by Chrome driver. If wanna adding some new QR Codes, just to adding new mjpeg on resources directory.

Sources

--

--