我最近更新了我的 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;
}`
Test项目中的Setup类:
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();
}
将AndroidDriver更改为AppiumDriver并将AndroidElement替换为MobileElement或WebElement。
public HomePage(AppiumDriver<MobileElement> driver) {
try {
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
} catch (Exception e) {
e.printStackTrace();
}
}
尝试:
我遇到了同样的问题,这是因为 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>
我找到了解决此问题的方法。 MobileElement 与 PageFactory 一起抛出此异常。我搜索了很多讨论论坛,没有确切的解决方案可以解决这个问题。我将 MobileElement 更改为 WebElement,它现在可以正常工作了。
像这样设置你的 android 驱动程序 'private AndroidDriver < WebElement > driver = null;' 并将 java-client 降级到 7.4.1。 您的问题将得到解决。