AppiumDriver升級到2.0.0版本引發的問題--Cannot instantiate the type AppiumDriver,2.0.0--cannot

來源:互聯網
上載者:User

AppiumDriver升級到2.0.0版本引發的問題--Cannot instantiate the type AppiumDriver,2.0.0--cannot
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 parametersprotected final String KEY_CODE = "keycode";protected final String PATH = "path";private final String SETTINGS = "settings";
根據2.0.0的Changelog的第一點:

Changelog2.0.0<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></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。
public class NoetPadTest {    /**     * Create the test case     *     * @param testName name of the test case     */private <span style="color:#ff0000;">AndroidDriver </span>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 <span style="color:#ff0000;">AndroidDriver</span>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);    }     @After    public void tearDown() throws Exception {        driver.quit();    }







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.