Select Page
Mobile App Testing

Appium Tutorial to Automate Inbuilt Mobile Features

Are you looking for an Appium Tutorial to automate the inbuilt features of the device you are testing? Look no further and continue reading.

Appium Tutorial to Automate Inbuilt Mobile Features

Appium is an open-source test automation tool that is used for automating mobile web, native, and hybrid applications across platforms like Android and iOS devices by executing our test scripts using the Appium server. If you are looking to completely automate your mobile app testing process by using Appium, then it is mandatory for you to know how to automate the in-build mobile features of the device. So in this Appium tutorial, we will be focusing on how to automate some of the most useful in-built features using Appium. As a leading QA company, we also provide mobile testing as a service and these features have come in handy for us when we’ve needed to toggle between screen orientations, retrieve OTP’s, and so on in our various projects. Let’s start with the basics and find out how to start a new session before we head over to the main aspects.

Appium Tutorial for initiating server

1. Creating a New Session

You can start a new session using the serer by defining the desired capabilities while passing the arguments. In the below code, we have defined the capabilities as per our need.

Passing the capabilities(Mobile Configuration)

DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PLATFORM, "Android");
cap.setCapability(CapabilityType.VERSION, "11");
cap.setCapability("deviceName", "Samsung-Galaxy");
cap.setCapability("automationName", "UiAutomator2");
cap.setCapability("appPackage", "com.android.settings");

//New session will be created...
AndroidDriver<MobileElement> driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), cap);

2. Installing the App:

Once you have created the new session, installing the application that you want to test is pretty much a no-brainer. Let’s take a look at the 2 ways you can install the New APK file,

(i.) Install using “DesiredCapabilities” before launching the Android driver instance.

File app = new File(new File(System.getProperty("user.dir")), "Calculator.apk"); //get APK file directory path.
capabilities.setCapability(MobileCapabilityType.APP, appDir.getAbsolutePath());

(ii.) Install via the ‘installApp’ method.

You can install the new app using the “installApp” command, and it is important to note that you would need to pass the APK file path.

Code:

driver.installApp("E:\\Projects\\APK-Files\\Calculator.apk");

Example: The calculator application will be installed on my real device through the Appium server.

3. Status of the Appium Server:

You can retrieve the Appium server status through the driver instance by making use of the below code.

Code:

driver.getStatus()

o/p: {build={version=1.20.2}}

4. Appium Tutorial to Get & Set Orientation:

If you are testing any application, one of the basic things you would want to make sure of is if it works well on both portrait and landscape orientations. So you can control the screen orientation of the smartphone using Appium and set it as per your needs. You can get/set the mobile orientation using the below-mentioned command,

i. Get Orientation:

– You can get the mobile orientation information using this command “getOrientation()” as illustrated in the below code

Code:

driver.getOrientation(); //return type String.

– o/p: LANDSCAPE|PORTRAIT

ii. Set Orientation:

– You can set the device orientation to Landscape or Portrait by using the following codes,

Code:


driver.rotate(ScreenOrientation.LANDSCAPE);
driver.rotate(ScreenOrientation.PORTRAIT);

5. Geo-Location Feature Usage:

Nowadays, so many applications depend on using the Geo-location feature of the smartphone for various reasons and functions. Let’s say you are testing an application that is used to make online payments, then there might come a scenario where you might have to turn on the GPS of the smartphone during sign-in to monitor any suspicious activity. So you can get/set the Geo-location using the location() command in Appium. It will return the Geo-location in the form of (Latitude, Longitude), and the code you need is listed below.

Location location = driver.location(); // Get current device location.
System.out.println("Locations : "+location);

6. Appium Tutorial to Retrieve Device Settings:

You can get the Device settings using the driver instance, which will return the current device settings.

Code:


Map<String, Object> settings = driver.getSettings();

If you want to add one more capability, we can add it via the setSetting() method. (Update the current setting in the mobile device) At times the page you want may not load though you have a fast internet connection, and you wouldn’t want your automation testing to be stuck there for no good reason. So by using this option, you will be able to assign a timeout.

Code:


driver.setSetting(Setting.WAIT_FOR_IDLE_TIMEOUT, 5000);

By making use of the above code, we have set a timeout of 500 milliseconds (5 seconds). After which, if the scenario is idle without proceeding any further, then it will be considered a failure, and the next scenario will begin testing.

7. Appium Tutorial to Start an Activity:

One of the most common scenarios where you might need to rely on another application like messages is to retrieve an OTP or send a verification message for safety purposes. You can easily start an Android activity by providing the package name and the activity name.

To initialize the New Android app using Activity and package name. If you want to launch the new app for the same execution, we can call the “startActivity” method.

For example, during the same execution, receive the OTP in the Messages app; You can call the “startActivity” method.

driver.startActivity(new Activity("<<PACKAGE-NAME>>", "<<ACTIVITY-NAME>>"));

Example:

driver.startActivity(new Activity("com.samsung.android.messaging", "com.android.mms.ui.ConversationComposer"));

8. Appium Tutorial for Recording the Screen:

