Autoplay



Transcripted Summary

# Simulate sending SMS for Android

In this demo, we will try to simulate sending SMS using the Appium for Android.

This example is only for Android because the library or this function is still not supported in iOS.

For Appium SMS, we can search "appium send SMS" and here we can find the "Send SMS" documentation from Appium.

Here, we can find example usage under the "Commands" from Appium, and here for iOS, it's not supported for XCUITest or UIAutomation, but it's supported for Android UIAutomator2, Espresso, and UIAutomator.

It's not supported for any versions for iOS, so we will use this one for Android only.

From our application, we need to open the SMS messages application and then we can start simulating sending an SMS and check that the SMS is sent successfully.

From the "APK Info" app, we can click on this one to be able to get the appPackage and appActivity for this application to start using it.

In APK info, here I can search for messages, I can find this application then long press on it and select "Detailed Information".



Here we have our requested permissions.

We don't need these, but here we have all the activities.



For example, here we have a conversation list, activity, messages, UI, and conversation.

Maybe we can select this one, but the first appPackage starts with com.google.android.apps.messaging, so here we can just start writing this one into the code for appPackage.

Then .ui.ConversationListActivity is our appActivity.


package Android;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;

public class Send_SMS_Test {

    public AppiumDriver driver;

    @BeforeTest
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "9.0");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("appPackage",   "com.google.android.apps.messaging");
        capabilities.setCapability("appActivity",   ".ui.ConversationListActivity");

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

    @Test
    public void send_SMS() {
    }

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

So let's run our test and just make sure that we are opening the correct application and there is no problem in the appPackage or appActivity.

After that, we will continue by adding the SMS code with Appium.

Here we opened Messages, so the application is already open and our test case passed.



Let's now continue with the SMS code.

This is simple code - we can just add driver. - we should call sendSMS but we can't find sendSMS.

Why? Because here we are using the Appium driver.

So in this case, we need to convert AppiumDriver to be AndroidDriver.

Now, with AndroidDriver we can find that we have sendSMS, setClipboard, setConnection... a lot of things.



So we can use the AndroidDriver and the sendSMS method and with sendSMS, you should pass the phone number as a string and the string message.

Here, I should add the correct number - for example, "555-123-4567" as a string, and then the message - "Hello from TAU".

Also, by the way, as we mentioned, this is only for Android, it is not supported in iOS, and it's only for emulators.


package Android;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;

public class Send_SMS_Test {

    public AndroidDriver driver;

    @BeforeTest
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "9.0");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("appPackage",   "com.google.android.apps.messaging");
        capabilities.setCapability("appActivity",   ".ui.ConversationListActivity");

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

    @Test
    public void send_SMS() {
        driver.sendSMS("555-123-4567","Hello from TAU");
    }

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

Let's run our test now, and let's select "Optimize Imports" from the "Code" menu.

Now, we are running our test and we will check what happened during our test when we send the SMS.

It will be added in Messages, or we have a problem in the script.

So we opened Messages and here we have messages already displayed in the Messages list.

We can open Messages and this one says, "Hello from TAU", so we have the phone number and "Just now".

Maybe after that, we can work with this application.

For example, from the list, we can longPress on this first message and we can find the element to delete this message, and then we can also click on the "Delete" button to confirm this.

But in this script, it just needs to send us an SMS using Appium, but you can also complete the scenario to delete messages after that, or you can assert that the message is already displayed in the list.



Resources



Quiz

The quiz for this chapter can be found in Chapter 5.10

© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR