【问题标题】:PageFactory and Appium's IOSElements are not compatible - alternative solution?PageFactory 和 Appium 的 IOSElements 不兼容 - 替代解决方案?
【发布时间】:2016-03-25 08:39:15
【问题描述】:

IOSElements 包含 .setValue() 方法,它的类型比 sendKeys() 快得多。但是,如果我将元素(使用 @FindBy 注释)设置为 IOSElement 而不是 WebElement,PageFactory 将返回错误:

java.lang.IllegalArgumentException:无法将 io.appium.java_client.ios.IOSElement 字段 screen.LoginScreen.signInEmail 设置为 org.openqa.selenium.remote.RemoteWebElement$$EnhancerByCGLIB$$62bef779

此外,我无法将 WebElements 转换为 IOSElements,因为这也会从 JVM 返回错误(无法转换)。

有没有办法使用 PageFactory 设计来初始化 IOSElements?我的示例代码如下:

public class LoginScreen {

private WebDriver driver;

@FindBy(className = "UIATextField")
public IOSElement signInEmail;

@FindBy(className = "UIASecureTextField")
public IOSElement signInPassword;

@FindBy(id = "Log in")
public IOSElement loginButton;

@FindBy(id = "Forgot your password?")
public IOSElement forgotPasswordButton;

public LoginScreen(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}

public SomeOtherObject login(String email, String password) {
    signInEmail.setValue(email);
    signInPassword.setValue(password);
    loginButton.click();
    return new SomeOtherObject(driver);
}

}

【问题讨论】:

    标签: java webdriver appium pageobjects


    【解决方案1】:

    您可以尝试将@iOSFindBy 注释用于WebElement 类型的定位器。 您可以在 Appium github page 上找到一些示例。

    我正在为您考虑的另一件事是尝试投射到 MobileElement 而不是 WebElement

    编辑:

    我现在已经尝试过了,我可以使用 @iOSFindByannotation 和 IOSElement 定位器类型。 我设置了一个简单的类,在我的 Maven pom.xml 中使用以下内容:

    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>2.1.0</version>
    </dependency>
    

    然后,测试类,如下:

    import io.appium.java_client.ios.IOSElement;
    import io.appium.java_client.pagefactory.AppiumFieldDecorator;
    import io.appium.java_client.pagefactory.iOSFindBy;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.support.PageFactory;
    
    public class ATestClass {
        private WebDriver driver;
    
        @iOSFindBy(className = "classname")
        public IOSElement testing;
    
        public ATestClass(WebDriver driver)
        {
            this.driver = driver;
            PageFactory.initElements(new AppiumFieldDecorator(driver), this);
        }
    
        public void ATestMethod()
        {
            testing.click();
        }
    }
    

    注意:MobileElement 定位器类型也是如此,因此,如果您仍然看到任何错误,请检查您所拥有的任何依赖项,因为这些依赖项很可能是导致您的错误的那些。

    【讨论】:

    • 不幸的是,使用 iOSFindBy 仍然给我留下了只能初始化 WebElements 的问题。我无法将它们投射到 MobileElement 或 iOSElement。
    • 用示例代码更新了我的答案。请在一个全新的项目中检查并亲自尝试,以确定您的问题是来自库还是来自原始项目中的其他部分。
    • 考虑使用 AppiumDriver 驱动而不是 Webdriver
    【解决方案2】:

    看起来我的问题是我正在使用 WebDriver 的远程实例,如下所示:

    this.driver = new WebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    

    但是,我可以获得具有相同设置的 IOSDriver 实例:

    this.driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    

    这在传递给 PageFactory.initElements() 时接受 IOSElements; - 没有错误。

    【讨论】:

      猜你喜欢
      • 2011-02-05
      • 1970-01-01
      • 2017-08-27
      • 2017-03-20
      • 2011-04-13
      • 2016-10-24
      • 2016-03-01
      • 1970-01-01
      • 2015-07-07
      相关资源
      最近更新 更多