AppiumDriver升级到2.0.0版本引发的问题--Cannot instantiate the type AppiumDriver
原文 http://blog.csdn.net/zhubaitian/article/details/39717889
1. 问题描述和起因
在使用Appium1.7.0及其以下版本的时候,我们可以直接使用如下代码来创建一个AppiumDriver实例进行对安卓设备的操作。
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
但在把Appium包升级到当前最新的2.0.0版本的时候,这段代码会引发如下的一问题
当前的pom.xml相关设置如下:记得把1.7.0改成2.0.0或者是将来更高级版本
<dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>2.0.0</version> <del><version>1.7.0</version></del> </dependency>
2.问题分析
究其原因,发现Appium2.0.0把AppiumDriver这个类改成了抽象函数,所以到导致了这个问题
public <span style="color:#ff0000;">abstract </span>class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, Rotatable, FindsByAccessibilityId, LocationContext, DeviceActionShortcuts, TouchShortcuts, InteractsWithFiles, InteractsWithApps, ScrollsTo { private final static ErrorHandler errorHandler = new ErrorHandler( new ErrorCodesMobile(), true); private URL remoteAddress; private RemoteLocationContext locationContext; private ExecuteMethod executeMethod; // frequently used command parameters protected final String KEY_CODE = "keycode"; protected final String PATH = "path"; private final String SETTINGS = "settings";
3.问题解决
根据问题分析,把代码修改如下,使用AndroidDriver而非原来的AppiumDriver。
public class NoetPadTest { /** * Create the test case * * @param testName name of the test case */ private AndroidDriver driver; @Before public void setUp() throws Exception { // set up appium File classpathRoot = new File(System.getProperty("user.dir")); File appDir = new File(classpathRoot, "apps"); File app = new File(appDir, "NotePad.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName","Android"); //capabilities.setCapability("platformVersion", "4.2"); capabilities.setCapability("platformName", "Android"); //capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("appPackage", "com.example.android.notepad"); capabilities.setCapability("appActivity", "com.example.android.notepad.NotesList"); //capabilities.setCapability("appActivity", ".NotesList"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @After public void tearDown() throws Exception { driver.quit(); }