Fingerprint authentication in Android Emulator using Appium

Some important points

  1. FaceId is currently not supported as of Jan 2020 in Android emulators as well as real phones
  2. Fingerprint simulation is supported in only Emulators and not in Real android phones
  3. You need to register the fingerprint in the emulator before doing simulation
  4. Then You can send matching and Non matching fingerprint authentication
  5. This is generally used in Biometrics applications

Below code demonstrates how you can simulate the fingerprint in Android Emulator.
package org.softpost.android.simulator;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * Created by Sagar on 10-07-2016.
 */
public class FingerprintTest {
    WebDriver driver;
    private  String securityText = "//android.widget.TextView[@text='Security']";
    private  String touchSensorText = "//android.widget.TextView[@text='Touch the sensor']";
    private  String doneText = "//android.widget.Button[@text='DONE']";

    @Test
    public void test1() throws Exception{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "android");

        caps.setCapability("deviceName", "Pixel 9");
        caps.setCapability("appPackage", "com.android.settings");
        caps.setCapability("appActivity","com.android.settings.Settings");
        caps.setCapability("automationName","uiautomator2");

try {
    driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    Thread.sleep(5000);
    TouchAction action = new TouchAction(((AndroidDriver)driver));
    int height = driver.manage().window().getSize().height;
    int width = driver.manage().window().getSize().width;
    System.out.println("Dimensions of screen ->  " + width + " x " + height);
    action.press(new PointOption().point(94, height - 1000))
            .waitAction(new WaitOptions().withDuration(Duration.ofSeconds(1)))
            .moveTo(new PointOption().point(94, 400)).release();
    MultiTouchAction multiAction = new MultiTouchAction(((AndroidDriver)driver));
    multiAction.add(action).perform();
    Thread.sleep(5000);

    WaitUntilElementIsDisplayed(By.xpath("//android.widget.TextView[contains(@text,'Security')]"),10);
    driver.findElement(By.xpath("//android.widget.TextView[contains(@text,'Security')]")).click();
    driver.findElement(By.xpath("//android.widget.TextView[@text='Fingerprint']")).click();
    driver.findElement(By.xpath("//android.widget.Button[@text='NEXT']")).click();
    Thread.sleep(5000);
    driver.findElement(By.xpath("//android.widget.TextView[@text='Fingerprint + PIN']")).click();
    driver.findElement(By.xpath("//android.widget.Button[@text='YES']")).click();
    driver.findElement(By.xpath("//android.widget.Button[@text='OK']")).click();

    WebElement pin = driver.findElement(By.xpath("//android.widget.EditText[@resource-id='com.android.settings:id/password_entry']"));
    pin.sendKeys("1234");
    driver.findElement(By.xpath("//android.widget.Button[@text='NEXT']")).click();
    Thread.sleep(2000);
    pin.sendKeys("1234");
    driver.findElement(By.xpath("//android.widget.Button[@text='CONFIRM']")).click();
    driver.findElement(By.xpath("//android.widget.Button[@text='DONE']")).click();
    WaitUntilElementIsDisplayed(By.xpath(touchSensorText), 10);

    for (int i = 1; i <= 3; i++)
    {
        Thread.sleep(5000);
        executeProcess("adb -e emu finger touch 1");
    }

    WaitUntilElementIsDisplayed(By.xpath(doneText), 10);

    driver.findElement(By.xpath("//android.widget.Button[@text='DONE']")).click();


    //Let's assume you are on fingerprint page
    //send the fingerprint
    ((AndroidDriver) driver).fingerPrint(1);


}catch (Exception ex) {
    ex.printStackTrace();
    System.out.println(driver.getPageSource());

}finally {

    driver.quit();
}
    }

    protected boolean WaitUntilElementIsDisplayed(By by, int seconds)
    {
        try
        {
            WebDriverWait wait = new WebDriverWait(driver, seconds);
            wait.until(ExpectedConditions.visibilityOfElementLocated(by));
            return true;
        }
        catch (Exception ex)
        {
            System.out.println("Exception while waiting {ex.Message}" + ex.getMessage());
            return false;
        }
    }

    private void executeProcess(String cmd){
        ProcessBuilder processBuilder = new ProcessBuilder();

        // Run a shell command
        processBuilder.command("bash", "-c", cmd);

        // Run a shell script
        //processBuilder.command("xyz.sh");

        // if running on windows

        // Run a command
        //processBuilder.command("cmd.exe", "/c", cmd);


        try {

            Process process = processBuilder.start();

            StringBuilder output = new StringBuilder();

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

            BufferedReader ereader = new BufferedReader(
                    new InputStreamReader(process.getErrorStream()));

            String eline;
            while ((eline = ereader.readLine()) != null) {
                output.append(eline + "\n");
            }

            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("Success");
                System.out.println(output);

            } else {
                System.out.println("Failure");
                System.out.println(output);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Youtube | Facebook | Twitter | Website Home | Online Grocery Shopping Brisbane