Java 无法实例化 AppiumDriver 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26715642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:08:31  来源:igfitidea点击:

Cannot instantiate the type AppiumDriver

javaseleniumappium

提问by paul

I have added following jars in my projects build path:

我在我的项目构建路径中添加了以下 jars:

  1. java-client-2.0.0 from http://appium.io/downloads.html>> Appium Client libraries >> Java
  2. selenium-java-2.43.1
  3. selenium-java-2.43.1-srcs
  4. selenium-server-standalone-2.43.1
    and here's my code:

    public class SampleApp{
    
    WebDriver dr;
    
    @Test
    public void testApp() throws MalformedURLException, InterruptedException {
        String apkpath = "D:\apkdump\sampleapp.apk";
        File app = new File (apkpath);
        DesiredCapabilities capabilities= new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME,"");
        capabilities.setCapability("deviceName","TestADB18");
        capabilities.setCapability("platformName","Android");
        capabilities.setCapability("app",app.getAbsolutePath());
        capabilities.setCapability("appPackage", "com.test");
        capabilities.setCapability("appActivity", "com.sampleapp.Main");
        dr = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
        dr.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    
    }
    
    
    }
    

    I am getting red line below new AppiumDriverwhich says that Cannot instantiate the type AppiumDriver. Now if remove all selenium jarsthe error disappears but then I can't resolve errors with webdriver.

    What is the conflict between jars?

    I saw similar question herebut that could run the code and was getting Null pointer exceptionbut in my case I cant even run it, it is giving run on saving the code. Secondly the answer has been posted without usingAppiumDriver

  1. java-client-2.0.0 来自http://appium.io/downloads.html>> Appium 客户端库 >> Java
  2. 硒-java-2.43.1
  3. selenium-java-2.43.1-srcs
  4. selenium-server-standalone-2.43.1
    这是我的代码:

    public class SampleApp{
    
    WebDriver dr;
    
    @Test
    public void testApp() throws MalformedURLException, InterruptedException {
        String apkpath = "D:\apkdump\sampleapp.apk";
        File app = new File (apkpath);
        DesiredCapabilities capabilities= new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME,"");
        capabilities.setCapability("deviceName","TestADB18");
        capabilities.setCapability("platformName","Android");
        capabilities.setCapability("app",app.getAbsolutePath());
        capabilities.setCapability("appPackage", "com.test");
        capabilities.setCapability("appActivity", "com.sampleapp.Main");
        dr = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
        dr.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    
    }
    
    
    }
    

    我在下面new AppiumDriver看到红线,上面写着Cannot instantiate the type AppiumDriver。现在,如果删除所有selenium jars错误消失,但我无法解决 webdriver 的错误。

    jar 之间的冲突是什么?

    我在这里看到了类似的问题但是可以运行代码并且正在运行,Null pointer exception但是在我的情况下,我什至无法运行它,它正在运行保存代码。其次,答案已发布而未使用AppiumDriver

采纳答案by paul

This error can be fixed by downgrading the Appium Client(see step 1 in my question) from latest to java-client-1.5.0. You can keep rest of the jars to latest.

可以通过将Appium Client(参见我的问题中的第 1 步)从 latest降级到java-client-1.5.0. 您可以将其余的罐子保持最新。

Downgraded version of Appium Clientcan be downloaded from here http://mvnrepository.com/artifact/io.appium/java-client/1.5.0

Appium Client可以从这里下载的 降级版本http://mvnrepository.com/artifact/io.appium/java-client/1.5.0

回答by Hassan Radi

You don't need to downgrade or anything. There is a design change in the Java Client version 2.0.0 as they mention on their site:

你不需要降级或任何东西。正如他们在他们的网站上提到的,Java 客户端 2.0.0 版中有一个设计更改:

AppiumDriver is now an abstract class, use IOSDriver and AndroidDriver which both extend it.

AppiumDriver 现在是一个抽象类,使用 IOSDriver 和 AndroidDriver 扩展它。

So, just change your driver line to be:

因此,只需将您的驱动程序行更改为:

dr = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);

Hope that helps...

希望有帮助...

回答by Satya Ram hari krishna swamy

WebDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),getDesiredCapabilities("192.21.168.56:5555"));

use this. and import:

用这个。并导入:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

回答by Guru

dr=new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);

dr=new AppiumDriver(new URL(" http://127.0.0.1:4723/wd/hub"),capabilities);

proper URL to be established

要建立正确的 URL

回答by Manish Patil

for appium version 1.7 use capability cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");

对于 appium 1.7 版使用功能 cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");

while for v1.8.1 its not needed

而对于 v1.8.1 则不需要