尽管可以找到现有的类名,但找不到 AppiumDriver MobileElement

AppiumDriver MobileElement not found despite Existing ClassName can be found

我已经设置了我的 AppiumDriver 自动化 class 并尝试进行简单的测试,例如并行测试、识别 MobileElements 和一般滑动命令是否正常工作。解决问题时,我收到一条错误消息,指出我初始化的 MobileElement 作为网页 Class 的一部分,例如 Chrome 浏览器上的 Google 主页。即使我选择的 ID 下的 className 是正确的,我也没有得到这样的元素。在我的 Appium Server 中,它试图通过 className (android.widget.EditText) 查找,但找不到。我试过 class 和 ".//android.widget.EditText" 但没有成功。

我会使用按名称搜索,但不,它没有给我很多选择。我什至尝试做一个 WebElements 列表,但它的大小不大于零。

这是我的页面对象 classes:

页面对象:

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 Approach):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();
    }

}

我正在执行测试的 class,在我设置 driver/drivers 之后(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。

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

经过更多关于在网页浏览器上使用 Appium 查找元素的研究,如果您要通过其元素测试的网页有桌面版本,(并且没有切换到非 webview 上下文)桌面布局优先于网页的移动版本。

你要考虑的另一半是查看移动网页版本的页面源,以防 AppiumDriver 找不到桌面元素。您可以使用 driver.getPageSource() 在移动版中找到元素设置。现在,我的 GoogleMainPage class 在输入一些文本并选择 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();
    }

}