TestNG | Create a simple Maven project with Appium and TestNG

The following code sample uses the iOS and Android Appium drivers, within a Maven project and TestNG testing framework. It shows how to create iOS and Android drivers. The iOS and Android driver methods are implemented differently. In addition, each driver has unique methods and consts. 

Copy
IOSDriver & AndroidDriver
if(os.equals("iOS")){
    driver = new IOSDriver(new URL("https://" + this.host + "/nexperience/perfectomobile/wd/hub"), capabilities) ;
}
else{
    driver = new AndroidDriver(new URL("https://" + this.host + "/nexperience/perfectomobile/wd/hub"), capabilities);
}
Copy
HashMap<String , Object> script = new HashMap<String, Object>();
script.put("name", "IMDb");
driver.executeScript("mobile:application:open", script)
Copy

Opening an installed application

HashMap<String , Object> script = new HashMap<String, Object>();
script.put("name", "IMDb");
driver.executeScript("mobile:application:open", script)
Copy

Keyboard operations (Android code sample)

((AndroidDriver)driver).sendKeyEvent(AndroidKeyCode.ENTER);

It is required to cast Appium driver to AndroidDriver to enable Android's special methods.

Copy

Closing the application

HashMap<String , Object> script = new HashMap<String, Object>();
script.put("name", "IMDb");
driver.executeScript("mobile:application:close", script);

For the full code sample and more samples, visit our GitHub repository.

Tip: To use this code sample, make sure that the IMDb application is already installed on the device before running the test.