Skip to content

Instantly share code, notes, and snippets.

@gwpantazes
Last active October 17, 2016 00:10
Show Gist options
  • Save gwpantazes/8e0f5dd2b43a1704f4a4e8d8933aed88 to your computer and use it in GitHub Desktop.
Save gwpantazes/8e0f5dd2b43a1704f4a4e8d8933aed88 to your computer and use it in GitHub Desktop.
Simple Appium Program in java with Setup, queries, and teardown.
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Altered frome the Calculator app example on http://www.guru99.com/introduction-to-appium.html
*
* Pretty darn good tutorial from 2015 on Appium, very specific: http://toolsqa.com/mobile-automation/appium/appium-a-cross-platform-mobile-automation-tool/
*/
public class SimpleAppium {
static AndroidDriver driver;
public static void main() throws Exception {
setUp();
// These are sample statements. Do your appium work here!
//locate the Text on the calculator by using By.name()
WebElement two=driver.findElement(By.name("2"));
two.click();
WebElement plus=driver.findElement(By.name("+"));
plus.click();
WebElement four=driver.findElement(By.name("4"));
four.click();
WebElement equalTo=driver.findElement(By.name("="));
equalTo.click();
//locate the edit box of the calculator by using By.tagName()
WebElement results=driver.findElement(By.tagName("EditText"));
//Check the calculated value on the edit box
assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";
teardown();
}
/**
* See the Appium capabilities documentation: http://appium.io/slate/en/master/?ruby#appium-server-capabilities
* and more Appium docs for capabilities: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md
*/
public static void setUp() throws MalformedURLException {
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
/* // This is the Guru99 original code for capabilities which we won't use.
// see: http://www.guru99.com/introduction-to-appium.html
capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("VERSION", "4.4.2");
capabilities.setCapability("deviceName","Emulator");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage", "com.android.calculator2");
// This package name of your app (you can get it from apk info app)
capabilities.setCapability("appActivity","com.android.calculator2.Calculator"); // This is Launcher activity of your app (you can get it from apk info app)
//Create RemoteWebDriver instance and connect to the Appium server
//It will launch the Calculator App in Android Device using the configurations specified in Desired Capabilities
*/
// These capabilities are all we actually need.
capabilities.setCapability("deviceName","Emulator");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "4.4.2");
// Now pick between using package and launch activity together, or using the apk path on your host machine
// The package and launch activity method (should already be installed):
capabilities.setCapability("appPackage", "com.your.app.package.name"); // This package name of your app (you can get it from apk info app)
// This is launchable activity of your app (you can get it from apk info app
// or from aapt in build-tools: http://stackoverflow.com/questions/12698814/get-launchable-activity-name-of-package-from-adb )
capabilities.setCapability("appActivity","com.your.app.package.name.themainlauncheractivity");
// The app path method (will install for you):
// capabilities.setCapability("app","C:/some/location/on/host/your.apk");
// Create AndroidDriver instance and connect to the Appium server
// It will launch the App in Android Device using the configurations specified in Desired Capabilities
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
// If you are automating the browser and not an apk, then do the Guru99 way
//driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
public static void teardown(){
//close the app
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment