一、环境准备:
二、使用Eclipse直接创建案例工程
1、打开Eclipse,【File】-->【New】-->【Project】
2、选择【Java Project】-->【Next】
3、输入工程名称Appium_demo,点击【Finish】
4、右键点击工程 New-Folder,新建两个文件夹:apps和libs,目录结构如下:
三、导入测试的类库
1、导入Selenum类库:
1) selenium-server-standalone-2.44.0.jar
2) selenium-java-2.44.0.zip
2、导入Appium类库:
1) java-client-1.2.1.jar
3、右键点击工程空白处,选择【Build Path】-->【Configure Build Path】
四、下载测试APK
1、下载测试的文件ContactManager.apk:https://github.com/appium/sample-code/tree/master/sample-code/apps/ContactManager
2、将下载的apk放到项目的apps目录下
五、建立package包和案例文件
1、在src文件夹上右键单击,【New】-->【package】,输入包名:com.glen.demo,点击【Finish】
2、在package下新建类:ContactsTest.java,如下:
下载地址:https://github.com/appium/sample-code/tree/master/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium
1 package com.glen.demo; 2 3 import io.appium.java_client.AppiumDriver; 4 import org.junit.After; 5 import org.junit.Before; 6 import org.junit.Test; 7 import org.openqa.selenium.By; 8 import org.openqa.selenium.WebElement; 9 import org.openqa.selenium.remote.CapabilityType; 10 import org.openqa.selenium.remote.DesiredCapabilities; 11 12 import java.io.File; 13 import java.net.URL; 14 import java.util.List; 15 16 17 public class ContactsTest { 18 private AppiumDriver driver; 19 @Before 20 public void setUp() throws Exception { 21 //设置apk的路径 22 File classpathRoot = new File(System.getProperty("user.dir")); 23 File appDir = new File(classpathRoot, "apps"); 24 File app = new File(appDir, "ContactManager.apk"); 25 26 //设置自动化相关参数 27 DesiredCapabilities capabilities = new DesiredCapabilities(); 28 capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); 29 capabilities.setCapability("platformName", "Android"); 30 capabilities.setCapability("deviceName", "Android Emulator"); 31 32 //设置安卓系统版本 33 capabilities.setCapability("platformVersion", "4.3"); 34 //设置apk路径 35 capabilities.setCapability("app", app.getAbsolutePath()); 36 37 //设置app的主包名和主类名 38 capabilities.setCapability("appPackage", "com.example.android.contactmanager"); 39 capabilities.setCapability("appActivity", ".ContactManager"); 40 41 //初始化 42 driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 43 } 44 45 @Test 46 public void addContact(){ 47 WebElement el = driver.findElement(By.name("Add Contact")); 48 el.click(); 49 List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText"); 50 textFieldsList.get(0).sendKeys("Some Name"); 51 textFieldsList.get(2).sendKeys("Some@example.com"); 52 driver.swipe(100, 500, 100, 100, 2); 53 driver.findElementByName("Save").click(); 54 } 55 56 @After 57 public void tearDown() throws Exception { 58 driver.quit(); 59 } 60 }