我最近更新了我的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();
}
答案 0 :(得分:0)
将 AndroidDriver 更改为 AppiumDriver ,并将 AndroidElement 替换为 MobileElement 或 WebElement 。
public HomePage(AppiumDriver<MobileElement> driver) {
try {
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
} catch (Exception e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
尝试:
我遇到了同样的问题,这是由于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>
答案 2 :(得分:0)
我找到了解决此问题的方法。 MobileElement与PageFactory一起引发此异常。我搜索了很多讨论论坛,但没有确切的解决方案可解决此问题。我将MobileElement更改为WebElement,现在可以使用了。
答案 3 :(得分:0)
像这样设置你的安卓驱动程序'private AndroidDriver < WebElement > driver = null;' 并将 java-client 降级到 7.4.1。 您的问题将得到解决。