Adroid APPIUM实例步骤

Posted testway

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Adroid APPIUM实例步骤相关的知识,希望对你有一定的参考价值。

 
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)
<?xmlversion="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.
  -->
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>3.3.0</version>
    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.48.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>17.0</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.4.1</version>
        </dependency>
    </dependencies>
    <packaging>jar</packaging>
    <name>java-client</name>
    <description>Java client for Appium Mobile Webdriver</description>
    <url>http://appium.io</url>
 
    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>
 
    <scm>
        <url>https://github.com/appium/java-client</url>
        <connection>scm:git:git://github.com/appium/java-client.git</connection>
        <developerConnection>scm:git:[email protected]:appium/java-client.git</developerConnection>
    </scm>
 
    <developers>
        <developer>
            <email>[email protected]</email>
            <name>Jonah Stiennon</name>
            <url>https://github.com/jonahss</url>
            <id>jonahss</id>
        </developer>
        <developer>
            <email>[email protected]</email>
            <name>Sergey Tikhomirov</name>
            <url>https://github.com/TikhomirovSergey</url>
            <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>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerId>eclipse</compilerId>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus-compiler-eclipse</artifactId>
                        <version>2.5</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <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‘
aunchable-activity: 

以上是关于Adroid APPIUM实例步骤的主要内容,如果未能解决你的问题,请参考以下文章

uiautomator与appium的区别

Adroid Studio创建Java项目

如何用appium测试ios包

python,appium,初步 --L简

Appium代码实例

Appium代码实例