Join the webinar on ‘Real-time Smart Alerts for Quick Issue Detection and Resolution’ on Dec 3rd.
Close
Simulating Incoming Phone Calls On AndroidSimulating Incoming Phone Calls On Android

Simulating Incoming Phone Calls On Android

November 7, 2018
 by 
 Jonathan Lipps Jonathan Lipps
Jonathan Lipps

In the past, we've covered how to simulate receiving SMS messages on Android emulators. It is also possible to receive and interact with incoming phone calls! This might be relevant for apps that engage with the phone subsystem in some way, or it might just be a good idea to test that nothing goes wrong in your app when someone takes a call while using it.

Why rely on simulations when you have the opportunity to conduct Android apps testing on actual devices? Learn more.

(Of course, this only works on emulators; to achieve the same result on a real device, just make sure the real device has a working SIM card and trigger a call to it using one of the available phone call automation services.)

Test your apps on real devices and get accurate results. Know more.

How can we take advantage of this feature? It's made available in the Appium Java client via the driver.makeGsmCall() method. This method takes two parameters:

  1. A string representing the phone number which the emulator will show as calling it (this should be purely numbers, no dashes or parentheses).
  2. A member of the GsmCallActions enum, namely one of GsmCallActions.CALL, GsmCallActions.CANCEL, GsmCallActions.ACCEPT, or GsmCallActions.HOLD. Each of these defines an action the emulator will take with respect to the particular phone number that has called.
Check out: Automating Biometric Authentication in Android

Basically, to initiate a call to the emulator, we first make a call like this:


driver.makeGsmCall("1234567890", GsmCallActions.CALL);

This will start the device "ringing". At this point it will continue to ring until we follow up with another action. We may, for example, choose to accept the call:


driver.makeGsmCall("1234567890", GsmCallActions.ACCEPT);

At this point the call will be active, and will remain active until we choose to end it:


driver.makeGsmCall("1234567890", GsmCallActions.CANCEL);

Using this series of commands, we can simulate the entire flow of a user receiving a phone call while in our app, accepting the call, continuing to go about her business in the app while the call is active, and then ending the call. At any point in this flow, we can continue to run regular Appium commands, which is how we would check that our app continues to function normally even as the call takes place.

Also check: Batching Appium Commands Using Execute Driver Script to Speed Up Tests

That's it! Take a look at the full example below, which introduces some artificial pauses in the course of the script so you can see the action taking place (otherwise it all happens too quickly):Using this series of commands, we can simulate the entire flow of a user receiving a phone call while in our app, accepting the call, continuing to go about her business in the app while the call is active, and then ending the call. At any point in this flow, we can continue to run regular Appium commands, which is how we would check that our app continues to function normally even as the call takes place.

Continuously monitor and optimize quality of experience across applications, devices and 3rd-party interfaces with mobile app performance testing insights. Learn more.

That's it! Take a look at the full example below, which introduces some artificial pauses in the course of the script so you can see the action taking place (otherwise it all happens too quickly):


import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.GsmCallActions;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Edition042_Android_Phone {

    private AndroidDriver driver;
    private String APP = "https://github.com/cloudgrey-io/the-app/releases/download/v1.7.1/TheApp-v1.7.1.apk";
    private String PHONE_NUMBER = "5551237890";

    @Before
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("automationName", "UiAutomator2");
        capabilities.setCapability("app", APP);

        driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
    }

    @After
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    public void testPhoneCall() throws InterruptedException {
        // do something in our app
        driver.findElementByAccessibilityId("Login Screen").click();

        // receive and accept a call
        driver.makeGsmCall(PHONE_NUMBER, GsmCallActions.CALL);
        Thread.sleep(2000); // pause just for effect
        driver.makeGsmCall(PHONE_NUMBER, GsmCallActions.ACCEPT);

        // continue to do something in our app
        driver.findElementByAccessibilityId("username").sendKeys("hi");
        Thread.sleep(2000); // pause just for effect

        // end the call
        driver.makeGsmCall(PHONE_NUMBER, GsmCallActions.CANCEL);
        Thread.sleep(2000); // pause just for effect
    }
}

Of course, you can also have a look at the sample on GitHub.

Share this

Simulating Incoming Phone Calls On Android

4 Parts