AppiumDriver升级到2.0.0版本引发的问题--Cannot instantiate the type AppiumDriver

1. 问题描述和起因

在使用Appium1.7.0及其以下版本的时候,我们可以直接使用如下代码来创建一个AppiumDriver实例进行对安卓设备的操作。

  1. 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或者是将来更高级版本

  1. <dependency>  
  2.   <groupId>io.appium</groupId>  
  3.   <artifactId>java-client</artifactId>  
  4.   <version>2.0.0</version>  
  5.   <del><version>1.7.0</version></del>  
  6. </dependency>  

2.问题分析

究其原因,发现Appium2.0.0把AppiumDriver这个类改成了抽象函数,所以到导致了这个问题
  1. public <span style="color:#ff0000;">abstract </span>class AppiumDriver extends RemoteWebDriver implements MobileDriver,  
  2.         ContextAware, Rotatable, FindsByAccessibilityId, LocationContext,  
  3.         DeviceActionShortcuts, TouchShortcuts, InteractsWithFiles,  
  4.         InteractsWithApps, ScrollsTo {  
  5.   
  6.     private final static ErrorHandler errorHandler = new ErrorHandler(  
  7.             new ErrorCodesMobile(), true);  
  8.     private URL remoteAddress;  
  9.     private RemoteLocationContext locationContext;  
  10.     private ExecuteMethod executeMethod;  
  11.   
  12.     // frequently used command parameters  
  13.     protected final String KEY_CODE = "keycode";  
  14.     protected final String PATH = "path";  
  15.     private final String SETTINGS = "settings";  
根据2.0.0的Changelog的第一点:
[plain] view plain copy
  1. Changelog  
  2.   
  3. 2.0.0  
  4.   
  5. <ul><li><span style="font-family: Arial, Helvetica, sans-serif;">AppiumDriver is now an abstract class, use IOSDriver and AndroidDriver which both extend it. You no longer need to include the PLATFORM_NAME desired capability since it's automatic for each class. Thanks to @TikhomirovSergey for all their work</span></li></ul><span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space:pre">     </span><span style="color:#3366ff;">AppiumDriver现在变成了一个抽象类,请使用从它继承下来的IOSDriver和AndroidDriver。你不再需要加上PLATFORM_NAME这个capability,因为代码会根据你使用的是IOSDriver或者AndroidDriver来确定究竟是哪个平台。</span>  
  6. </span><ul><li><span style="font-family: Arial, Helvetica, sans-serif;">ScrollTo() and ScrollToExact() methods reimplemented</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;">Zoom() and Pinch() are now a little smarter and less likely to fail if you element is near the edge of the screen. Congratulate @BJap on their first PR!</span></li></ul>  

3.问题解决

根据问题分析,把代码修改如下,使用AndroidDriver而非原来的AppiumDriver。
  1. public class NoetPadTest {  
  2.     /** 
  3.      * Create the test case 
  4.      * 
  5.      * @param testName name of the test case 
  6.      */  
  7.     private <span style="color:#ff0000;">AndroidDriver </span>driver;  
  8.   
  9.     @Before  
  10.     public void setUp() throws Exception {  
  11.         // set up appium  
  12.         File classpathRoot = new File(System.getProperty("user.dir"));  
  13.         File appDir = new File(classpathRoot, "apps");  
  14.         File app = new File(appDir, "NotePad.apk");  
  15.         DesiredCapabilities capabilities = new DesiredCapabilities();  
  16.         capabilities.setCapability("deviceName","Android");  
  17.         //capabilities.setCapability("platformVersion", "4.2");  
  18.         capabilities.setCapability("platformName""Android");  
  19.         //capabilities.setCapability("app", app.getAbsolutePath());  
  20.         capabilities.setCapability("appPackage""com.example.android.notepad");  
  21.         capabilities.setCapability("appActivity""com.example.android.notepad.NotesList");  
  22.         //capabilities.setCapability("appActivity", ".NotesList");  
  23.         driver = new <span style="color:#ff0000;">AndroidDriver</span>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);  
  24.     }   
  25.   
  26.     @After  
  27.     public void tearDown() throws Exception {  
  28.         driver.quit();  
  29.     }  


Item

Description

Warning

Author

天地会珠海分舵

转载请注明出处!

更多精彩文章请查看本人博客!

Blog Address

http://blog.csdn.net/zhubaitian



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值