1、下载eclipse
2、安装java 配置环境变量
3、eclipse 安装adt android development tools
4、android sdk manager 安装tools 和相应api的sdk版本 (安装api19,底部extras 安装了Android Support Library 和Google usb driver) sdk管理器的下载代理用 mirrors.neusoft.edu.cn 80 这个代理还是比较快
5、创建虚拟机(AVD)
3、安装nodejs 并添加环境变量: 安装文件名node-v4.1.2-x64.msi
C:\Program Files\nodejs\;
4、安装appium 执行 npm install -g appium 安装,但是这种安装非常慢,而且不知道进度在哪里
下载AppiumForWindows-1.4.0.zip 执行appium-installer.exe安装
安装后添加环境变量:
C:\Program Files (x86)\Appium;C:\Program Files (x86)\Appium\node_modules\.bin;
执行appium-doctor 检查环境是否正常:
C:\Users\dingxz>appium-doctor
Running Android Checks
✔ ANDROID_HOME is set to "D:\software\sdk\android-sdk_r23.0.2-windows\android-sd
k-windows"
✔ JAVA_HOME is set to "C:\Program Files\Java\jdk1.7.0_71."
✔ ADB exists at D:\software\sdk\android-sdk_r23.0.2-windows\android-sdk-windows\
platform-tools\adb.exe
✔ Android exists at D:\software\sdk\android-sdk_r23.0.2-windows\android-sdk-wind
ows\tools\android.bat
✔ Emulator exists at D:\software\sdk\android-sdk_r23.0.2-windows\android-sdk-win
dows\tools\emulator.exe
✔ Android Checks were successful.
✔ All Checks were successful
6、新建一个android application project项目
7、安装maven
8、 AppiumForWindows_1_4_16_1.zip 下载并安装更新 下载java-client-3.3.0.jar 并导入
http://appium.io/downloads.html(安装 AppiumForWindows_1_4_16_1 需要先安装.net)
https://search.maven.org/#artifactdetails|io.appium|java-client|3.3.0|jar 页面 有pom例子:
加maven pom 文件:
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>3.3.0</version>
</dependency>
实际加了pom.xml 文件中,eclipse 提示:failed to read artifact descriptor for io.appium:java-client:jar:3.3.0
同页面右边有完整的例子:
roject Object Model (POM)
<? xml version = "1.0" encoding = "UTF-8" ?>
<!-- ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ See the NOTICE file distributed with this work for additional
~ information regarding copyright ownership.
~ You may obtain a copy of the License at
~
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion >4.0.0</ modelVersion >
< version >3.3.0</ version >
< dependencies >
< dependency >
< version >2.2.4</ version >
</ dependency >
< dependency >
< version >2.48.2</ version >
</ dependency >
< dependency >
< version >4.11</ version >
< scope >test</ scope >
</ dependency >
< dependency >
< version >4.3.3</ version >
</ dependency >
< dependency >
< version >17.0</ version >
</ dependency >
< dependency >
< version >3.1</ version >
</ dependency >
< dependency >
< version >1.4.1</ version >
</ dependency >
</ dependencies >
< packaging >jar</ packaging >
< name >java-client</ name >
< description >Java client for Appium Mobile Webdriver</ description >
< licenses >
< license >
< name >Apache License, Version 2.0</ name >
< distribution >repo</ distribution >
</ license >
</ licenses >
< scm >
< connection >scm:git:git://github.com/appium/java-client.git</ connection >
< developerConnection >scm:git:git@github.com:appium/java-client.git</ developerConnection >
</ scm >
< developers >
< developer >
< email >jonahss@gmail.com</ email >
< name >Jonah Stiennon</ name >
< id >jonahss</ id >
</ developer >
< developer >
< email >tichomirovsergey@gmail.com</ email >
< name >Sergey Tikhomirov</ name >
< id >TikhomirovSergey</ id >
</ developer >
</ developers >
< distributionManagement >
< snapshotRepository >
< id >ossrh</ id >
</ snapshotRepository >
< repository >
< id >ossrh</ id >
</ repository >
</ distributionManagement >
< build >
< resources >
< resource >
< directory >src/main/resources</ directory >
</ resource >
</ resources >
< plugins >
< plugin >
< version >2.2.1</ version >
< executions >
< execution >
< id >attach-sources</ id >
< goals >
< goal >jar-no-fork</ goal >
</ goals >
</ execution >
</ executions >
</ plugin >
< plugin >
< version >2.9.1</ version >
< executions >
< execution >
< id >attach-javadocs</ id >
< goals >
< goal >jar</ goal >
</ goals >
</ execution >
</ executions >
</ plugin >
< plugin >
< version >1.5</ version >
< executions >
< execution >
< id >sign-artifacts</ id >
< phase >verify</ phase >
< goals >
< goal >sign</ goal >
</ goals >
</ execution >
</ executions >
</ plugin >
< plugin >
< version >3.3</ version >
< configuration >
< source >1.7</ source >
< target >1.7</ target >
< compilerId >eclipse</ compilerId >
</ configuration >
< dependencies >
< dependency >
< version >2.5</ version >
</ dependency >
</ dependencies >
</ plugin >
< plugin >
< version >2.18.1</ version >
< dependencies >
< dependency >
< version >2.18.1</ version >
</ dependency >
</ dependencies >
< executions >
< execution >
< goals >
< goal >test</ goal >
</ goals >
< phase >integration-test</ phase >
< configuration >
< includes >
< include >**/*Test.java</ include >
</ includes >
</ configuration >
</ execution >
</ executions >
</ plugin >
</ plugins >
</ build >
</ project >
|
9、启动appium.exe 点击launch the appium node server
10、 http://appium.io/ 官网有个sample 的例子
下载代码的位置:
package com.saucelabs.appium;
import static org.junit.Assert.assertEquals;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.net.URL;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
public class AndroidTest {
private AppiumDriver<WebElement> driver;
@Before
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
File app = new File(appDir, "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "io.appium.android.apis");
capabilities.setCapability("appActivity", ".ApiDemos");
driver = new AndroidDriver<>(new URL(" http://127.0.0.1:4723/wd/hub"), capabilities);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void apiDemo(){
WebElement el = driver.findElement(By.name("Animation"));
assertEquals("Animation", el.getText());
el = driver.findElementByClassName("android.widget.TextView");
assertEquals("API Demos", el.getText());
el = driver.findElement(By.name("App"));
el.click();
List<WebElement> els = driver.findElementsByClassName("android.widget.TextView");
assertEquals("Activity", els.get(2).getText());
}
}
11、下载哔哩哔哩动画.apk,
12、配置skd 环境变量, cmd adb 就可以使用了,adb install 该apk
13、启动android里面的被测试应用。同时启动 D:\soft\appium\android-sdk_r23.0.2-windows\android-sdk-windows\tools sdk tools 下面的bat文件 hierarchyviewer.bat。
被启动的应用在hierarchyviewer 里面显示为粗黑体
14、在测试代码下里面新建一个AppiumTest 类 复制上面测试例子代码中的 setup方法到类的main方法中,复制内容如下:
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
File app = new File(appDir, "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "io.appium.android.apis");
capabilities.setCapability("appActivity", ".ApiDemos");
driver = new AndroidDriver<>(new URL(" http://127.0.0.1:4723/wd/hub"), capabilities);
复制以后测试类如下:
public static void main(String[] args) throws MalformedURLException {
AppiumDriver driver;
/* // TODO Auto-generated method stub
AndroidDriver driver;
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "BiliPlayer.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "io.appium.android.apis");
capabilities.setCapability("appActivity", ".ApiDemos");
driver = new AndroidDriver(new URL(" http://127.0.0.1:4723/wd/hub"), capabilities);
System.out.println("launch");
driver.quit();*/
//设置apk的路径
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "BiliPlayer.apk");
//设置自动化相关参数
DesiredCapabilities capabilities = new DesiredCapabilities();
/* capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
*/
capabilities.setCapability("deviceName", "emulator-5554");
//设置安卓系统版本
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("platformName", "Android");
//设置apk路径
capabilities.setCapability("app", app.getAbsolutePath());
//设置app的主包名和主类名 tv.danmaku.bilixl
capabilities.setCapability("appPackage", "tv.danmaku.bilixl");
capabilities.setCapability("appActivity", ".LauncherActivity");
//初始化
driver = new AndroidDriver(new URL(" http://127.0.0.1:4723/wd/hub"),capabilities);
}
执行后提示:
xception in thread "main" org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 26.97 seconds
Build info: version: '2.48.2', revision: '41bccdd10cf2c0560f637404c2d96164b67d9d67', time: '2015-10-09 13:08:06'
System info: host: 'ding', ip: '192.168.56.1', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_45'
Driver info: io.appium.java_client.android.AndroidDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:129)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:142)
at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:37)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:161)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:170)
at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:82)
at com.test.appium.appiumtraining.AppiumTest.main(AppiumTest.java:65)
网上查询这个提示是说路径问题,也不知道具体什么路径问题。
同时也查找到有人在adb shell中 执行am start 启动应用,提供的语句为:
/home/chhagan/android/adt/sdk/platform-tools/adb -s emulator-5554 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.google.android.gm/.ConversationListActivityGoogleMail"
但是不明白am start 的语句是什么意思,随查找android am start 命令, 获得如下信息:
# am start -n {包(package)名}/{包名}.{活动(activity)名称}
如启动计算器: am start -n com.android.calculator2/com.android.calculator2.Calculator
这个命令中,需要提供程序的包名和activity 名称
随执行命令:am start -n tv.danmaku.bilixl/tv.danmaku.bilixltv.danmaku.bili.LauncherActivity(这个命令没有成功启动)
命令中的: tv.danmaku.bilixl 为包名,tv.danmaku.bilixltv.danmaku.bili.LauncherActivity 为activity 名称,名称来着于re-sign 提示
提示为不能启动:
d:\soft> adb.exe -s emulator-5554 shell "am start -n tv.danmaku.bilixl/tv.danmaku.bilixltv.danmaku.bili.LauncherActivity"
Starting: Intent { cmp=tv.danmaku.bilixl/tv.danmaku.bilixltv.danmaku.bili.LauncherActivity }
Error type 3
Error: Activity class {tv.danmaku.bilixl/tv.danmaku.bilixltv.danmaku.bili.LauncherActivity} does not exist.
d:\soft>
使用: adb logcat>D:\log.txt 获取android日志,启动被测试应用,再查看D:\log.txt 中的记录, 获得日志信息:
I/ActivityManager( 367): START u0 {flg=0x10000000 cmp= tv.danmaku.bilixl/.tv.danmaku.bilixlty.danmaku.bili.LauncherActivity} from pid 2174
让后用这个包名来启动,执行命令:
adb.exe -s emulator-5554 shell "am start -n tv.danmaku.bilixl/.tv.danmaku.bilixlty.danmaku.bili.LauncherActivity"
d:\soft>adb.exe -s emulator-5554 shell "am start -n tv.danmaku.bilixl/.tv.danmaku.bilixlty.danmaku.bili.LauncherActivity"
Starting: Intent { cmp=tv.danmaku.bilixl/.tv.danmaku.bilixlty.danmaku.bili.LauncherActivity }
Error type 3
Error: Activity class {tv.danmaku.bilixl/tv.danmaku.bilixl.tv.danmaku.bilixlty.danmaku.bili.LauncherActivity} does not exist.
结果也是启动不成功。
看样子是获取包名的数据有问题,再搜索 android 获取包名 获得在sdk 相应api版本下面的一个命令 aapt
命令格式为: aapt dump badging xxxx.apk
执行命令: aapt dump badging D:\soft\appium\android-sdk_r23.0.2-windows\android-sdk-windows\build-tools\19.1.0\BiliPlayer.apk
获得正在的包名和activity名,如下红色标记:
D:\soft\appium\android-sdk_r23.0.2-windows\android-sdk-windows\build-tools\19.1.0>aapt dump badging D:\soft\appium\android-sdk_r23.0.2-windows\android-sdk-windows\build-tools\19.1.0\BiliPlayer.apk
package: name='tv.danmaku.bilixl' versionCode='304000' versionName='1.0.0'
sdkVersion:'9'
targetSdkVersion:'21'
uses-permission:'tv.danmaku.bili.permission.MIPUSH_RECEIVE'
uses-permission:'android.permission.INTERNET'
uses-permission:'android.permission.WAKE_LOCK'
uses-permission:'android.permission.WRITE_EXTERNAL_STORAGE'
uses-permission:'android.permission.WRITE_MEDIA_STORAGE'
uses-permission:'android.permission.ACCESS_ALL_EXTERNAL_STORAGE'
uses-permission:'android.permission.ACCESS_NETWORK_STATE'
uses-permission:'android.permission.ACCESS_WIFI_STATE'
uses-permission:'android.permission.READ_PHONE_STATE'
uses-permission:'android.permission.CAMERA'
uses-feature-not-required:'android.hardware.camera'
uses-feature-not-required:'android.hardware.camera.autofocus'
uses-feature-not-required:'android.hardware.touchscreen'
application-label:'哔哩哔哩白'
application-label-ja:'哔哩哔哩白'
application-label-zh:'哔哩哔哩白'
application-label-zh_HK:'哔哩哔哩白'
application-label-zh_CN:'哔哩哔哩白'
application-label-zh_TW:'哔哩哔哩白'
application-icon-120:'res/drawable/ic_launcher.png'
application-icon-160:'res/drawable-mdpi-v4/ic_launcher.png'
application-icon-213:'res/drawable-tvdpi-v4/ic_launcher.png'
application-icon-240:'res/drawable-hdpi-v4/ic_launcher.png'
application-icon-320:'res/drawable-xhdpi-v4/ic_launcher.png'
application-icon-480:'res/drawable-xxhdpi-v4/ic_launcher.png'
application-icon-640:'res/drawable-xxxhdpi-v4/ic_launcher.png'
application: label='哔哩哔哩白' icon='res/drawable-mdpi-v4/ic_launcher.png'
l aunchable-activity: name='tv.danmaku.bili.LauncherActivity' label='哔哩哔哩白' icon='res/drawable-mdpi-v4/ic_launcher.png'
uses-permission:'android.permission.READ_EXTERNAL_STORAGE'
uses-implied-permission:'android.permission.READ_EXTERNAL_STORAGE','requested WRITE_EXTERNAL_STORAGE'
uses-feature:'android.hardware.wifi'
uses-implied-feature:'android.hardware.wifi','requested android.permission.ACCESS_WIFI_STATE, android.permission.CHANGE_WIFI_STATE, or android.permission.CHANGE_WIFI_MULTICAST_STATE permission'
uses-feature:'android.hardware.screen.portrait'
uses-implied-feature:'android.hardware.screen.portrait','one or more activities have specified a portrait orientation'
main
other-activities
search
other-services
supports-screens: 'small' 'normal' 'large' 'xlarge'
supports-any-density: 'true'
locales: '--_--' 'ja' 'zh' 'zh_HK' 'zh_CN' 'zh_TW'
densities: '120' '160' '213' '240' '320' '480' '640'
native-code: 'armeabi' 'armeabi-v7a' 'mips' 'x86'
因此,修改am start 命令:
adb.exe -s emulator-5554 shell "am start -n tv.danmaku.bilixl/tv.danmaku.bili.LauncherActivity"
结果如下:
D:\soft\eclipse-mars\workspace\appiumtraining\apps>adb.exe -s emulator-5554 shell "am start -n tv.danmaku.bilixl/tv.danmaku.bili.LauncherActivity"
Starting: Intent { cmp=tv.danmaku.bilixl/tv.danmaku.bili.LauncherActivity }
启动成功!!!
源来java代码:
capabilities.setCapability("appPackage", "tv.danmaku.bilixl");
capabilities.setCapability("appActivity", ".LauncherActivity");
修改为
capabilities.setCapability("appPackage", "tv.danmaku.bilixl");
capabilities.setCapability("appActivity", "tv.danmaku.bili.LauncherActivity");
运行脚本,也可以启动,脚本运行成功
完整启动脚本java 代码:
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
public class AppiumTest {
public static void main(String[] args) throws MalformedURLException {
AppiumDriver driver;
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "BiliPlayer.apk");
//设置自动化相关参数
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName", "emulator-5554");
//设置安卓系统版本
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("platformName", "Android");
//设置apk路径 D:\soft\eclipse-mars\workspace\appiumtraining\apps\BiliPlayer.apk
capabilities.setCapability("app", app.getAbsolutePath());
String abc = app.getAbsolutePath();
//设置app的主包名和主类名 tv.danmaku.bilixl
capabilities.setCapability("appPackage", "tv.danmaku.bilixl");
capabilities.setCapability("appActivity", "tv.danmaku.bili.LauncherActivity");
//初始化
driver = new AndroidDriver(new URL(" http://127.0.0.1:4723/wd/hub"),capabilities);
}
}
14、打开ddms
eclipse -> window ->perspective ->open perspective ->other... 选中ddms
ddms 里面可以看到一些android系统的信息
在ddms 页面 点击左侧:dump view hierarchy for UI Automator 工具,能获取到android页面的元素信息
2015-12-26