如何解决无法设置io.appium.java_client.android.AndroidElement错误

时间:2019-07-15 05:54:01

标签: java android appium page-factory

我正在尝试使用Page Factory从基本测试和测试运行器中分离元素,但是当我运行它时会返回错误。有人可以在我的代码中帮助我吗?

我已经尝试在Google上搜索此修复程序,但没有任何效果

公共类BaseTest {

private final String URL_STRING = "http://127.0.0.1:7622/wd/hub";
protected URL url;
public AndroidDriver<AndroidElement> driver;

private final static String APP_PACKAGE_NAME = "com.android.chrome";
private final static String APP_ACTIVITY_NAME = "com.google.android.apps.chrome.Main";

public AndroidDriver<AndroidElement> init() throws MalformedURLException {
    url = new URL(URL_STRING);
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability(AndroidMobileCapabilityType.PLATFORM_NAME, Platform.ANDROID);
    caps.setCapability(MobileCapabilityType.UDID, "emulator-5554");
    caps.setCapability(MobileCapabilityType.NO_RESET, true);
    caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, APP_PACKAGE_NAME);
    caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, APP_ACTIVITY_NAME);
    caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Samsung");


    driver = new AndroidDriver<AndroidElement>(url, caps);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


    return driver;
}

}

公共类HomePage扩展了BaseTest {

@AndroidFindBy(id = "com.android.chrome:id/search_box_text")
public AndroidElement searchTxt;

public HomePage() {
    PageFactory.initElements(new AppiumFieldDecorator(this.driver), this);
}

公共类SampleGoogle扩展了BaseTest {

@Test
public void Sample() throws MalformedURLException {

    HomePage homePage       = new HomePage();

    homePage.init();
    homePage.setSearch("Test");

预期结果应该是将在我的模拟器上打开Chrome,然后将Test(测试)写入搜索栏中,但是运行代码时会返回错误。

  

失败:样本java.lang.IllegalArgumentException:无法设置   io.appium.java_client.android.AndroidElement字段   com.google.HomePage.searchTxt至   org.openqa.selenium.remote.RemoteWebElement $$ EnhancerByCGLIB $$ d27c0df4     在   sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(未知   来源)   sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(未知   源)位于sun.reflect.UnsafeObjectFieldAccessorImpl.set(未知   源代码),位于java.lang.reflect.Field.set(未知源),位于   org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:117)     在   org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:105)     在com.google.HomePage。(HomePage.java:21)处   com.testcases.SampleGoogle.Sample(SampleGoogle.java:17)位于   sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)位于   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于   java.lang.reflect.Method.invoke(来源未知)   org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)     在org.testng.internal.Invoker.invokeMethod(Invoker.java:661)处   org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)在   org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)在   org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)     在   org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)     在org.testng.TestRunner.privateRun(TestRunner.java:744)处   org.testng.TestRunner.run(TestRunner.java:602)在   org.testng.SuiteRunner.runTest(SuiteRunner.java:380)在   org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)在   org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)在   org.testng.SuiteRunner.run(SuiteRunner.java:289)在   org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)在   org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)在   org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)在   org.testng.TestNG.runSuitesLocally(TestNG.java:1226)在   org.testng.TestNG.runSuites(TestNG.java:1144)在   org.testng.TestNG.run(TestNG.java:1115)在   org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)     在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)     在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题, 我的错误:我在appium URL(所需功能)的wd中使用了大写字母“ W”。将其更改为小“ w”(wd),现在可以正常使用