我们先看一段真实的java代码,我将他放在了TestNG框架的@BeforeTest里,执行这段即可启动安卓手机上的APP“com.tencent.Q108” 后边一一详述
1 @BeforeTest
2 public void setUp() throws MalformedURLException{
3 DesiredCapabilities caps = new DesiredCapabilities();
4 // des.setCapability("app", "c:\\");
5 caps.setCapability("automationname", "Appium");
6 caps.setCapability("platformName", "Android");
7 caps.setCapability("platformVersion", "23");
8 caps.setCapability("udid", "6EJ4C18A18000811");
9 caps.setCapability("deviceName", "Honor");
10 caps.setCapability("appPackage", "com.tencent.Q108");//com.android.contacts
11 caps.setCapability("appActivity", "com.tencent.Q108.SplashActivity");//.activities.PeopleActivity
12 caps.setCapability("appWaitActivity", "com.tencent.Q108.SplashActivity");
13 caps.setCapability("unicodeKeyboard", "True");
14 caps.setCapability("resetKeyboard", "True");
15 caps.setCapability("newCommandTimeout", "15");
16 caps.setCapability("nosign", "True");
17 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),caps);
18 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
19 }
测试脚本到驱动移动端的整个运转过程,Desired capability是以一个JSON对象形式传递的,包含一组key和value值。它由客户端发送给服务端,告诉服务端期望的Capabilities(可以理解为一种能力)有哪些,然后服务端根据这些capabilities创建自动化会话(session)
手机连接电脑或模拟器开启,并确定已经开机手机的USB调试,CMD下输入adb devices
,如果出现以下提示,则大概率上是因为移动端没有成功开启USB调试
确认USB调试开启后,再次输入命令,则
所获取的串6EJ4C18A18000811
就是udid
如果没有build-tools那说明还没有下载,可打开SDK Manager自行下载,下载完成后,添加Path路径即可
将准备测试的 APK 放到某个目录,在相同的目录下启动CMD,并执行如下命令:aapt dump badging tencentmobilemanager.apk(根据各自的apk包名来)
如图所示,能看到package:name/launchable activity:name, 这两项即使我们要获取的"appPackage" 和 “appActivity”
注意这个地址可以自定义,例如你的手机连接在别的机器上通过这个IP的配置,一样可以实现驱动,并非只能驱动链接到本机的移动设备
io.appium.java_client 也提供了MobileCapabilityType和AndroidMobileCapabilityType,看如下代码,也可以设置Capabilities
1 package testscript;
2 import io.appium.java_client.TouchAction;
3 import io.appium.java_client.android.AndroidDriver;
4 import io.appium.java_client.remote.AndroidMobileCapabilityType;
5 import io.appium.java_client.remote.MobileCapabilityType;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.concurrent.TimeUnit;
9 import org.openqa.selenium.remote.DesiredCapabilities;
10 import org.testng.annotations.AfterClass;
11 import org.testng.annotations.BeforeClass;
12 import org.testng.annotations.Test;
13
14 public class Test_Scroll_Swipe {
15 AndroidDriver driver;
16 @BeforeClass
17 public void setUp() throws MalformedURLException{
18 DesiredCapabilities caps = new DesiredCapabilities();
19 caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
20 caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
21 caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
22 caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.google.android.launcher");
23 caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.google.android.launcher.StubApp");
24 driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
25 driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
26 }
在Desired Capabilities的设置上Java和Python是同理的