Skip to main content

Java

Continuous Testing Cloud lets you to execute Appium tests developed in Java, on local and remotely located devices.

You can use the sample tests in our git repository by cloning them, or the sample tests below.

info

Replace <server> with the appropriate URL.

Android App Testing using Java Expand source

package apptest;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class AndroidAppTest {

AndroidDriver driver = null;
DesiredCapabilities dc = new DesiredCapabilities();
String testName = "Testing Android App with Java";
String accessKey = System.getenv("SEETEST_IO_ACCESS_KEY");

@Before
public void setUp() throws IOException {
dc.setCapability("testName", testName);
dc.setCapability("accessKey", accessKey);
//install the app on the device
dc.setCapability(MobileCapabilityType.APP, "http://d242m5chux1g9j.cloudfront.net/eribank.apk");
//get an Android device
dc.setCapability("platformName", "Android");
//launch the app
dc.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.experitest.ExperiBank");
dc.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".LoginActivity");
driver = new AndroidDriver(new URL("<server>"), dc);
}

@Test
public void testYourAndroidApp() {
driver.findElement(By.xpath("//*[@id='usernameTextField']")).sendKeys("company");
driver.findElement(By.xpath("//*[@id='passwordTextField']")).sendKeys("company");
driver.findElement(By.xpath("//*[@id='loginButton']")).click();
driver.findElement(By.xpath("//*[@id='makePaymentButton']")).click();
driver.findElement(By.xpath("//*[@id='phoneTextField']")).sendKeys("123456");
driver.findElement(By.xpath("//*[@id='nameTextField']")).sendKeys("Test");
driver.findElement(By.xpath("//*[@id='amountTextField']")).sendKeys("10");
driver.findElement(By.xpath("//*[@id='countryTextField']")).sendKeys("US");
driver.hideKeyboard();
driver.findElement(By.xpath("//*[@id='sendPaymentButton']")).click();
driver.findElement(By.xpath("//*[@id='button1']")).click();


}

@After
public void tearDown() {
if (driver != null)
{
driver.quit();
System.out.println("Report URL : " + driver.getCapabilities().getCapability("reportUrl"));

}
}

}

IOS App Testing for java Expand source

package apptest;

import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;

public class iOSAppTest {

IOSDriver driver = null;
DesiredCapabilities dc = new DesiredCapabilities();
String testName = "Testing iOS App with Java";
String accessKey = System.getenv("SEETEST_IO_ACCESS_KEY");

@Before
public void setUp() throws MalformedURLException {
dc.setCapability("testName", testName);
dc.setCapability("accessKey", accessKey);
//install the app on the device
dc.setCapability(MobileCapabilityType.APP, "http://d242m5chux1g9j.cloudfront.net/EriBank.ipa");
//get an iOS device
dc.setCapability("platformName", "iOS");
dc.setCapability("autoDismissAlerts", true);
//launch the app
dc.setCapability(IOSMobileCapabilityType.BUNDLE_ID, "com.experitest.ExperiBank");

driver = new IOSDriver(new URL("<server>"), dc);
}

@Test
public void testYouriOSApp() {

driver.findElement(By.xpath("//*[@text='Username']")).sendKeys("company");
driver.findElement(By.xpath("//*[@text='Password']")).sendKeys("company");
driver.findElement(By.xpath("//*[@text='loginButton']")).click();
driver.findElement(By.xpath("//*[@text='makePaymentButton']")).click();
driver.findElement(By.xpath("//*[@text='Phone']")).sendKeys("123456");
driver.findElement(By.xpath("//*[@text='Name']")).sendKeys("Test");
driver.findElement(By.xpath("//*[@text='Amount']")).sendKeys("10");
driver.findElement(By.xpath("//*[@text='Country']")).sendKeys("US");
driver.findElement(By.xpath("//*[@text='sendPaymentButton']")).click();
driver.findElement(By.xpath("//*[@text='Yes']")).click();

}

@After
public void tearDown() {
if (driver != null)
{
driver.quit();
System.out.println("Report URL : " + driver.getCapabilities().getCapability("reportUrl"));

}
}

}

Chrome on Android Test using Java Expand source

