It is really good to know that we can automate mobile apps on real devices. Appium provides the support for automating apps on real devices. We can test the apps in a physical device and experience the look and feel that an end user would.
In this chapter, we will learn the following topics:
Before starting with Appium, make sure that you have all the necessary software installed. Let's take a look at the prerequisites for Android and iOS:
Prerequisites for Android |
Prerequisites for iOS |
---|---|
Java (Version 7 or later) |
Mac OS (Version 10.7 or later) |
A real Android device |
An iOS provisional profile |
Chrome browser on a real device |
An iOS real device |
Eclipse |
The SafariLauncher app |
TestNG |
ios-webkit-debug-proxy |
The Appium Server |
Java Version 7 |
Eclipse | |
Selenium Server and WebDriver Java library |
TestNG |
The Apk Info app | |
The Appium client library (Java) | |
Selenium Server and WebDriver Java library |
While working with the Android real device, we need to enable USB debugging under Developer options. To enable USB debugging, we have to perform the following steps:
To ensure that we are ready to automate apps on the device, perform the following steps:
adb devices
and press the Enter button.You will get a list of Android devices; if not, then try to kill and start the adb server with the adb kill-server
and adb start-server
commands.
Now, we've come to the coding part. First, we need to set the desired capabilities and initiate an Android/iOS driver to work with Appium on a real device. Let's discuss them one by one.
We have two ways to set the desired capabilities, one with the Appium GUI and the other by initiating the desired capabilities object.
Using the desired capabilities object is preferable; otherwise, we have to change the desired capabilities in the GUI repeatedly whenever we are testing another mobile app.
Let's take a look at the Appium GUI settings for native and hybrid apps:
Perform the following steps to set the Android Settings:
If the application is already installed on the real device, then we don't need to follow steps 2-4. In this case, we have to install the Apk Info app on the device to know the package and activities of the app and set them using the desired capabilities object, which we will see in the next section. You can easily get the 'Apk info' app from the Android Play Store.
Moto X
).Perform the following steps to set the Android Settings for web apps:
Let's discuss how to initiate the desired capabilities object and set the capabilities.
We have already discussed desired capabilities in Chapter 1, Appium – Important Conceptual Background, so here we will directly dive into the code with comments. First, we need to import the following packages:
import java.io.File; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.remote.MobileCapabilityType;
Now, let's set the desired capabilities for the native and hybrid apps:
DesiredCapabilities caps = new DesiredCapabilities();//To create an object File app=new File("path of the apk");//To create file object to specify the app path caps.setCapability(MobileCapabilityType.APP,app);//If app is already installed on the device then no need to set this capability. caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");//To set the Android version caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");//To set the OS name caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Moto X");//You can change the device name as yours. caps.setCapability(MobileCapabilityType.APP_PACKAGE, "package name of your app (you can get it from apk info app)"); //To specify the android app package caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "Launch activity of your app (you can get it from apk info app)");//To specify the activity which we want to launch
In Android mobile web apps, some of the capabilities that we used in native and hybrid apps such as APP
, APP PACKAGE
, and APP ACTIVITY
are not needed because we launch a browser here. First, we need to import the following packages:
import java.io.File; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.remote.MobileCapabilityType;
Now, let's set the desired capabilities for the web apps:
DesiredCapabilities caps = new DesiredCapabilities();//To create an object caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");//To set the android version caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");//To set the OS name caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Moto X");//You can change the device name as yours caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome"); //To launch the Chrome browser
We are done with the desired capability part; now, we have to initiate the Android driver to connect with the Appium Server by importing the following packages:
import io.appium.java_client.android.AndroidDriver; import java.net.URL;
Then, initiate the Android driver as shown here:
AndroidDriver driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);//TO pass the url where Appium server is running
This will launch the app in the Android real device using the configurations specified in the desired capabilities.
Before moving on to the desired capabilities for iOS, we have to make sure that we have set up a provisional profile and installed the SafariLauncher app and ios-webkit-debug-proxy to work with the real device.
First, let's discuss the provisional profile.
This profile is used to deploy an app on a real iOS device. To do this, we need to join the iOS Developer Program (https://developer.apple.com/programs/ios/).
For this, you will have to pay USD 99. Now, visit https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/MaintainingProfiles/MaintainingProfiles.html#//apple_ref/doc/uid/TP40012582-CH30-SW24 to generate the profile.
After this, you will also need to install provisional profile on your real device; to do this, perform the following steps:
The SafariLauncher app is used to launch the Safari browser on a real device for web app testing. You will need to build and deploy the SafariLauncher app on a real iOS device to work with the Safari browser. To do this, you need to perform the following steps:
Appium.dmg
; to do this, perform the following steps:Appium.dmg
.Contents/Resources/node_modules/appium/build/SafariLauncher
.SafariLauncher.zip
.submodules/SafariLauncher/build/Release-iphoneos
and replace the SafariLauncher app with your app.submodules
and rename it as SafariLauncher.zip
.Now, we need to install the ios-webkit-debug-proxy on Mac to establish a connection in order to access the web view. To install the proxy, you can use brew and run the command brew install ios-webkit-debug-proxy
in the terminal (this will install the latest tagged version), or you can clone it from Git and install it using the following steps:
git clone https://github.com/google/ios-webkit-debug-proxy.git
, and then click on the Enter button.cd ios-webkit-debug-proxy ./autogen.sh ./configure make sudo make install
We are now all set to test web and hybrid apps. Next, it's time to move on to the desired capabilities for iOS.
Similar to Android, we can set the desired capabilities using the Appium GUI and by initiating the desired capabilities object. Let's discuss the Appium GUI settings for native and hybrid apps. The following screen will be displayed when the iOS Settings icon is clicked:
Perform the following steps to set the iOS settings for native and hybrid apps:
Let's set the Appium GUI settings for web apps:
Perform the following steps to set the iOS settings for web apps:
Now, let's take a look at how to initiate the desired capabilities object and set the capabilities.
We have already discussed the desired capabilities for iOS in Chapter 1,Appium – Important Conceptual Background, so here we will directly dive into the code with comments. First, we need to import the following packages:
import java.io.File; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.remote.MobileCapabilityType;
Now, let's set the desired capabilities for the native and hybrid apps:
DesiredCapabilities caps = new DesiredCapabilities();//To create an object of desired capabilities File app=new File("path of the .app");//To create a file object to specify the app path caps.setCapability(MobileCapabilityType.APP,app); //To set the app path caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1");//To set the iOS version caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");//To set the OS name caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPad");// Type device name caps.setCapability("udid","Real Device Id ");// To specify the UDID of the device
In iOS mobile web apps, some of the capabilities that we used in native and hybrid apps such as APP
, APP PACKAGE
, and APP ACTIVITY
are not needed because we launch a browser here. First, we need to import the following packages:
import java.io.File; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.remote.MobileCapabilityType;
Now, let's set the desired capabilities for the web apps:
DesiredCapabilities caps = new DesiredCapabilities();//To create an object of desired capabilities caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1");//To set the iOS version caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");//To set the OS name caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad");//To type the device name caps.setCapability("udid","Real Device Id ");//To specify the UDID of real device caps.setCapability(MobileCapabilityType.BROWSER_NAME,"Safari"); //To launch the Safari browser
We are done with the desired capabilities part; now, we have to initiate the iOS driver to connect to the Appium Server by importing the following packages:
import io.appium.java_client.ios.IOSDriver; import java.net.URL;
Then, initiate the iOS Driver as shown here:
IOSDriver driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"),caps); //To pass the url where Appium server is running
This will launch the app in the simulator using the configurations specified in the desired capabilities. Now, you can use the following class for scripts with TestNG:
import io.appium.java_client.ios.IOSDriver; import io.appium.java_client.remote.MobileCapabilityType; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class TestAppIication { IOSDriver driver; @BeforeClass //It will execute one time before execute all test cases public void setUp() throws MalformedURLException{ //Set up desired capabilities DesiredCapabilities caps = new DesiredCapabilities(); File app=new File("path of the .app"); caps.setCapability(MobileCapabilityType.APP,app); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS"); caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad"); caps.setCapability("udid","Real Device Id "); caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");// In case of web-apps driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testExample(){ //We will put test scripts here } @AfterClass public void tearDown(){ driver.closeApp(); //driver.quit(); //in case of web apps } }
18.218.153.50