首页 > 解决方案 > 我想知道一种用于滚动和单击菜单每个元素的通用方法

问题描述

在此处输入图像描述

我想滚动浏览此应用程序和其他相关应用程序的菜单,也想单击它们。所以请告诉一个可以在 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();
    }
}

标签: javascrollmenuclickappium

解决方案


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

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

推荐阅读