AppiumFieldDecorator 无法与 POM 一起使用

问题描述 投票:0回答:3

我正在尝试使用 Appium、TestNG 和 Maven 实现自动化。我在初始化页面对象模型时遇到错误。附有错误日志。尝试了很多方法,但没有运气。如果有人知道我在哪里做错了,那就太好了。 java-client --> 6.1.0 测试 --> 7.4.0

public final class Driver {
    public static void initDriver() {
        if (Objects.isNull(DriverManager.getDriver())) {
            URL url = null;
            try {
                url = new URL("http://127.0.0.1:4723/wd/hub");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            DesiredCapabilities capability = new DesiredCapabilities();
            capability.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
            capability.setCapability("appPackage", AppConstant.appPackage);
            capability.setCapability("appActivity", AppConstant.appActivity);
            
            AppiumDriver<MobileElement> driver = new AppiumDriver<>(url, capability);
            DriverManager.setDriver(driver);
        }
    }
}
public final class DriverManager {
    private static ThreadLocal<AppiumDriver<MobileElement>> driver = new ThreadLocal<AppiumDriver<MobileElement>>();
    
    public static AppiumDriver<MobileElement> getDriver() {
        return driver.get();
    }
    static void setDriver(AppiumDriver<MobileElement> driverRef) {
        driver.set(driverRef);
    }
}

public class HomePage{
    
    @AndroidFindBy(xpath="//*[@content-desc='Accessibility']")
    private MobileElement eleAccessibility;
    
    public HomePage(){
        PageFactory.initElements(new AppiumFieldDecorator(DriverManager.getDriver(), Duration.ofSeconds(10)), this);}
    
    public void clickAccessibility() {
        eleAccessibility.click();
    }
}


我遇到以下错误......

java.lang.RuntimeException: java.lang.NoSuchMethodException: jdk.proxy2.$Proxy10.proxyClassLookup()
    at io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder.prepareAnnotationMethods(AppiumByBuilder.java:85)
    at io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder.getFilledValue(AppiumByBuilder.java:92)
    at io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder.createBy(AppiumByBuilder.java:148)
    at io.appium.java_client.pagefactory.DefaultElementByBuilder.getBys(DefaultElementByBuilder.java:133)
    at io.appium.java_client.pagefactory.DefaultElementByBuilder.buildMobileNativeBy(DefaultElementByBuilder.java:177)
    at io.appium.java_client.pagefactory.DefaultElementByBuilder.buildBy(DefaultElementByBuilder.java:216)
    at io.appium.java_client.pagefactory.AppiumElementLocatorFactory.createLocator(AppiumElementLocatorFactory.java:66)
    at io.appium.java_client.pagefactory.AppiumElementLocatorFactory.createLocator(AppiumElementLocatorFactory.java:53)
    at io.appium.java_client.pagefactory.AppiumElementLocatorFactory.createLocator(AppiumElementLocatorFactory.java:1)
    at org.openqa.selenium.support.pagefactory.DefaultFieldDecorator.decorate(DefaultFieldDecorator.java:56)
    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 com.pages.HomePage.<init>(HomePage.java:23)
    at com.testCases.TC001_AccessibilityTest.accessibilityTest(TC001_AccessibilityTest.java:14)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:794)
    at org.testng.TestRunner.run(TestRunner.java:596)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
    at org.testng.SuiteRunner.run(SuiteRunner.java:276)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
    at org.testng.TestNG.runSuites(TestNG.java:1063)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.NoSuchMethodException: jdk.proxy2.$Proxy10.proxyClassLookup()
    at java.base/java.lang.Class.getMethod(Class.java:2195)
    at io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder.prepareAnnotationMethods(AppiumByBuilder.java:83)
    ... 42 more

java maven testng appium java-client
3个回答
1
投票

我也面临这个问题,我可以使用以下步骤进行解决

  1. 删除构造函数HomePage()

  2. 现在在类中,如果您正在调用页面对象类,请按以下格式调用它。

    HomePage hommePagePO = PageFactory.intiElement(driver, HomePage.class);
    

例如: 页面对象类:

 public class HomePage{
    @AndroidFindBy(xpath="//*[@content-desc='Accessibility']")
    private MobileElement eleAccessibility;

    public void clickAccessibility() {
    eleAccessibility.click();
    }
    }

测试班:

  @BeforeClass
   public void setup(){
   // initialize your driver and then pass the driver
   
   HomePage homePagePO = PageFactory.initElements(driver, HomePage.class);
   }

或者

当您创建 pageObject 调用构造函数时,请调用 initElement,如下所示:

PageObject 类:

 public class HomePage{

    public Home(Appium driver){
    PageFactory.intiElement(driver, this.getClass());
    }
    @AndroidFindBy(xpath="//*[@content-desc='Accessibility']")
    private MobileElement eleAccessibility;
    Page
    public void clickAccessibility() {
    eleAccessibility.click();
    }
    }

0
投票

这样写

PageFactory.initElements(驱动程序,这个)

并使用 @FindBy 而不是 @AndroidFindBy

设置 1.8 的更改根据您的 comp 中的 JDK 路径 -program files/Java/Jdk1.8.0 将项目文件更改为 11 作为您的 comp 中的 JDK 路径 -program files/Java/Jdk-11.0.17


-1
投票

初始化页面对象模型时面临同样的问题。到目前为止还没有找到结果。

© www.soinside.com 2019 - 2024. All rights reserved.