public static AppiumDriver commonInit() throws MalformedURLException {
AppiumDriver driver;
// 使用phonegap打包的hybrid app路径
String apppath ="F:\\XXXX.apk";
// setup AppniumDriver
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "XXXX");// 真机测试android设备
// capabilities.setCapability("udid", "XXXX");
capabilities.setCapability("platformVersion", "5.0.1");// 真机android版本为4.2.2
capabilities.setCapability("platformName", "Android");// 真机为android系统
capabilities.setCapability("app", apppath);// 被测app apk包的位置
capabilities.setCapability("appPackage","com.xxxxxxx");// 被测app的Activity类所在包
// capabilities.setCapability("appActivity", "xxxxxx");从I5版本后Acitivity变成了MainActivity
capabilities.setCapability("appActivity",".MainActivity");// 被测app的Activity类
capabilities.setCapability("automationName","selendroid");// 因为真机为4.2版本,所以使用selendroid
// capabilities.setCapability("noReset", true);
capabilities.setCapability("androidDeviceReadyTimeout", 15);
//
http://127.0.0.1:4723/wd/hub地址就是AppiumServer的地址
capabilities.setCapability("app-wait-activity", "activity-to-wait-for");
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
Set<String> contextNames = driver.getContextHandles();
for (final String contextName : contextNames)
System.out.println("App type:" + contextName);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.context("WEBVIEW_0");// 让appium切换到webview模式以便查找web元素
// Thread.sleep(8000);
WebElement select = driver.findElement(By.xpath("//*[@id='firstLaunchForm']/div[3]"));
select.click();// 点击language
driver.context("NATIVE_APP");
WebElement AlertText=driver.findElement(By.id("message")); ///alert id
String Alert=AlertText.getText();
}