Is there a way to automate scanning (bar code, QR code etc.)?

I’m trying to automate a scanning app. Prime activity is with camera scanning a barcode or QR code. How would anyone should approach this ? Is it …

  1. Forget scanning, use some images of barcode/qr code instead and ‘mock’ scanning.
  2. Do it with an attached device on a stand and stick an image front of the camera

I can’t seem to get my head around it …

You ?

3 Likes

Well, for app’s functional testing you want to cover both. But if the core goal is to test the code scanning functionality, then whichever is easier (likely #1) will do, since it technically works the same internally I would assume, just the source data originated differently, but the type of data is essentially same.

I would try tackling #1 first since that should work with simulator and device, if the app offers scanning off an image on the device.

Try tackle #2, once you got #1 working. For #2, I’d have a test setup with printed codes on paper, cardboard, etc. to test against like being at a store, etc. and manually swap the hardcopy images. But I’d also have a soft copy image setup where the bar/QR codes come from a digital display (monitor, TV, digital picture frame, etc.) positioned in front of the device camera and the device camera scans off the digital display. This latter setup would then be automatable and can programmatically swap source images in the digital display.

As for specifics on automating this with Appiums, others can help you. I haven’t touched Appium much to give guidance code/setup wise.

I’m actually facing the same problem. Any update on this topic please?

with iOS and Simulator we can add any image and use it. thus you can just add your test images and select them in Camera Roll as photos taken :slight_smile:

you must add photo while simulator is open. you just set correct Simulator ID and that is it.

public void addPhoto() {
        //xcrun instruments -s device = list all simulators
        //xcrun simctl addphoto C2C451F0-9D33-4872-8615-4DE41D1A1017 /Users/Aleksei/Documents/test2.png
        try {
            String[] cmd = {"xcrun", "simctl", "addphoto", "C2C451F0-9D33-4872-8615-4DE41D1A1017", "/Users/Aleksei/Downloads/userPhoto.png"};
            Process process = Runtime.getRuntime().exec(cmd);
            BufferedReader bufferedReader = new BufferedReader(new
                    InputStreamReader(process.getErrorStream()));

            String lsString;
            String output = "";
            while ((lsString = bufferedReader.readLine()) != null) {
                output += lsString;
            }
            System.out.println(output);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
2 Likes

In Android, I have tried one workaround to resolve this but you cannot say it as an Automation testing because it needs manual intervention.
Also it can only be implemented using physical device not with emulator.
Approach:
Take QR code in a file in PC (say .PNG image)
Click scan button using appium.
Point you device camera to QR code and let it focus to scan.
(Synchronize the process with correct time outs)

But, if any feasible solution can be implemented then let me know.

Haven’t tried this solution as we were stopping the suite just before scanning, so I can’t vouch for it but inside of my mind I was looking for something like this, so full credit to @Aleksei for this. It’s not SO other then that I’d mark it as ‘Solved’.
It would be good to know if some one tried this solution successfully.

Thank you for all your help people.

Aleksei Can you help me with this. I always get Invalid device: " "
what are you passing in the string cmd after addphoto, I passed my real device UDID… I am running this on real device

@rubina_sayyed it is for simulator only.

This is now also implemented with appium commands. See https://appiumpro.com/editions for examples

@Aleksei To simplify my life, can you please post a working code for me which scans QR code for real device.

I have a scenario where I have to capture the image of a check to send it as an input to scanning API. I need to automate it. I need to capture front and back of the check. Can anyone help me with the way to do it? Following are the steps:

  1. Tap on the camera icon
  2. Capture the image of front of check
  3. Tap on the camera icon for capturing back of the check
  4. Capture the back of the check
  5. Tap on the Next button

Have you found a solution to mock/feed a camera?

Hi Guys,
I want to automate reading barcode with phone camera. Daily we get around 100 voachars to read. We need to read this voachars in iPhone, any idea how to automate.

I would try to work with application developers on this. They could hardcode a special place in application documents, where the automation script would upload specially prepared images to recognize.

1 Like

When we may expect this get working!

Hi @mykola-mokhnach. Any update or roadmap on the scanning related automation. So many projects especially in postal, e-commerce have camera based scanning and it is becoming more significant for automating those areas also

Unfortunately mobile OS vendors don’t provide any instruments to automate such scenarios. The only viable scenario I know of is still something similar to https://marcelog.github.io/articles/android_emulator_linux_virtual_webcam_video_device.html (fake camera device connected to Android Emulator)

@rubina_sayyed were you able to get this?i am trying to scan a QR code for the real device and i get the same error "Invalid UDID…

Appreciate your response!

@Aleksei what if i have to use the real device?i get Invalid UDID error.

Appreciate your response

tel a bit more about problem. add a bit code and logs pls.

I am trying to automate a QR code using a real device and used the following code, but i get Invalid UDUD error
public void addPhoto() {
//xcrun instruments -s device = list all simulators
//xcrun simctl addphoto C2C451F0-9D33-4872-8615-4DE41D1A1017 /Users/Aleksei/Documents/test2.png
try {
String[] cmd = {“xcrun”, “simctl”, “addphoto”, “C2C451F0-9D33-4872-8615-4DE41D1A1017”, “/Users/Aleksei/Downloads/userPhoto.png”};
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(process.getErrorStream()));

        String lsString;
        String output = "";
        while ((lsString = bufferedReader.readLine()) != null) {
            output += lsString;
        }
        System.out.println(output);
    } catch (Exception e) {
        e.printStackTrace();
    }
}