1 package com.saucelabs.appium;
2
3 import io.appium.java_client.AppiumDriver;
4 import io.appium.java_client.android.AndroidDriver;
5
6 import java.io.File;
7 import java.net.URL;
8 import java.util.List;
9
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.openqa.selenium.By;
14 import org.openqa.selenium.WebElement;
15 import org.openqa.selenium.remote.CapabilityType;
16 import org.openqa.selenium.remote.DesiredCapabilities;
17
18 public class AndroidContactsTest {
19 private AppiumDriver driver;
20
21 @Before
22 public void setUp() throws Exception {
23 // set up appium
24 File classpathRoot = new File(System.getProperty("user.dir"));
25 File appDir = new File(classpathRoot, "../../../apps/ContactManager"); //目录位置对应好
26 File app = new File(appDir, "ContactManager.apk");
27 DesiredCapabilities capabilities = new DesiredCapabilities();
28 capabilities.setCapability("deviceName","Android Emulator");
29 capabilities.setCapability("platformVersion", "4.4");
30 capabilities.setCapability("app", app.getAbsolutePath());
31 capabilities.setCapability("appPackage", "com.example.android.contactmanager");
32 capabilities.setCapability("appActivity", ".ContactManager");
33 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
34 }
35
36 @After
37 public void tearDown() throws Exception {
38 driver.quit();
39 }
40
41 @Test
42 public void addContact(){
43 WebElement el = driver.findElement(By.name("Add Contact"));
44 el.click();
45 List
46 textFieldsList.get(0).sendKeys("Some Name");
47 textFieldsList.get(2).sendKeys("Some@example.com");
48 driver.swipe(100, 500, 100, 100, 2);
49 driver.findElementByName("Save").click();
50 }
51
52 }
调试时遇到问题1:AndroidDriver包找不到,原因是我前面导的包是java-client-1.2.1.jar,需要导入新的包java-client-2.1.0.jar,下载地址:https://search.maven.org/remotecontent?filepath=io/appium/java-client/2.1.0/java-client-2.1.0.jar
import io.appium.java_client.android.AndroidDriver;
调试时遇到问题2:调试时跑起来没问题,执行到44行时程序退出,eclipse的failure trace报的错是找不到element,考虑到43行元素已经定位都没问题,觉得应该是第二个页面元素还没找到,于是在44行后添加如下代码,等待页面加载
try{Thread.sleep(10);}catch(Exception e){}
调试时遇到问题3:定位行数为42,即@Test后的函数那行,这个问题基本都是由于appium没启动或者长时间开着的缘故,一般调试前最好重新launch下
org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
System info: host: 'PC201404231316', ip: '192.168.18.100', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45'