1.安装appium,并且启动服务
2.安装sdk,并配置环境变量
ANDROID_HOME C:\Users\admin\AppData\Local\Android\android-sdk
path ANDROID_HOME%\build-tools;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;
3.
打开AVD manager,创建Android Virtual Device,device name跟version要跟代码中的匹配,并且Start。
4.
package PhoneTest;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
public class OpenAndroid {
private AppiumDriver driver;
@Before
public void setUp() throws Exception {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.BROWSER_NAME, "");
cap.setCapability("platformName", "Android"); //指定测试平台
cap.setCapability("deviceName", "ATest4w"); //指定测试机的ID,通过adb命令adb devices获取
cap.setCapability("platformVersion", "5.1.1");
// capabilities.setCapability("app", app.getAbsolutePath());
//将上面获取到的包名和Activity名设置为值
cap.setCapability("appPackage", "com.android.calculator2");
cap.setCapability("appActivity", "com.android.calculator2.Calculator");
//A new session could not be created的解决方法
cap.setCapability("appWaitActivity","com.android.calculator2.Calculator");
//每次启动时覆盖session,否则第二次后运行会报错不能新建session
cap.setCapability("sessionOverride", true);
cap.setCapability("automationName","UiAutomator1");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
}
@Test
public void testplus() {
//获取1
driver.findElementById("com.android.calculator2:id/digit_1").click();
driver.findElementById("com.android.calculator2:id/op_add").click();
//获取2
driver.findElementById("com.android.calculator2:id/digit_2").click();
//获取=
driver.findElementById("com.android.calculator2:id/eq").click();
//fail("Not yet implemented");
}
@After
public void tearDown()
{
driver.quit();
}
}