You might be testing an app that is specifically designed to work well with screen recorders, or if we look at a more common scenario, you might have to record the entire test execution from step A to step Z as it is easier to explain what the bug is. Or you might want to use this feature to maintain a log of all the tests that you have done. In these scenarios, you can make use of the below-mentioned code snippet to get the job done. We can record the mobile script execution using the “startRecordingScreen” method, which is provided by appium.java_client package.

driver.startRecordingScreen(
new AndroidStartScreenRecordingOptions().withTimeLimit(Duration.ofSeconds(60)));
// Do the actions
driver.activateApp("com.android.settings");
driver.openNotifications();
driver.toggleWifi();
// Stop the Recording screen function.
String result = driver.stopRecordingScreen();
byte[] decodedVideo001 = Base64.getMimeDecoder().decode(result);
Path testVideoFile11 = Paths.get("E:\\Projects\\Codoidian\\Android-InBuiltMobOptions", String.format("%s-%d.%s", "Test-Video-", System.currentTimeMillis(), "mp4"));
Files.write(testVideoFile11, decodedVideo001);

The feature has more customization options to offer as we can assign time limits or size limits for the screen record. You can use the above codes for this purpose.

i. withTimeLimit(), ii. withVideoSize(), etc…

Once you stop the screen recording, you will have the option to set/write the path for the video file and define where it has to be saved.

9. Device Interactions Features:

Device Interactions are some of the basic features that every end-user would use, and a feature we would not miss covering in our Appium tutorial. Features like locking the device or unlocking the device are a couple of examples. If you are looking to test a video player application, then you might want to see what happens to the app when the device gets locked as a video is playing. Either there should be an option for background play, which will result in the audio continuing to play, or the app must stop playing once the device is locked. Now let’s find out how to accomplish this. We have listed the feature and the code that is required.

i. Lock the device

driver.lockDevice();

ii. Unlock the device

driver.unlockDevice();

iii. Get DeviceLocked status

driver.isDeviceLocked(); // return boolean function{TRUE|FALSE}

10. Controlling Keyboard Actions:

Another basic inbuilt feature every application uses is the keyboard. We ourselves would have seen the same bug in many applications where the keyboard does not hide once the OTP or pin or any data is entered. To make things worse, the button to click to go to the next stage will be hidden by the keyboard during such scenarios. So how can you open the keyboard, hide it and even check the current status of the keyboard? Easy, you can simply follow the below commands.

i. To open the Keyboard.

driver.getKeyboard();

ii. To hide the Keyboard.

driver.hideKeyboard();

iii. You can use the below code to check whether the Device keyboard is Opened or not.

driver.isKeyboardShown(); // return boolean function{TRUE|FALSE}

11. Appium Tutorial to Get Performance Data:

Obtaining the performance data is a very important feature that we will be seeing now in this Appium Tutorial as nobody wants to use an app that is overloading their device. You can verify if the app you are testing is optimal or not by obtaining the logs of the Mobile application performance data types such as CPU, Memory, Network traffic, and Battery. We have listed the codes you will need below.

Code:

List<String> performanceTypes = driver.getSupportedPerformanceDataTypes();

O/P: [cpuinfo, memoryinfo, batteryinfo, networkinfo]

We can also retrieve the performance log from the Appium driver instance using this method, and we need to pass the parameters like AppPackageName, PerformanceDataTypes, and Data Read Timeout.

Get performance data - Appium Tutorial

Code:

List<List<Object>> LstPerfDataBatteryInfo = driver.getPerformanceData("com.COMPANY.android", "batteryinfo", 5);
List<List<Object>> LstPerfDataNetworkInfo = driver.getPerformanceData("com.COMPANY.android", "networkinfo", 5);

12. Terminate App & Remove App Features:

Once you have completed testing the app with a few test cases you would have to start over with a new combination after closing the app. Or you might have completely finished testing and would want to uninstall the application from the device itself. You can easily do both by making use of the below codes.

i. Terminate App:

– In this Method(terminateApp), We can use it to close the App from a real device using App Packages or Activity Names.

Example:

driver.terminateApp('com.android.settings');

ii. Remove App:

– In this function(removeApp), To uninstall the Application from the real device. (It will be removed/deleted entirely from the real device which is passing the Package Name)

Example:

driver.removeApp("com.example.AppName");

13. Is App Installed Feature

You might use this feature at the beginning before you start testing or use it at the end once you have uninstalled the application. It is used to check whether the application is installed or not in the real device, and we can use the ‘isAppInstalled” command to do it. It’ll return a Boolean function that is either true or false.

Example:

driver.isAppInstalled("com.samsung.android.messaging")

Conclusion:

We hope that this blog has been an informative and enjoyable read for you. These are the basic in-built features that we thought you might use in most of your testing. If you would like to know how to automate any other in-built feature using Appium apart from what we have discussed in this Appium Tutorial, just head over to our comments section and post your question. As one of the companies that provide the best mobile device testing services, we will be able to help you out with any doubts. Also, make sure to subscribe to our blog as we will be posting more of such informative content that will help you stay on top of your game.

Comments(0)

Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility