maven appium从5.0.4版升级到6.1.0版后,初始化页面工厂类时遇到java.lang.ExceptionInInitializerError

0yycz8jy  于 2023-03-01  发布在  Maven
关注(0)|答案(5)|浏览(163)

我最近更新了我的appium java-client maven依赖项,以促进触摸功能。
在运行之前执行良好的脚本时遇到以下错误。

java.lang.ExceptionInInitializerError
at io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy(ProxyFactory.java:52)
    at io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy(ProxyFactory.java:33)
    at io.appium.java_client.pagefactory.AppiumFieldDecorator.proxyForAnElement(AppiumFieldDecorator.java:217)
    at io.appium.java_client.pagefactory.AppiumFieldDecorator.access$0(AppiumFieldDecorator.java:215)
    at io.appium.java_client.pagefactory.AppiumFieldDecorator$1.proxyForLocator(AppiumFieldDecorator.java:107)
    at org.openqa.selenium.support.pagefactory.DefaultFieldDecorator.decorate(DefaultFieldDecorator.java:62)
    at io.appium.java_client.pagefactory.AppiumFieldDecorator.decorate(AppiumFieldDecorator.java:155)
    at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:113)
    at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:105)
    at org.framework.adib.selendroidTestApp.pages.HomePage.<init>(HomePage.java:18)
    at org.framework.adib.selendroidTestApp.StepDefinition.SelendroidTest.user_opens_Selendroid_application(SelendroidTest.java:39)
    at ✽.Given User opens Selendroid application(src/test/java/org/framework/adib/selendroidTestApp/Feature/MyApplicationFeature.feature:5)
    • POM定义**
<!--https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.14.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>6.1.0</version>
        </dependency>
    • 类定义:**
public class HomePage {

    private AndroidDriver<AndroidElement> driver;

