Two-factor authentication with Appium
You can use Perfecto APIs to access third-party applications on your mobile device. Two-factor authentication is a great way to allow your users to protect themselves against getting their accounts hacked. It is one of dozens of flows that require accessing a third party application on the device.
You can utilize Perfecto;s OS level control to implement any one of these flows, whether it's an incoming call flow, switching context to the Facebook app, or testing the background/foreground scenario of your application.
When users want to authenticate, they will use their personal access license, such as user name and password, plus a one-time valid, dynamic passcode consisting of digits. The code is sent to their mobile device by SMS or via a special app.
Implement this flow in your Appium script
Lets have a look at the example of the following two-factor authentication login flow with Salesforce. The challenging part here is the retrieval of the authentication code from the SMS application. It gets tricky after this step.
The following image shows how the login verification code is received through SMS.
The standard Appium APIs do not allow you to access third-party applications. This example uses the Perfecto APIs to implement the following flow:
-
Launch the SMS application.
-
Retrieve the verification code.
-
Launch the Salesforce1 application.
-
Enter the retrieved verification code.
The following sample code implements this flow.
Map<String, String> startapp = new HashMap<>();
startapp.put("name", "Messages");
Object result = driver.executeScript("mobile:application:open", startapp);
String command = "mobile:edit-text:get";
Map<String, String> gettext = new HashMap<>();
gettext.put("label", "Use");
gettext.put("label.direction", "left");
gettext.put("timeout", 60);
String code = (String)driver.executeScript(command, gettext);
startapp.put("name", "Salesforce1");
driver.executeScript("mobile:application:open", startapp);
driver.findElementByXPath("//*[@name=\"Verification Code\"][2]").sendKeys(code);