【问题标题】:I want to know a generic method for scrolling and clicking on each element of the menu我想知道一种用于滚动和单击菜单每个元素的通用方法
【发布时间】:2019-03-14 15:12:46
【问题描述】:

我想滚动浏览此应用程序和其他相关应用程序的菜单,也想单击它们。所以请告诉一个可以在 Appium Java 中的所有应用程序上运行的方法

到目前为止,这是我点击所有应用程序的菜单按钮但不滚动或点击的代码:

package project;
import java.util.Scanner;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.PressesKeyCode;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidKeyCode;

public class firstpro {
    static AppiumDriver<MobileElement> driver = null;

    public static void main(String[] args) {

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("deviceName", "Samsung Galaxy");
    caps.setCapability("udid", "HFTUN678");
    caps.setCapability("platformName", "Android");
    caps.setCapability("platformVersion", "7.0");
    caps.setCapability("appPackage", "com.olx.pk");
    caps.setCapability("appActivity", 
    "pl.tablica2.activities.ProxyActivity");


        try {
            driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        } 
        menus();
    }

    public static void menus() {
       WebDriverWait wait = new WebDriverWait(driver, 10000);
       WebElement Element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("android.view.ViewGroup")));
       driver.findElement(By.className("android.widget.ImageButton")).click();
    }
}

【问题讨论】:

  • 不用喊了。请参阅this guide on how to ask questions on stack overflow。你试过什么?
  • 如果您有这种感觉,我深表歉意 :)。我试图获取菜单的长度并滚动并单击它,但没有成功。你能说出一些适用于所有应用程序的代码吗?
  • 还有一件事所有菜单栏都将“主页”作为第一个选项,因此可以运行循环以从主页开始点击,然后再次找到主页然后停止?
  • 请展示您目前拥有的东西,以便我们知道如何处理。
  • public static void menus() { WebDriverWait wait = new WebDriverWait(driver, 10000); WebElement 元素 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("android.view.ViewGroup"))); driver.findElement(By.className("android.widget.ImageButton")).click(); }

标签: java scroll menu click appium


【解决方案1】:

你可以试试这段代码,我不确定它有多准确,因为我仍然不知道完整的节点树结构,但它应该让你对可以采取的一些方法有一个很好的了解:

public static void menus() {
    List<WebElement> scrollViews = driver.findElements(By.className("android.widget.ScrollView"));
    Iterator<WebElement> it = scrollViews.iterator();
    while(it.hasNext()) {
        WebElement scrollView = it.next();
        // make sure this scroll view has the child element "Home"
        List<WebElement> homeElements = scrollView.findElements(By.xpath("*[text() = 'Home' or text() = 'home']"));
        if(homeElements.isEmpty()) {
            // filter out scroll views that don't have a child with the text "Home"
            it.remove();
        } else if (homeElements.size() > 1) {
            // this scroll view has multiple children with the text "Home"
        }
    }

    if(scrollViews.isEmpty()) {
        // TODO error, couldn't find any scroll view containing a child with the text "Home"
    } else if (scrollViews.size() > 1) {
        // TODO error, there are multiple scroll views containing children with the text "Home"
    }

    // the only scroll view that has a child with text "Home"
    // likely the main menu scroll view
    WebElement targetMenuScrollView = scrollViews.get(0);

    // get all the ImageButtons in the menu
    List<WebElement> menuButtons = targetMenuScrollView
            .findElements(By.className("android.widget.ImageButton"));

    // click all the buttons
    for (WebElement button : menuButtons) {
        button.click();
    }
}

【讨论】:

  • 它没有点击菜单中的任何按钮,虽然仍然打开了菜单
  • 我的代码也全部加进去了,请您再看一遍:)
  • 请给我看完整的节点树并指定你想要点击的节点,我相信按钮是android.widget.ImageButton,正如你所说,但是menuButtonContainer得到了正确的节点。您需要确保wait.until(...) 实际上选择了包含您要单击的所有按钮的父节点。您可以运行调试器并设置断点以查看实际选择了哪个节点并查看其子节点。
  • 它不会因应用程序而异,因为我想要一个通用方法,可以滚动浏览所有本机应用程序的菜单并单击
  • 由于它因应用程序而异,您对如何确定菜单节点有什么好的想法吗?菜单的类在应用程序之间可能不一样,所以你不能选择By.className(),也不能保证按钮是android.widget.ImageButtons。您需要查看多个应用程序并在它们之间找到足够的共性,否则无法通用。
猜你喜欢
  • 2019-03-13
  • 2015-05-10
  • 1970-01-01
  • 2021-05-01
  • 2018-08-25
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多