代码:
package org.newlight.appiumDemo;
import io.appium.java_client.AppiumDriver;
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.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
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;
public class CopyOfAndroidContactsTest {
private AppiumDriver driver;
@Before
public void setUp() throws Exception {
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(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 AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@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();
}
}
运行结果如下:
823
723
java.lang.NoSuchMethodError: org.openqa.selenium.remote.ErrorHandler.<init>(Lorg/openqa/selenium/remote/ErrorCodes;Z)V
at io.appium.java_client.AppiumDriver.<clinit>(AppiumDriver.java:37)
at org.newlight.appiumDemo.CopyOfAndroidContactsTest.setUp(CopyOfAndroidContactsTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
报错语句为: AppiumDriver dr = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
引入的包有:
测试机器:
模拟器:安卓 4.4
真机:安卓 4.4
而,使用 webdriver 是正常的:
package org.newlight.appiumDemo;
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.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
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;
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(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);
}
@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;
}
}
}