(1)安装Android Studio(大礼包,啥都有)或Android SDK
(2)安装Appium Desktop,设置AndroidSDK地址,直接启动
(3)真手机,打开usb调试,连接电脑
(4)我是Android Studio,在C:\Users\Administrator\AppData\Local\Android\Sdk\platform-tools下有个adb.exe, 执行adb devices,可以看到手机的id。
(5)执行adb logcat,然后手机打开计算器,从log里找到包名和appActivity名
01-03 23:01:58.640 726 726 E ANDR-IOP: IOP HAL: Received pkg_name = cn.nubia .calculator2.preset pid = 0 01-03 23:01:58.640 1253 1332 I ActivityManager: Displayed cn.nubia.calculator2 .preset/cn.nubia.calculator2.Calculator: +197ms
(6)java程序走起~~ Maven导入jar包
<!-- https://mvnrepository.com/artifact/io.appium/java-client --> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>7.3.0</version> </dependency>
(7)代码来了
import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.By; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.android.AndroidDriver;
public class CalculatorTest {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName", "9f0d769"); //我的手机ID capabilities.setCapability("automationName", "Appium"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("platformVersion", "8.1.0"); //要和手机的android版本一致 capabilities.setCapability("appPackage", "cn.nubia.calculator2.preset"); //自己用logcat找,每个手机可能不一样 capabilities.setCapability("appActivity", "cn.nubia.calculator2.Calculator"); //自己用logcat找,每个手机可能不一样 //capabilities.setCapability(MobileCapabilityType.APP, "c:\\xxx.apk" ); //手机没装的,可以上传 capabilities.setCapability("noReset", "true");
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
System.out.println(driver.getPageSource()); //我用的打印大法,找控件 driver.findElement(By.id("cn.nubia.calculator2.preset:id/digit1")).click(); driver.findElement(By.id("cn.nubia.calculator2.preset:id/plus")).click(); driver.findElement(By.id("cn.nubia.calculator2.preset:id/digit6")).click(); driver.findElement(By.id("cn.nubia.calculator2.preset:id/equal")).click(); Thread.sleep(1000);
String result = driver.findElement(By.id("cn.nubia.calculator2.preset:id/cal_txt2")).getText(); System.out.println(result);
driver.quit(); }
}
这就行了。
(8)打印的pageSource是这样:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <hierarchy index="0" class="hierarchy" rotation="0" width="1080" height="2040"> <android.widget.FrameLayout index="0" package="cn.nubia.calculator2.preset" class="android.widget.FrameLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,0][1080,2040]" displayed="true"> <android.widget.LinearLayout index="0" package="cn.nubia.calculator2.preset" class="android.widget.LinearLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,0][1080,2040]" displayed="true"> <android.widget.FrameLayout index="0" package="cn.nubia.calculator2.preset" class="android.widget.FrameLayout" text="" resource-id="android:id/content" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,0][1080,2040]" displayed="true"> <android.widget.RelativeLayout index="0" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,0][1080,2040]" displayed="true"> <android.widget.TextView index="0" package="cn.nubia.calculator2.preset" class="android.widget.TextView" text="" resource-id="cn.nubia.calculator2.preset:id/equation" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,291][1080,405]" displayed="true" /> <android.widget.ViewSwitcher index="1" package="cn.nubia.calculator2.preset" class="android.widget.ViewSwitcher" text="" resource-id="cn.nubia.calculator2.preset:id/display" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,345][1080,855]" displayed="true"> <android.widget.EditText index="0" package="cn.nubia.calculator2.preset" class="android.widget.EditText" text="" resource-id="cn.nubia.calculator2.preset:id/cal_txt2" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="true" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[0,345][1080,855]" displayed="true" /> </android.widget.ViewSwitcher> <android.support.v4.view.ViewPager index="2" package="cn.nubia.calculator2.preset" class="android.support.v4.view.ViewPager" text="" resource-id="cn.nubia.calculator2.preset:id/panelswitch" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="true" selected="false" bounds="[0,825][1080,2040]" displayed="true"> <android.widget.GridLayout index="0" package="cn.nubia.calculator2.preset" class="android.widget.GridLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,825][1080,2040]" displayed="true"> <android.widget.RelativeLayout index="0" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/clear" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[46,825][292,1065]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[69,845][269,1045]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="1" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/leftAndrightParen" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[292,825][538,1065]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[315,845][515,1045]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="2" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/del" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[538,825][784,1065]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[561,845][761,1045]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="3" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/div" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[784,825][1030,1065]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[807,845][1007,1045]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="4" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/digit7" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[46,1065][292,1305]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[69,1085][269,1285]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="5" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/digit8" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[292,1065][538,1305]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[315,1085][515,1285]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="6" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/digit9" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[538,1065][784,1305]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[561,1085][761,1285]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="7" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/mul" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[784,1065][1030,1305]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[807,1085][1007,1285]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="8" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/digit4" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[46,1305][292,1545]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[69,1325][269,1525]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="9" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/digit5" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[292,1305][538,1545]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[315,1325][515,1525]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="10" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/digit6" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[538,1305][784,1545]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[561,1325][761,1525]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="11" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/minus" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[784,1305][1030,1545]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[807,1325][1007,1525]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="12" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/digit1" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[46,1545][292,1785]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[69,1565][269,1765]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="13" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/digit2" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[292,1545][538,1785]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[315,1565][515,1765]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="14" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/digit3" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[538,1545][784,1785]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[561,1565][761,1765]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="15" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/plus" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[784,1545][1030,1785]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[807,1565][1007,1765]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="16" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/digit0" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[46,1785][505,2025]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[68,1805][482,2005]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="17" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/dot" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[538,1785][784,2025]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[561,1805][761,2005]" displayed="true" /> </android.widget.RelativeLayout> <android.widget.RelativeLayout index="18" package="cn.nubia.calculator2.preset" class="android.widget.RelativeLayout" text="" resource-id="cn.nubia.calculator2.preset:id/equal" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[784,1785][1030,2025]" displayed="true"> <android.widget.ImageView index="0" package="cn.nubia.calculator2.preset" class="android.widget.ImageView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[807,1805][1007,2005]" displayed="true" /> </android.widget.RelativeLayout> </android.widget.GridLayout> </android.support.v4.view.ViewPager> </android.widget.RelativeLayout> </android.widget.FrameLayout> </android.widget.LinearLayout> </android.widget.FrameLayout> </hierarchy>
|