1.创建一个maven项目
成功新建工程:
编辑pom.xml,在<dependencies></dependencies>下添加appium相关依赖:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.10</version> <scope>test</scope> </dependency> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>LATEST</version> <exclusions> <exclusion> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.saucelabs</groupId> <artifactId>sauce_junit</artifactId> <version>LATEST</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-remote-driver</artifactId> <version>2.53.0</version> </dependency>
然后在<dependencies></dependencies>后面加上appium专用库:
<repositories> <repository> <id>saucelabs-repository</id> <url>https://repository-saucelabs.forge.cloudbees.com/release</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
保存之后会自动下载Maven Dependencies相关jar包
2.新建一个TestNG class
右键选择com.sun.appiumdemo
TestNG->Create TestNG class
具体代码如下:
package com.sun.appiumdemo; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.AndroidDriver; public class AppiumTest { public AppiumDriver<WebElement> driver; @BeforeClass public void startTest() throws MalformedURLException { File classpathRoot = new File(System.getProperty("user.dir")); File appDir = new File(classpathRoot, "res/app"); File app = new File(appDir, "ContactManager.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("automationName", "Appium"); capabilities.setCapability("platformName","Android"); capabilities.setCapability("deviceName","Android Emulator"); capabilities.setCapability("platformVersion", "4.4.2"); capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("appPackage", "com.example.android.contactmanager"); capabilities.setCapability("appActivity", ".ContactManager"); driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @Test public void addContact() { WebElement el = driver.findElement(By.xpath(".//*[@text='Add Contact']")); el.click(); List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText"); textFieldsList.get(0).sendKeys("Some Name"); textFieldsList.get(2).sendKeys("Some@example.com"); driver.swipe(100, 500, 100, 100, 2); driver.findElementByXPath(".//*[@text='Save']").click(); } @AfterClass public void afterClass() { driver.quit(); } }
在maven项目的根目录下新建res/app目录,将安卓测试APP放在此目录。
测试应用下载地址 :
链接: http://pan.baidu.com/s/1skPrdVJ 密码: bkvh
3.新建虚拟机或者真机运行
模拟器推荐使用Genymotion,直接下载 .exe,双击安装。
下载地址:
链接: http://pan.baidu.com/s/1nv3YXZz 密码: 3u93
如果想要用真机测试,那么需要打开真机的USB调试模式,打开CMD输入adb devices命令,返回一下设备信息表明设备开启调试模式成功。
4.启动Appium Server
启动Appium Server,用于接收和处理来自client的请求。双击已经安装好的Appium
如图所示表示启动成功
5.执行测试用例
执行测试用用例前,确保你的安卓模拟器已经打开或者真机已经打开USB调试并连接到电脑并且Appium Server启动成功。打开eclipse并打开AppiumTest.java,鼠标右键执行run as TestNG test,用例就开始执行了
Appium服务端就会输出相关信息
用例执行完毕。