【问题标题】:AppiumDriver MobileElement not found despite Existing ClassName can be found尽管可以找到现有的 ClassName,但找不到 AppiumDriver MobileElement
【发布时间】:2018-04-18 20:34:52
【问题描述】:

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

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

这是我的页面对象类:

页面对象:

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

}

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

@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 Java 客户端?这个问题似乎与它和 Appium 服务器的版本有关,但我很难找到没有这个错误的组合。使用客户端 6.0.0-BETA5 和服务器 1.7.2(我认为)对我来说工作正常,但我犯了将服务器更新到 1.8.1 的错误。将其放回 1.7.2 并没有为我解决问题。
  • @BillHileman,抱歉回复晚了,我一直在做一个更大的项目,为 appium 自动化制作动态功能。我想在这个项目上解决我的问题,但我得到了类似的结果。使用客户端 6.10 仍然遇到问题。我正在使用 maven 运行我的项目。
  • 最终对我有用的,我无法解释为什么,将我的驱动程序的定义更改为静态。这没有任何意义,因为它在所有工具的多个版本中一直以它的方式(非静态)工作,但是根据另一位发帖人的建议,我将其更改为静态,突然问题就消失了。
  • 该建议的链接是什么?我一直在尝试寻找解决方案,以在使用 java 客户端 6.1.0 时保持驱动程序非静态。到目前为止,对于以前的版本不需要它的解决方案和走这条静态路线的方式并不多。更令我惊讶的是,我找不到答案。
  • 好的,我已经将我的 selenium 版本更新到 3.13.0,(不知道这是否有帮助。)使用 findElement 而不是 PageFactory。现在找到id没有问题的MobileElement,我在find className上有问题。我可以看到 className,它一直说 NoSuchElement Exception。我会更新问题。

标签: java appium pageobjects getelementsbyclassname page-factory


【解决方案1】:

在对在网页浏览器上使用 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();
    }

}

【讨论】:

    猜你喜欢
    • 2017-03-21
    • 2016-05-19
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多