步骤如下:
1、搭建安卓环境,需要设置系统变量“ANDROID_HOME”。
2、安装Node.js
http://www.nodejs.org/download/ 下载相关操作系统的版本
3、安装Appium
(1)下载地址:http://appium.io/ 下载完毕后解压到pc本地,将压缩包里有appium.cmd文件的路径设置到环境变量path中。
(2)进入命令行,输入appium-doctor
另外也可在线安装(可选,网络不好还是选上面的好些)
(1)进入命令行
(2)输入命令:npm install -g appium
注:翻墙下载比较靠谱
(3)等待更新完毕
(4)安装后验证:appium-doctor
4、创建虚拟机或使用实际设备,要记住虚拟机的名称
1、此虚拟机名称为5554
2、真机时,点击设置,点击关于手机,查看型号,如华手机型号:HDC-30
5、编写Test,使用ADT安装好Maven插件,创建一个Maven项目,添加一个文件夹apps用来存放被测的app,这里测试的是ContactManager.apk。
备注:在最新版的eclipse中,点击help,点击eclipse marketplace,在find中输入maven,
找到此项,安装。成功后,新建maven工程,
5、(1)导入Selenium类库
http://selenium-release.storage.googleapis.com/2.44/selenium-java-2.44.0.zip
http://selenium-release.storage.googleapis.com/2.44/selenium-server-standalone-2.44.0.jar
(如无效请访问http://docs.seleniumhq.org/download/ 进行下载 )
注:需要导入两个,一个是客户端,一个是服务端,服务端库不导入会导致运行时报错。
(2)导入Appium类库
https://search.maven.org/remotecontent?filepath=io/appium/java-client/1.2.1/java-client-1.2.1.jar
(如无效请访问http://appium.io/downloads.html 进行下载 )
pom.xml添加如下依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>LATEST</version>
<scope>test</scope>
</dependency>
</dependencies>
编写AndroidContactsTest
package com.guowen.appiumdemo;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.HasTouchScreen;
import org.openqa.selenium.interactions.TouchScreen;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteTouchScreen;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.io.File;
import java.net.URL;
import java.util.List;
public class AndroidContactsTest {
private WebDriver driver;
@Before
public void setUp() throws Exception {
// set up appium
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps/ContactManager");
File app = new File(appDir, "ContactManager.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("platformName", "Android");
// 设备名称在设置中,点击关于手机,型号
// capabilities.setCapability("deviceName","Android Emulator");
// capabilities.setCapability("deviceName","Emulator-5554");
capabilities.setCapability("deviceName","H30-C00");
capabilities.setCapability(CapabilityType.VERSION, "4.4");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("app-package", "com.example.android.contactmanager");
capabilities.setCapability("app-activity", ".ContactManager");
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void addContact(){
WebElement el = driver.findElement(By.name("Add Contact"));
el.click();
List<WebElement> textFieldsList = driver.findElements(By.tagName("textfield"));
textFieldsList.get(0).sendKeys("Some Name");
textFieldsList.get(2).sendKeys("Some@example.com");
driver.findElement(By.name("Save")).click();
}
public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
private RemoteTouchScreen touch;
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
touch = new RemoteTouchScreen(getExecuteMethod());
}
public TouchScreen getTouch() {
return touch;
}
}
}
下载示例程序
https://github.com/appium/sample-code/tree/master/sample-code/apps/ContactManager
上面链接中下载ContactManager.apk
并将该apps放到当前项目的apps-ContactManager目录下(目录自行创建)
6. 点击appium.exe,运行。
7.
8. 运行maven程序,run as JunitTest
备注:
1、建立的虚拟机必须在API18或者以上
2、注意AVD里的Android如果没有解锁需要先解锁
3、在程序执行这程中,用Send_keys()函数对输入框输入值的时候,无法输入?与手机系统或者自带的输入法有关,因为一输入就会打开软键盘,而软键盘上面的布局是九宫格的,点击一直出错。卸载安装的输入法,保留系统输入法,此问题正在解决,给大家留个任务,会的告诉我,我补充。
4、关于脚本的编写,请学习webdriver,class name
<mirror>
<id>UK</id>
<name>UK Central</name>
<url>http://uk.maven.org/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>