转自:http://blog.csdn.net/zhubaitian/article/details/39717889

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. private final static ErrorHandler errorHandler = new ErrorHandler(
  6. new ErrorCodesMobile(), true);
  7. private URL remoteAddress;
  8. private RemoteLocationContext locationContext;
  9. private ExecuteMethod executeMethod;
  10. // frequently used command parameters
  11. protected final String KEY_CODE = "keycode";
  12. protected final String PATH = "path";
  13. private final String SETTINGS = "settings";

根据2.0.0的Changelog的第一点:

  1. Changelog
  2. 2.0.0
  3. <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>
  4. </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. @Before
  9. public void setUp() throws Exception {
  10. // set up appium
  11. File classpathRoot = new File(System.getProperty("user.dir"));
  12. File appDir = new File(classpathRoot, "apps");
  13. File app = new File(appDir, "NotePad.apk");
  14. DesiredCapabilities capabilities = new DesiredCapabilities();
  15. capabilities.setCapability("deviceName","Android");
  16. //capabilities.setCapability("platformVersion", "4.2");
  17. capabilities.setCapability("platformName", "Android");
  18. //capabilities.setCapability("app", app.getAbsolutePath());
  19. capabilities.setCapability("appPackage", "com.example.android.notepad");
  20. capabilities.setCapability("appActivity", "com.example.android.notepad.NotesList");
  21. //capabilities.setCapability("appActivity", ".NotesList");
  22. driver = new <span style="color:#ff0000;">AndroidDriver</span>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  23. }
  24. @After
  25. public void tearDown() throws Exception {
  26. driver.quit();
  27. }

最新文章

  1. 用DllImport引用的外部DLL文件如何通过clickonce发布
  2. 解决webkit浏览器中js方法中使用window.event提示未定义的问题
  3. 开源服务专题之------sshd服务安装管理及配置文件理解和安全调优
  4. 换手率的公司使用MQTT的框架
  5. 概述Log4j简介
  6. 【工作记录】c#操作win7注册表
  7. 菜鸟学习Struts——简易计算器
  8. jquer ajax
  9. 浅析Netty的异步事件驱动(二)
  10. 在masterpage中添加对usercontrol的引用
  11. linux杂记(八)linux压缩与打包
  12. Rails当你运行一个数据库回滚错误:ActiveRecord::IrreversibleMigration exception
  13. Linux--Windows与Linux互传文件
  14. 你不可错过的二维码生成与解析-java后台与前端js都有
  15. CSS Box Model 盒子模型
  16. Webpack 4教程 - 第七部分 减少打包体积与Tree Shaking
  17. vue中import引入模块路径中@符号是什么意思
  18. Mac 10.12下安装python3环境
  19. 修改jupyter notebook的默认打开地址
  20. php mongo类

热门文章

  1. 大数据学习——Storm学习单词计数案例
  2. Java中NIO、BIO、AIO相关概念及应用场景
  3. 自己写的java返回结果集封装
  4. Split The Tree
  5. intellij idea 和 myeclipse 转换
  6. php——离线执行任务
  7. centos7 安装teamviewer 报错libQt5WebKitWidgets.so.5()(64bit)
  8. Flutter学习(一)——搭建开发环境(Windows)
  9. mysql索引底层的数据结构和算法
  10. B站papi酱、陈一发、李云龙