package webtest;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileBrowserType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class AndroidWebTest {
ChromeOptions chromeOptions;
AndroidDriver driver = null;
DesiredCapabilities dc = new DesiredCapabilities();
String testName = "Testing Website on Android Chrome with Java";
String accessKey = System.getenv("SEETEST_IO_ACCESS_KEY");

@Before
public void setUp() throws MalformedURLException {
dc.setCapability("testName", testName);
dc.setCapability("accessKey",accessKey);
dc.setBrowserName(MobileBrowserType.CHROME);
driver = new AndroidDriver(new URL("<server>"),dc);
}

@Test
public void testYourAndroidApp() throws InterruptedException {
driver.get("https://amazon.com");
System.out.println(driver.getTitle());
if( driver.getCapabilities().getCapability("device.category").equals("TABLET")){

driver.findElement(By.xpath("//*[@name='field-keywords']")).sendKeys("iPhone");
driver.findElement(By.xpath("//*[@text='Go']")).click();
}
else{
driver.findElement(By.xpath("//*[@name='k']")).sendKeys("iPhone");
driver.findElement(By.xpath("//*[@value='Go']")).click();
}
}

@After
public void tearDown() {
if (driver != null)
{
driver.quit();
System.out.println("Report URL : " + driver.getCapabilities().getCapability("reportUrl"));

}
}

}

Safari on IOS Test using Java Expand source

package apptest;

import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;

public class iOSAppTest {

IOSDriver driver = null;
DesiredCapabilities dc = new DesiredCapabilities();
String testName = "Testing iOS App with Java";
String accessKey = System.getenv("SEETEST_IO_ACCESS_KEY");

@Before
public void setUp() throws MalformedURLException {
dc.setCapability("testName", testName);
dc.setCapability("accessKey", accessKey);
//install the app on the device
dc.setCapability(MobileCapabilityType.APP, "http://d242m5chux1g9j.cloudfront.net/EriBank.ipa");
//get an iOS device
dc.setCapability("platformName", "iOS");
dc.setCapability("autoDismissAlerts", true);
//launch the app
dc.setCapability(IOSMobileCapabilityType.BUNDLE_ID, "com.experitest.ExperiBank");

driver = new IOSDriver(new URL("<server>"), dc);
}

@Test
public void testYouriOSApp() {

driver.findElement(By.xpath("//*[@text='Username']")).sendKeys("company");
driver.findElement(By.xpath("//*[@text='Password']")).sendKeys("company");
driver.findElement(By.xpath("//*[@text='loginButton']")).click();
driver.findElement(By.xpath("//*[@text='makePaymentButton']")).click();
driver.findElement(By.xpath("//*[@text='Phone']")).sendKeys("123456");
driver.findElement(By.xpath("//*[@text='Name']")).sendKeys("Test");
driver.findElement(By.xpath("//*[@text='Amount']")).sendKeys("10");
driver.findElement(By.xpath("//*[@text='Country']")).sendKeys("US");
driver.findElement(By.xpath("//*[@text='sendPaymentButton']")).click();
driver.findElement(By.xpath("//*[@text='Yes']")).click();

}

@After
public void tearDown() {
if (driver != null)
{
driver.quit();
System.out.println("Report URL : " + driver.getCapabilities().getCapability("reportUrl"));

}
}

}
info

To make use of the Appium Java Client, add a dependency to your Maven and Gradle projects. Make sure you replace <VERSION> with 7.6.0. It is recommended to use the public maven repository before the code export.

pom.xml Expand source

    <dependency>
<groupId>com.experitest</groupId>
<artifactId>appium</artifactId>
<version>VERSION</version>
</dependency>

build.gradle Expand source

    dependencies {
compile group: 'io.appium', name: 'java-client', version: 'VERSION'
}

Prerequisites

warning

Before you proceed with fetching the sample tests:

  1. Upload your app to your project.
  2. Fetch your access key.

Appium requires you to fetch and integrate Appium Dependencies, also known as Appium Java Client Libraries. The process requires you to download the .jar files and add them to your project. It takes time and there could be some setbacks on the way. We want it to be as easy as possible for you to start your testing so we created a git project for you that will help you set up your project with a few clicks. The project includes one iOS test class and one Android test class.

Included in the git project is a Gradle file that takes care of adding the Appium Java Client libraries and JUnit (Unit Testing).

To use the repository:

  1. Close the repository using either Intellij or Eclipse.
  2. Edit the test and run it.

You can also use the TestNG testing framework.