    public HomePage(AndroidDriver<AndroidElement> driver) {
        this.driver = driver;
        try {
            PageFactory.initElements(new AppiumFieldDecorator(driver), this);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

任何帮助都将不胜感激。
用于初始化Core项目中的驱动程序的基类:'

private static AppiumDriver<MobileElement> androiddriver;

private static IOSDriver<IOSElement> iosdriver;

/**
 * Gets the driver.
 *
 * @return the driver
 */
public static AppiumDriver<MobileElement> getAndroidDriver() {
    return androiddriver;
}

/**
 * Gets the driver.
 *
 * @return the driver
 */
public static IOSDriver<IOSElement> getIosDriver() {
    return iosdriver;
}

private static AppiumDriverLocalService service;
private static AppiumServiceBuilder builder;
private static DesiredCapabilities cap;

/**
 * Method to initialize the appium driver.
 * 
 * @param: appURL
 *             location of the apk file
 * @param: CONFIG
 *             Config properties of the application under test
 */
public static void openApplication(String appUrl, Properties config) throws MalformedURLException {
    File appDir = new File(appUrl);

    File app = new File(appDir, config.getProperty("APP_NAME"));

    // defining appium url
    String appiumUrl = "http://" + config.getProperty("APPIUM_IP") + ":" + config.getProperty("APPIUM_PORT")
            + "/wd/hub";

    boolean isRunning = checkIfServerIsRunnning(config.getProperty("APPIUM_IP"),
            Integer.parseInt(config.getProperty("APPIUM_PORT")));

    if (!isRunning) {
        startServer(config);
    }

    switch (config.getProperty("OS")) {
        default:
            // Log.info("OS version is not correct");
            break;
        case "ANDROID": {
            // Defining APIUM Capabilities
            DesiredCapabilities cap = new DesiredCapabilities();
            // cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
            cap.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
            cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, config.getProperty("PLATFORM_VERSION"));
            cap.setCapability(MobileCapabilityType.DEVICE_NAME, config.getProperty("EMU_NAME"));
            cap.setCapability(MobileCapabilityType.BROWSER_NAME, config.getProperty("BROWSER_NAME"));
            cap.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, config.getProperty("APP_PACKAGE"));
            cap.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, config.getProperty("APP_ACTIVITY"));
            cap.setCapability(MobileCapabilityType.FULL_RESET, false);
            cap.setCapability(MobileCapabilityType.NO_RESET, true);
            cap.setCapability(MobileCapabilityType.UDID, config.getProperty("EMU_NAME"));
            // if(config.getProperty("IS_PHYSICAL").equalsIgnoreCase("FALSE"))
            // {
            // //To run with emulator
            // cap.setCapability(MobileCapabilityType.u"avd",
            // config.getProperty("EMU_NAME"));
            //
            // }
            androiddriver = new AppiumDriver<MobileElement>(new URL(appiumUrl), cap);

            // Implicit wait definition
            androiddriver.manage().timeouts().implicitlyWait(Integer.parseInt(config.getProperty("TIME_OUT")),
                    TimeUnit.SECONDS);

            System.out.println("The session ID for " + config.getProperty("EMU_NAME") + " is : " + ""
                    + androiddriver.getSessionId());
            break;
        }`

测试项目中的测试项目中的设置类:

public static AppiumDriver<MobileElement> driver;
    public static XlsReader XLR = null;
    Scenario scenario = null;
    public static Properties CONFIG;
    static CommonFunctionAndroidImpl com_fun = null;
    protected static String appUrl;

@Before
public void beforeMethod(Scenario scenario) throws Exception {

    DOMConfigurator.configure("log4j.xml");

    // getting instance of scenario
    this.scenario = scenario;
    Log.startTestCase(scenario.getName());

    // Initialize Configurations
    CONFIG = Config.initializeConfig();

    // Initialize App location
    appUrl = (System.getProperty("user.dir") + CONFIG.getProperty("APP_URL"));
    Log.info("App location:" + appUrl);

    // Initialize Appium Driver
    BaseClass.openApplication(appUrl, CONFIG);
    driver = BaseClass.getAndroidDriver();

    // Initialize XCEL for Data
    XLR = new XlsReader(System.getProperty("user.dir") + CONFIG.getProperty("DATA_FILE"));

    // Initialize Common Function
    com_fun = new CommonFunctionAndroidImpl(driver);
}`

实际测试类别:

CommonFunctionAndroidImpl com_fun = Setup.com_fun;

// Variables for accessing XLS methods
XlsReader XLR = Setup.XLR;

String name, userName, email, pwd, lang, adds;
int rowNum = 0;

HashMap<String, String> data = null;

// Page objects for all the application pages
protected HomePage hp;
protected static NewRegistrationPage rp;
protected static ConfirmRegistrationPage cp = null;

@Given("^User opens Selendroid application$")
public void user_opens_Selendroid_application() throws Throwable {

    hp = new HomePage(Setup.driver);
}

@When("^User clicks on Create New User icon$")
public void user_clicks_on_Create_New_User_icon() throws Throwable {
    rp = hp.clickNewRegBttn();
}

主页类:

private AppiumDriver<MobileElement> driver;

public HomePage(AppiumDriver<MobileElement> driver) {
    this.driver = driver;
    try {
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    } catch (Exception e) {

        e.printStackTrace();
    }
}

@AndroidFindBy(id = "io.selendroid.testapp:id/startUserRegistration")
public WebElement newRegBttn;

@AndroidFindBy(id = "android:id/title")
public WebElement appTitle;

@AndroidFindBy(id = "io.selendroid.testapp:id/showToastButton")
public WebElement displayToast;

@AndroidFindBy(id = "io.selendroid.testapp:id/showPopupWindowButton")
public WebElement displayPopup;

public NewRegistrationPage clickNewRegBttn() {
    newRegBttn.click();
    return new NewRegistrationPage(driver);
}

public void clickDisplayToast() {
    displayToast.click();
}

public void clickDisplayPopup() {
    displayPopup.click();
}

public boolean appTitleTextDisplayed() {
    return appTitle.isDisplayed();
}
ctehm74n

ctehm74n1#

安卓驱动程序更改为苹果驱动程序,并将安卓元素替换为移动元素网络元素

public HomePage(AppiumDriver<MobileElement> driver) {

    try {
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    } catch (Exception e) {

        e.printStackTrace();
    }
}
xmd2e60i

xmd2e60i2#

试试看:
我也遇到过同样的问题,这是因为java客户端或TestNG之间的版本不合适或旧版本依赖关系。一旦我用下面的TestNG版本更新了pom中的java客户端版本,问题就解决了。

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>7.0.0</version>
  <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
  <groupId>io.appium</groupId>
  <artifactId>java-client</artifactId>
  <version>7.2.0</version>
</dependency>
vhmi4jdf

vhmi4jdf3#

我找到了解决这个问题的方法。MobileElement沿着PageFactory抛出了这个异常。我搜索了很多论坛,没有确切的解决方案可以解决这个问题。我将MobileElement更改为WebElement,它现在可以工作了。

bwleehnv

bwleehnv4#

像这样设置你的安卓驱动程序'private AndroidDriver< WebElement >driver = null;'并将java-client降级到7.4.1。您的问题将得到解决。

cx6n0qe3

cx6n0qe35#

我使用的是java 17,appium java客户端8.3.0
运行单个testng测试,然后添加“--add-opens java.base/java.lang=ALL-UNNAMED”vm参数(运行配置)

1.通过pom.xml

运行

相关问题