【问题标题】:The type AndroidDriver is not generic; it cannot be parameterized with arguments <MobileElement>AndroidDriver 类型不是通用的;它不能用参数 <MobileElement> 参数化
【发布时间】:2019-06-18 17:39:14
【问题描述】:

我有一个简单的代码来自动化我的手机,所以它会自行打开 chrome 并打开 google.com,但我收到一个错误,我不确定如何修复。

我有所有更新的罐子

package browser_tests;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.MobileElement;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

public class ChromeTest {

    public static void main(String[] args) {

        //Set the Desired Capabilities
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("deviceName", "My Phone");
        caps.setCapability("udid", "77d1232f"); //Give Device ID of your mobile phone
        caps.setCapability("platformName", "Android");
        caps.setCapability("platformVersion", "8.0");
        caps.setCapability("browserName", "Chrome");
        caps.setCapability("noReset", true);

        //Set ChromeDriver location
        System.setProperty("webdriver.chrome.driver","C:\\selenium_drivers\\chromedriver.exe");

        //Instantiate Appium Driver
        AndroidDriver<MobileElement> driver = null;
        try {
            driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);

        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        }

        //Open URL in Chrome Browser
        driver.get("http://www.google.com");
    }
}

**线程“main”java.lang.Error 中的异常:未解决的编译问题: AndroidDriver 类型不是通用的;它不能用参数参数化 AndroidDriver 类型不是通用的;它不能用参数进行参数化

at browser_tests.ChromeTest.main(ChromeTest.java:31)**

【问题讨论】:

    标签: eclipse selenium automation appium


    【解决方案1】:

    将 Android 驱动程序设置为从其findElement 调用返回MobileElement 不是错误(请参阅AndroidDriver class Javadoc in Appium's Github),因此我们可以排除此问题。

    /**
     * Android driver implementation.
     *
     * @param <T> the required type of class which implement {@link org.openqa.selenium.WebElement}.
     *           Instances of the defined type will be returned via findElement* and findElements*.
     *           Warning (!!!). Allowed types:
     *           {@link org.openqa.selenium.WebElement}
     *           {@link org.openqa.selenium.remote.RemoteWebElement}
     *           {@link io.appium.java_client.MobileElement}
     *           {@link io.appium.java_client.android.AndroidElement}
     */
    public class AndroidDriver<T extends WebElement>
    

    但是您的问题是,在调用 new AndroidDriver 时,不应将泛型类型放入构造函数调用 / 中。使用示例见 Appium 的 Github 上的 BaseAndroidTestline 34line 54

    public class BaseAndroidTest {
        // ...
        protected static AndroidDriver<AndroidElement> driver;
    
        @BeforeClass public static void beforeClass() {
            // ...
            driver = new AndroidDriver<>(service.getUrl(), capabilities);
    }
    

    因此,要解决您的问题,只需从 new AndroidDriver 构造函数中删除 MobileElement,但将其保留在声明中:

    //Instantiate Appium Driver
    AndroidDriver<MobileElement> driver;
    try {
       driver = new AndroidDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
    } catch (MalformedURLException e) {
        System.out.println(e.getMessage());
    }
    

    您的驱动程序应该实例化。

    【讨论】:

    • 我尝试更改代码,但仍然收到错误消息:Exception in thread "main" java.lang.Error: Unresolved compilation problems: The type AndroidDriver is not generic; it cannot be parameterized with arguments &lt;MobileElement&gt; The type AndroidDriver is not generic; it cannot be parameterized with arguments &lt;&gt; at browser_tests.ChromeTest.main(ChromeTest.java:28)
    • @BryanPerez 你能看看这个 github 问题的响应中的代码吗? github.com/appium/java-client/issues/700 这与您遇到的问题几乎相同,因此请尝试使用 TikhomirovSergey 的代码,它可能会帮助您解除阻塞。
    猜你喜欢
    • 2019-10-14
    • 2016-06-01
    • 2015-06-15
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多