Appium android example program for windows using java - native app
To perform the automation using appium for the native android app just .apk file is enough. No need of developers code at all.I used following environments to accomplish this example program,
- Windows7
- Android SDK
- JDK
- Eclipse
- TestNG
Install and start the appium server:
- Download the latest AppiumForWindows.zip from here
- Unzip the AppiumForWindows.zip
- Click on the appium.exe present in the unzipped appium folder. It will be opened up as below,
Appium Server For Windows |
- Now click on the 'Launch' button, It will start the server at 127.0.0.1:4723 as below,
Appium server is started |
- Then run the below program once the android emulator is ready.
Appium example program for android
- Check the option 'Perform Full Reset'
- Go to File > Preferences and check the option 'Reset Application State After Each Session'
- Here I used the uiautomatorviewer to find the element hierarchy and then I constructed the xpath manually. To learn about constructing the xpath, please gohere
- You should set ANDROID_HOME and path for Android SDK in environment variable
- Emulator adb device should have API level greater than or equal to 17
修改后的代码如下:
package com.qa.test;
import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class androidExample {
WebDriver driver = null;
@BeforeMethod
public void setup() throws Exception {
File appDir = new File("E://AndroidApps");
File app = new File(appDir, "myApp.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.2.2");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
// Here we mention the app's package name, to find the package name we have to convert .apk file into java class files
capabilities.setCapability("app-package","com.myApp.activity");
//Here we mention the activity name, which is invoked initially as app's first page.
capabilities.setCapability("app-activity","LoginActivity");
//capabilities.setCapability("app-wait-activity","LoginActivity,NewAccountActivity");
capabilities.setCapability("app", app.getAbsolutePath());
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
}
@Test
public void loginTest() throws Exception {
driver.findElement(By.xpath("//EditText[@text='Email Address']")).sendKeys("tester@gmail.com");
driver.findElement(By.xpath("//LinearLayout/EditText[2]")).sendKeys("Testerpwd");
driver.findElement(By.xpath("//CheckBox")).click();
driver.findElement(By.xpath("//Button[@text='Login']")).click();
WebDriverWait wait = new WebDriverWait(driver,80);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//Button[@text='Logout']")));
driver.findElement(By.xpath("//Button[@text='Logout']")).click();
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}