工程目录如下:
封装初始化androiddriver 方法
import org.openqa.selenium.remote.DesiredCapabilities; import java.io.File; public class DeviceSession {
static
{
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
static DesiredCapabilities capabilities=null;
public static DesiredCapabilities getDesiredCapabilities()
{
//因为所有在java.io中的类都是将相对路径名解释为起始于用户的当前工作目录,
// 所以可以通过调用System.getProperty("user.dir") 来获得
File classpathRoot = new File(System.getProperty("user.dir"));//默认文件位置
File appDir = new File(classpathRoot, "apk");
File apk = new File(appDir, "android-debug.apk");
if(capabilities==null)
{
capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "UGUWUGTWYP49KVNJ");
capabilities.setCapability("platformVersion", "4.2");
capabilities.setCapability("app", apk.getAbsolutePath());//"E:\\apk\\android-debug.apk"(绝对路径,存放在电脑的某个位置)
capabilities.setCapability("appPackage", "com.eshare.Purse");
capabilities.setCapability("automationName", "Appium");
capabilities.setCapability("appActivity", ".MainActivity"); }
return capabilities;
}
}
测试用例如下:
import com.eshare.qa.common.*;
import com.eshare.qa.wireless.DeviceSession;
import io.appium.java_client.android.AndroidDriver;
import org.json.JSONObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import java.net.URL; public class TestPurse {
public AndroidDriver<WebElement> driver=null;
Assertion assertion;
@BeforeClass
public void setUp() throws Exception{
assertion = new Assertion();
DesiredCapabilities capabilities = DeviceSession.getDesiredCapabilities();
try {
driver = new AndroidDriver<WebElement>(
new URL(Configuration.Url), capabilities);
} catch (Exception ex) {
System.out.println(ex.toString());
}
//System.out.println("new!");
try {
Thread.sleep(15 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @Test
public void test() throws Exception{
boolean ret = ViewUtils.waitForWebViewInit(driver,"WEBVIEW_com.eshare.Purse");
assertion.assertTrue(ret);
driver.context("WEBVIEW");
String loginButtonXpath = "//*[text()='手机登入']";
boolean isExsit = isElementExist(loginButtonXpath);
if(isExsit){
login();
afterLogin();
}else{
afterLogin();
}
}
@AfterClass
public void teardown(){
driver.quit();
}