0

我已经使用 Appium 和 Selenium 在 Java 中创建了一个页面对象,该对象当前适用于 Android 应用程序,如下所示:

public class MattVerifyPage extends PageObject{

private AppiumDriver driver = FrameworkInitialize.driver;

By verifyTitle = By.xpath("/hierarchy/android.widget.TextView");

public void verifyTitle(String expectedTitle){

String actualTitle = driver.findElement(verifyTitle).getText();

但是,我需要它来运行 Android 应用程序和 iOS 应用程序,这两个应用程序的 xpath 选择器是不同的。我想我需要做这样的事情:

@AndroidFindBy(xpath = “androidxpath”)
@iOSFindBy(xpath = “iOSxpath”)
public MobileElement verifyTitle ;

这意味着无论我使用的是 Android 还是 iOS,我仍然只使用一个名为“verifyTitle”的变量。

但是,当我这样做时, driver.findElement 行 (String actualTitle = driver.findElement(verifyTitle).getText() 显示以下错误:

findElement
(org.openqa.selenium.By)
in DefaultGenericMobileDriver cannot be applied
to
(io.appium.java_client.MobileElement)

我想我正在将 AppiumElements 与 SeleniumElements 进行比较,但我不确定如何解决它。

任何帮助将不胜感激。

谢谢

马特

4

2 回答 2

2

是的,在您的原始示例中大量混合了对象类型。使用 @OSFindBy 注释,您走在了正确的轨道上。一旦你定义了这些元素,你就已经有了元素,所以不需要再次找到它。您只需要以下内容:

verifyTitle.getText()

有关页面对象模型(POM)的更多信息,请参阅此博客文章。

概括:

import all the good stuff including PageFactory;

public class YourPage {
  private WebDriver driver;

  public YourPage(AppiumDriver<MobileElement> driver) {
    this.driver = driver;
    PageFactory.initElements(new AppiumFieldDecorator(driver), this);
  }

  @AndroidFindBy(id = "android_button")
  @iOSFindBy(id = "ios_button")
  private MobileElement that_button;

  public void pushTheButton() {
    that_button.click()
  }
}

注意:上面的代码未经测试/写在我的头上/我不是以编写 Java 为生的。容易出错,但应该给你的想法。

于 2019-02-04T22:51:55.280 回答
0

这与我一起工作,我的项目 Selenium,TestNG 和 Appium 使用 PageFactory.initElements

public class Login extends Setup {

    @Test
    public void loginAlert() throws InterruptedException {
        

    Button button = new Button(driver);
            PageFactory.initElements(driver, button);
            Input input = new Input(driver);
            PageFactory.initElements(driver, input);
        
        
        Input input1 = new Input(driver);
        System.out.println("Test Alert Login");
        button.ById("navigation_more");
        button.ById("btnLogin");
        input.ById("et_email_or_phone_number", "dikakoko.com");
        input1.ById("tet_password", "dikakoko");
        
    }
}

下面是我上面调用的函数。

public class Input {
    AppiumDriver<MobileElement> driver;
    Root root = new Root();

    public Input(AppiumDriver<MobileElement> driver) {
        this.driver = driver;
    }

    public void ById(String selector, String textValue) {
        MobileElement element = driver.findElement(By.id(root.element() + ":id/" + selector));
        waitForVisible(driver, element);
        Actions actions = new Actions(driver);
        actions.moveToElement(element);
        actions.click();
        actions.sendKeys(textValue);
        actions.build().perform();
        System.out.println("Input: " + textValue);
    }
    
    private void waitForVisible(AppiumDriver<MobileElement> driver, MobileElement element) {
        try {
            Thread.sleep(5000);
            System.out.println("Waiting for element visibility");
            WebDriverWait wait = new WebDriverWait(driver, 20);
            wait.until(ExpectedConditions.visibilityOf(element));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

和这个

public class Button {

    AppiumDriver<MobileElement> driver;
    Root root = new Root();

    public Button(AppiumDriver<MobileElement> driver) {
        this.driver = driver;
    }

    public void ById(String selector) {
        MobileElement element = driver.findElement(By.id(root.element() + ":id/" + selector));
        Actions actions = new Actions(driver);
        actions.moveToElement(element);
        actions.click();
        actions.build().perform();
        System.out.println("Button is Clicked!");
    }
}

我用这个

Button button = new Button(driver);
PageFactory.initElements(driver, button);
Input input = new Input(driver);
PageFactory.initElements(driver, input);

我的参考资料:来自 www.seleniumeasy.com

于 2020-07-06T14:16:45.653 回答