After set-up now you can write your first Test case:
1.First create the folder->click main in app folder->Right click on the package ->Click on 'create Java Class.
2.Set Desired Capabilities:
Desired capabilities are options that can be used to customize and configure the browser session.
This tells Appium server what kind of automation we're interested in.
automationName :which automation engine to use: Appium(default) or Selendroid
platformName : which mobile OS : iOS ,Android,FirefoxOS
platformVersion : Mobile OS version : 5.1,7.1,4.0
deviceName : Kind of mobile device or emulator: Galaxy S4,iPad Simulator
app :The absolute local path or remote URL to .ipa or .apk file:/abs/path/to/my.apkor http://myapp.com/app.ipa(Note that this capability is not required for Android if you specify appPackage and appActivity capabilities )
browserName : Name of mobile web browser to automate. Should be an empty string if automating an app instead.‘Safari’ for iOS and ‘Chrome’, ‘Chromium’, or ‘Browser’ for Android
newCommandTimeout :How long (in seconds) Appium will wait for a new command from the client before assuming the client quit and ending the session:
autoLaunch :Whether to have Appium install and launch the app automatically. Default true
There is no need to mention all the desired capabilities
Now you need to mention only:platform name
device name
app
DesiredCapabilities cap=new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME,MobilePlatform.ANDROID);
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "100");
cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
3.Apk file
For your first program lets copy the apk file and paste in the source folder of your project.
4.Next need to set the path of the apk file.
File appDir = new File("src");
File app = new File(appDir, "app-debug.apk");
5.Pass this absolute path to desired capabilities
cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
6.Invoke driver:
For Android : AndroidDriver
For iOS :iOS Driver
You need to create an object for Android driver class and pass two parameter.
2.Pass desired capabilities to server (so pass ‘cap’ as second argument)
AndroidDriver driver;
driver=new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),cap );
7.Test Case here is:
Here we will install the app and check if there is a text HelloWorld and click on menu which shows settings.
Now open uiautomatorviewer (navigate to tools folder and type uiautomatorviewer)and find the elements unique identifier.
In Appium you have to do trial and error to locate the elements.
a)Here HelloWorld can be found using Xpath .
//To find the HelloWorld
WebElement text1 = driver.findElement(By.xpath("//android.widget.TextView[@index='0']"));
//Assert the text displayed is same
Assert.assertEquals("HelloWorld", text1.getText());
b)Click on Menu Object identifier(here Menu is found using content-description).
driver.findElementByName("More options").click();
c)Settings (On Menu click Settings should be displayed).This is checked using Assert statements.
Assert.assertEquals("Settings",driver.findElementById("title").getText());
8)Use JUnit or TestNG framework.
Here we are using JUnit Test framework
JUnit test framework which uses annotations to identify methods that specify a test.
Some of the most used annotations:
@Test : The @Test annotation identifies a method as a test method.
@Test(timeout=100) : Fails if the method takes longer than 100 milliseconds.
@Before : This method is executed before each test.
@After : This method is executed after each test.
@BeforeClass : This method is executed once, before the start of all tests.Method
annotated with this should be defined as static.
@AfterClass : This method is executed once, after all tests have been finished.Method
annotated with this should be defined as static.
@Ignore : Ignores the test method.
9)Here is the snippet of the code
Before executing the testcase(pass the capabilities to the appium server hosted on local server on port 4723).You can change the port to any free port.
@Before
public void testCaseSetUp() throws MalformedURLException {
File appDir = new File("src");
File app = new File(appDir, "app-debug.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "100");
cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
cap.setCapability("fullReset", true);
}
After testcase is executed(quit the driver)
@After
public void testCaseTearDown()
{
if (driver != null)
driver.quit();
}
Testcase:1.Check Hello world is displayed
2.Click on Menu
3.Check ‘Settings’ is displayed
@Test
public void testHelloWorld() throws Exception {
//find text HelloWorld and assert
WebElement text1 = driver.findElement(By.xpath("//android.widget.TextView[@index='0']"));
Assert.assertEquals("HelloWorld", text1.getText());
//Click Menu and asset settings
driver.findElementByName("More options").click();
Assert.assertEquals("Settings", driver.findElementById("title").getText());
}
10.Complete code
package com.test.helloworld;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;
public class AppiumTest {
AndroidDriver driver;
@Before
public void testCaseSetUp() throws MalformedURLException {
File appDir = new File("src");
File app = new File(appDir, "app-debug.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "100");
cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
cap.setCapability("fullReset", true);
}
@Test
public void testHelloWorld() throws Exception {
//find text HelloWorld and assert
WebElement text1 = driver.findElement(By.xpath("//android.widget.TextView[@index='0']"));
Assert.assertEquals("HelloWorld", text1.getText());
//Click Menu and asset settings
driver.findElementByName("More options").click();
Assert.assertEquals("Settings", driver.findElementById("title").getText());
}
@After
public void testCaseTearDown()
{
if (driver != null)
driver.quit();
}
}
11.How to run the TestCase:
a)Set the build variant to unit test
b)Type appium on terminal and start the server
c)Right click on TestClass and Click on Run
Build variant Set-up
Appium server start
Appium logs after test executed
Android Studio Terminal after testcases executed
Done!!!!!!!