Create an Appium Project by Integrating Appium-Eclipse-Maven-TestNG

This blog will explain the steps to follow for creating a simple Appium project.

Prerequisite: following software’s should be installed:

  • Appium GUI App
  • Java
  • Eclipse
  • Maven
  • eclipse-maven-plugin
  • eclipse-TestNG-plugin
  • Genymotion Android Emulator

Step 1) Lets create a new project in eclipse by following steps:

Click on new –> Other –> Maven –> Maven Project — > Next

1.png

Step 2) Now click on Simple project and keep the default workspace location.

2.png

Step 3) Provide details as Artifact id, Group id, name and description. and click on Finish.Screen Shot 2016-02-24 at 5.32.22 pm

Step 4) This should create a basic maven project. Now we need to add Appium and TestNG Dependencies in pom.xml.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>AppiumRecipeBook</groupId>
  <artifactId>AppiumBookBlog</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>AppiumBookBlog</name>
  <description>AppiumBookBlog</description>

   <properties>
		<appium.version>3.3.0</appium.version>
		<testng.version>6.9.10</testng.version>
	</properties>

	<dependencies>

		<!-- Appium -->
		<dependency>
			<groupId>io.appium</groupId>
			<artifactId>java-client</artifactId>
			<version>${appium.version}</version>
		</dependency>

		<!-- testng -->
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>${testng.version}</version>
			<scope>test</scope>
		</dependency>

	</dependencies>

</project>

Step 6) We will create a simple project structure, to keep files at logical places. We need to create few packages, and please see below screenshot for better understanding.

Screen Shot 2016-02-25 at 4.14.00 pm.png
Step 7) We will add the andrroid apk that we will use for the demo, to apps folder. ApiDemos-debug.apk is available in assets folder in Appium source code.

Step 8) Now we will create a new class in appium package, AppiumDriverBase. Add following code to this class:

package appium;

import io.appium.java_client.android.AndroidDriver;

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

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class AppiumDriverBase {

    protected AndroidDriver driver;
    protected WebDriverWait wait;

    //before Test Annotation makes a java function to run every time before a TestNG test case
    @BeforeTest
    protected void createAppiumDriver() throws MalformedURLException, InterruptedException {

	//relative path to apk file
	final File classpathRoot = new File(System.getProperty("user.dir"));
	final File appDir = new File(classpathRoot, "src/test/resources/apps/");
	final File app = new File(appDir, "ApiDemos-debug.apk");

	//setting up desired capability
	DesiredCapabilities caps = new DesiredCapabilities();
	caps.setCapability("browserName", "");
	caps.setCapability("platform", "ANDROID");
	caps.setCapability("platformVersion", "5.0");
	caps.setCapability("deviceName", "ANDROID");
	caps.setCapability("app", app.getAbsolutePath());

	//initializing driver object
	driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);

	//initializing explicit wait object
	wait = new WebDriverWait(driver, 10);
    }

    //After Test Annotation makes a java function to run every time after a TestNG test case
    @AfterTest
    public void afterTest(){

	//quit the driver
	driver.quit();
    }

}
 

 

Step 9) now we need to add a test case. Create one more class in Appium package and add class ‘SampleTestCase’. Add following code to this class:

package appium;

import io.appium.java_client.MobileBy;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.Test;

public class SampleTestCase extends AppiumDriverBase{

    //Test Annotation changes any java function to TestNG test case
    @Test
    public void sampeTest(){

	//click on Accessibility link
	wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("Accessibility")));
	driver.findElement(MobileBy.AccessibilityId("Accessibility")).click();

	//click on 'Accessibility Node Querying' link
	wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("Accessibility Node Querying")));
	driver.findElement(MobileBy.AccessibilityId("Accessibility Node Querying")).click();

	//back
	driver.navigate().back();

	//back
	driver.navigate().back();
    }

}

PS: for simplicity purpose i have kept first test case as very simple. you can modify it as per your convenience.

Step 10) Now we need to run the sample test case. For that we need to get Appium Server and Android Emulator app and running.

– Appium Server running via GUI on mac looks like this:
Screen Shot 2016-02-25 at 5.20.00 pm

Step 11) Once Appium Server and Android emulator are up and running, we need to execute the test case using TestNG plugin.

Screen Shot 2016-02-25 at 5.32.20 pm

This steps will help you start with Appium project in Java.

Advertisements

2 thoughts on “Create an Appium Project by Integrating Appium-Eclipse-Maven-TestNG

  1. Hi,

    Thanks for the informative post. I had one question here. I am able to successfully run the script in windows, but everytime i have to run appium server and emulator before running the script. Is it possible to integrate appium server and emulator in the maven or in Jenkins so that the manual intervention can be avoided?.

    Thanks and Regards,
    Lalith Kumar Sharma

    Like

    • Hi Lalit

      You can start Appium Server and Emulator once before starting the first test, after that, all test cases will use the same server and same Emulator.

      Also, if you are preparing the test cases, then keep the Appium server and Emulator running in the background, No need to start it again and again.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s