Run tests with QR code scan
Below you can find information on how to run tests, which include QR code scan
Prerequisites
- Existing native application with a feature, related to the QR code scan.
- The app is installed on a device with image injection enabled.
More details can be found here and in our API reference collection. - The tests can operate with system alerts
For example, using the Appium command. - Access key is generated.
Step 1: Add executing of the QR code upload API
We have the specific API action for the QR code upload in our public API.
The action accepts a QR code as the file, which should be sent in the “multipart/form-data” parameter.
Below you can find an example for popular Java API framework.
Rest-assured:
public void uploadQrCode(String deviceSerial, File qrCode) {
RestAssured.given().auth().
oauth2(<ACCESS_KEY>).
baseUri("https://app.mobitru.com/billing/unit/"+
<TEAM_ACCOUNT_NAME>
+"/automation/api").
header("X-Content-Type", "image/png").
header("x-File-Name", qrCode.getName()).
contentType("multipart/form-data").
multiPart("file", qrCode).
post("device/{serial}/injection/image", deviceSerial).then().
statusCode(HTTP_NO_CONTENT);
}
Step 2: Cover the QR code scan pop-up
It depends of the Platform, but, usually, the two kinds of behavior should be covered:
- For Android: “allow permission” action on the Camera dialog.
- For iOS: “accept” action on simple system Alert.
The both pop-ups usually activate when the QR code scan process is triggered.
Also, here is an example of a single method for both items in Java + Appium:
@AndroidFindBy(xpath = "//*[@resource-id='com.android.permissioncontroller:id/permission_allow_one_time_button']")
private WebElement allowPermissionsOnceButton;
public void openUploadQrCodeScreen() {
//example of trigger the QR code scan process
applyPromoButton.click();
//wait for the process is started
new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(1))
.ignoring(WebDriverException.class)
.until(ExpectedConditions.invisibilityOf(applyPromoButton));
//make different actions depending on the platform
if (driver.getCapabilities().getPlatformName().equals(Platform.ANDROID)){
// wait for allow permission button and click on it
waitForElementDisplayed(allowPermissionsOnceButton);
allowPermissionsOnceButton.click();
}else{
// wait for simple system alert and perform accept
new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(1))
.ignoring(WebDriverException.class)
.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
}
}
Step 3: Use both actions in a test
In the end, you can cover the full scenario by using both actions one after another.
Below you can find an example for a popular Java Test framework:
@Test(description = "Check apply promo from qr code")
public void checkPromoApply() {
//read a QR code image to file object
File qrCode = new File(IOUtils.resourceToURL("qr.png", this.getClass().getClassLoader()).getFile());
//trigger the QR code scan process
openUploadQrCodeScreen();
//upload the QR code image, which will be applied automatically
uploadQrCode(udid, qrCode);
//actions after apply the QR code...
}
Note:
If you don’t have an Application with the QR code scan but would like to try the described steps, please use our open-source demo apps for Android and iOS.