无法找到现有ClassName的AppiumDriver MobileElement

时间:2018-04-18 20:34:52

标签: java appium pageobjects getelementsbyclassname page-factory

我已经设置了我的AppiumDriver自动化类并尝试进行简单测试,例如并行测试,识别MobileElements和常规滑动命令正在运行。通过扭结,我收到一个错误,指出我将MobileElement初始化为网页类的一部分,例如Chrome浏览器上的Google主页。即使className undereneath我选择的id是正确的,我也没有得到这样的元素。从我的Appium Server,它试图通过className(android.widget.EditText)找到它,但它无法找到它。我已经尝试过那个班级和“.//android.widget.EditText”,但没有运气。

我会按名称使用搜索,但不会给我很多选项。我甚至试图做一个Web元素列表,但它没有大于零的大小。

这是我的页面对象类:

PageObject:

public class PageObject {
    public AppiumDriver<MobileElement> driver;
    protected WebDriverWait wait;
    Dimension size;


    public PageObject(AppiumDriver<MobileElement> driver){
        this.driver = driver;
        wait = new WebDriverWait(driver, 15);

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

    @SuppressWarnings("rawtypes")
    public  void  swipeVertical  (double startPercentage, double finalPercentage, int duration) {
        size = driver.manage().window().getSize();
        int width = (int) (size.width/2);
        int startPoint = (int) (size.getHeight() * startPercentage);
        int endPoint = (int) (size.getHeight() * finalPercentage);
        new TouchAction(driver)
            .press(PointOption.point(width, startPoint))
            .waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
            .moveTo(PointOption.point(width, endPoint))
            .release()
            .perform();
    } 

    @SuppressWarnings("rawtypes")
    public void swipeHorizontal (double startPercentage, double finalPercentage, int duration) {
        size = driver.manage().window().getSize();
        int height = (int) (size.height/2);
        int startPoint = (int) (size.getWidth() * startPercentage);
        int endPoint = (int) (size.getWidth() * finalPercentage);
        new TouchAction(driver)
            .press(PointOption.point(startPoint, height))
            .waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
            .moveTo(PointOption.point(endPoint, height))
            .release()
            .perform();
    }

(更新,这次不使用PageFactory方法):GoogleMainPage:

public class GoogleMainPage extends PageObject {

    //@AndroidFindBy(id="com.android.chrome:id/tsf")
    private MobileElement searchSection = driver.findElement(By.id("main"));

    //@AndroidFindBy(id="com.android.chrome:id/tophf")
    //private MobileElement searchCategories;


    public GoogleMainPage(AppiumDriver<MobileElement> driver) {
        super(driver);


    }

    public void searchQuery(String query) {

        MobileElement searchText = searchSection.findElement(By.className(".//android.widget.EditText"));
        MobileElement confirmSearch = searchSection.findElement(By.name("Google Search"));
        searchText.click();
        searchText.sendKeys(query);
        confirmSearch.click();
    }

}

在我设置驱动程序/驱动程序后,我正在执行测试的类(generalWait()只是一个Thread Sleep方法):

@Test
    public void testActivation() {
        assertTrue(driver != null);

        pageObject = new PageObject(TLDriverFactory.getTLDriver());
        pageObject.driver.get("https://www.google.com");
        System.out.println(pageObject.driver.getCurrentUrl());
        assertTrue(pageObject.driver.getCurrentUrl().equals("https://www.google.com/"));

        try {generalWait(8000);} catch (InterruptedException e) {e.printStackTrace();}

        GoogleMainPage googleMainPage = new GoogleMainPage(TLDriverFactory.getTLDriver());

        googleMainPage.searchQuery("star wars");

        GoogleSearchResultsPage resultsPage = new GoogleSearchResultsPage(TLDriverFactory.getTLDriver());

        resultsPage.swipeVertical(0.20, 0.80, 2000);
    }

我目前正在使用Maven运行它,而io.appium java客户端是6.1.0。

设置时我有什么遗漏?如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

在对通过Web浏览器上的Appium查找元素进行了更多研究之后,如果要通过其元素测试的网页具有桌面版本(并且尚未切换到非Webview上下文),则桌面布局将采用优先于网页的移动版本。

您需要考虑的另一半是查看移动网页版本的页面源,以防AppiumDriver无法找到桌面元素。您可以使用driver.getPageSource()在移动版本中找到元素设置。现在,我的GoogleMainPage类可以在键入一些文本并选择Google搜索按钮时使用

public class GoogleMainPage extends PageObject {

    private MobileElement searchSection = driver.findElement(By.id("tsf"));

    public GoogleMainPage(AppiumDriver<MobileElement> driver) {
        super(driver);

    }

    public void searchQuery(String query) {

        MobileElement searchText = searchSection.findElement(By.name("q"));
        MobileElement confirmSearch = searchSection.findElement(By.className("Tg7LZd")); 
        searchText.click();
        searchText.sendKeys(query);
        confirmSearch.click();
    }

}