Starting your test
Initiate your test by adding the following:
// constructor to initialize driver
object
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
This is the default port number from which the Appium Server will be initialized.
Detecting Elements on your Android App
Elements can be detected by using the UI Automator application. This application is found in the 'tools' sub folder of your Android SDK folder. To detect the element, open UI Automator and take a screenshot of the page on which the script is to be run.
The right hand side of the UI Automator application shows all the elements of the page grouped by classes. Click on the desired element in the screenshot. The bottom right hand side window will populate with all the details of the selected element. For example, click on the text window under the text "Enter First Number". The element gets highlighted by a red rectangular colour. You will see the element details populate on the bottom right side table. Since we are using Selenium Web Driver libraries and classes, we can use the 'findelement' method to detect and select elements using various parameters such as 'Class', 'Name' or 'Id'.
To detect and select the element, first create a WebElement object and assign the relevant method to it:
For example:
//locating the First text box
WebElement FirstNo = driver.findElementById("EditText01");
Here, EditText01 is the element id if the first text box. You can click / activate the text box and enter a value in it by using the following commands:
//Clicking the First Text Box and
entering the value
FirstNo.click();
FirstNo.sendKeys("56");
This will activate the First text box and enter the numeric value '56' in it.
You can create a similar script for locating the second text box and entering a value it in:
//locating the second text box
WebElement SecondNo = driver.findElementById("EditText02");
//Clicking the Second text box and
entering the value
SecondNo.click();
SecondNo.sendKeys("65");
Similarly, create an object for the multiply button, select it and click on it by using the following code:
//Select and click the multiply
button
WebElement Multiply = driver.findElementByName("Multiply");
Multiply.click();
This should click the 'Multiply' button and display the result of the product of '56' and '65'. After your test is done, use the following command below to exit the script cleanly and end the test.
driver.quit();
Optiona: You can import TestNG assertions and add an assert to make sure that the result is as per specification.
Your complete Appium Script should look something like this:
package <Your Package name>;
import io.appium.java_client.AppiumDriver;
import java.io.File;
import
java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import
org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.remote.CapabilityType;
import
org.openqa.selenium.remote.DesiredCapabilities;
import org.junit.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import io.appium.java_client.AppiumDriver;
import
io.appium.java_client.android.AndroidDriver;
import org.testng.annotations.Test;
public class AppiumTest {
AndroidDriver
driver;
public static void main(String[] args) throws Exception {
// TODO Auto-generated
method stub
// creating a class
object
AppiumTest testcases = new AppiumTest();
// call launch app method
testcases.launchCalc();
//Start GlobalMenu
//testcases.launchGMenu();
// call add name method
testcases.addName();
}
public void launchCalc() throws
InterruptedException, MalformedURLException
{
File app=new File("D:\\Radio\\AndroidCalculator-V1_0.apk");
DesiredCapabilities capabilities = new
DesiredCapabilities();
//Name of mobile web browser to automate.
Should be an empty string if automating an app instead.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
//which mobile OS to use: Android, iOS or
FirefoxOS
capabilities.setCapability("platformName", "Android");
//Mobile OS version – in this case 4.4
since my device is running Android 4.4.2
capabilities.setCapability(CapabilityType.VERSION, "4.4.2");
//device name – since this is an actual
device name is found using ADB
capabilities.setCapability("deviceName", "92268ee10561a040");
//the absolute local path to the APK
capabilities.setCapability("app", app.getAbsolutePath());
//Java package of the tested Android app
capabilities.setCapability("appPackage", "com.calculator");
// activity name for the Android activity
you want to run from your package. This need to be preceded by a . (example:
.MainActivity)
capabilities.setCapability("appActivity", ".Main");
// constructor to initialize driver object
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(4000);
System.out.println("App
Launched");
//locating the First text box
WebElement FirstNo = driver.findElementById("EditText01");
//Clicking the First Text Box and entering
the value
FirstNo.click();
FirstNo.sendKeys("56");
Thread.sleep(2000);
//locating the second text box
WebElement SecondNo = driver.findElementById("EditText02");
//Clicking the Second text box and entering
the value
SecondNo.click();
SecondNo.sendKeys("65");
//Select and click the multiply button
WebElement Multiply = driver.findElementByName("Multiply");
Multiply.click();
Thread.sleep(10000);
driver.quit();
}
public void addName() throws InterruptedException
{
System.out.println("Test
success");
// write all your tests here
}
}
Since the actual test runs very fast, I have added a few thread.sleep methods to pause the test in between, so that you can get a better idea of how the test runs and to give the app some breathing space (Phew!)
Starting the Appium Server
This step is crucial to running your tests. Without this, your tests will not execute. Start the Appium.exe application on windows. Click on the 'Android' button to set the Android settings.
Click on the 'Choose' button, browse and select the APK to be tested. App package and Main Activity details will be displayed automatically. Enter device name as derived from the command 'adb devices' in CMD.
Click on the general settings icon and check the server number and port number from which the Appium server will be launched.
Now click on the 'Play' button on the top right corner of the Appium window. This will start the Appium server. The Appium server will look like below:
Now run the JAVA test you have in Eclipse and watch the result on your device.
Here we have reached the end of this blog. Hopefully, you have learned something useful from this blog and hope this helps you install and run Appium Scripts smoothly.
There is more to this. In my next blog, I will take up other commands and reporting via Test NG etc. Stay tuned !!
Thank you for taking the trouble to read this blog. Comment and criticism are always welcome.
Enjoy !!