我应该使用哪种方法(最快),选择元素? [英] Which method should I use (fastest) to select element?

查看:982
本文介绍了我应该使用哪种方法(最快),选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么有很多不同的方法来选择驱动程序之间的元素。我想知道哪一个是最快和最适合本机应用程序(iOS和Android)。

Well there are a lot of different methods to select elements between Drivers. I would like to know which one is the fastest and the most suitable for native apps (iOS and Android) .

随着Appium Driver类有:

With the Appium Driver class there is :

findElementByAccessibilityId(String using)

随着移动类有:

findElement(org.openqa.selenium.By by) //with ById/Xpath/Name/ClassName...

使用Android和iOS的驱动程序类有:

With Android and iOS driver class there are :

findElementByAndroidUIAutomator(String using)
findElementByIosUIAutomation(String using)

和使用RemoteWebDriver类有:

And using the RemoteWebDriver class there are :

findElementById();
findElementByXPath();
findElementById(); //css, className etc... -> WebElement which can be cast in mobileElement

所以我猜使用 UIAutomator UIAutomation 更快,但 selendroid 为的Andr​​oid 2.3 +

你怎么做,为什么?你能提供我的 findElementByAndroidUIAutomator(字符串使用)的一些例子的 findElementByIosUIAutomation(字符串使用)

How do you do and why? Can you provide me some examples for findElementByAndroidUIAutomator(String using) and findElementByIosUIAutomation(String using)

我看到有一些问题与XPath选择。从我的使用 findElement(By.name)观点似乎很简单。

I saw there are some issues with XPath selectors. From my point of view using findElement(By.name) seems quite simple.

推荐答案

好了,我们终于做到了这样面面俱到,优化了一下:

Well, we finally did it that way to cover everything and to optimize a bit :

public AndroidDriver getDriver() {
    return driver;
}
/**
 * Return an element by locator *
 */
public static MobileElement element(By locator) {
    return driver.findElement(locator);
}

/**
 * Return element by its Id, use it for tests which can FAIL on purpose (selector unknown)
 */
public static MobileElement findById(String value){
    return (MobileElement) getDriver().findElementByAndroidUIAutomator("new UiSelector().resourceId(\"" + value + "\")");
}
/**
 * Return element by its Name, use it for tests which can FAIL on purpose (selector unknown)
 */
public static MobileElement findByName(String value){
    return (MobileElement) getDriver().findElementByAndroidUIAutomator("new UiSelector().text(\"" + value + "\")");
}
/**
 * Return a MobileElement whatever is the selector : text, name, id, className ... using first AndroidUiAutomator
 * Use it for tests which must PASS
 */
public static MobileElement selector(String value) {
    System.out.println("\n" + "1) try ByAndroidUIAutomator : fieldText -> " + value);
    try {
        return (MobileElement) getDriver().findElementByAndroidUIAutomator("new UiSelector().text(\"" + value + "\")"); // <-> ByIosUIAutomation
    } catch (Exception e0) {
        System.out.println("2) then ByAndroidUIAutomator with Id -> " + value);
        try{
            return (MobileElement) getDriver().findElementByAndroidUIAutomator("new UiSelector().resourceId(\"" + value + "\")");
        } catch (Exception e0_2) {
            System.out.println("3) try By.id -> " + value);
            try {
                return element(By.id(value));
            } catch (Exception e1) {
                System.out.println("4) try By.name -> " + value);
                try {
                    return element(By.name(value));
                } catch (Exception e2) {
                    System.out.println("5) try By.className -> " + value);
                    try {
                        return element(By.className(value));
                    } catch (Exception e3) {
                        System.out.println("6) try By.xpath -> " + value);
                        try {
                            return element(By.xpath("//*[@content-desc=\"" + value + "\" or @resource-id=\"" + value +
                                    "\" or @text=\"" + value + "\"] | //*[contains(translate(@content-desc,\"" + value +
                                    "\",\"" + value + "\"), \"" + value + "\") or contains(translate(@text,\"" + value +
                                    "\",\"" + value + "\"), \"" + value + "\") or @resource-id=\"" + value + "\"]"));
                        } catch (Exception e4) {
                            System.out.println("[WARNING] Selector not present");
                        }
                    }
                }
            }
        }
    }
    return null;
}

/**
 * Return a MobileElement whatever is the selector, specifying UiSelector
 */
public static MobileElement selector(String value, String uiselector) {
    System.out.println("try ByAndroidUIAutomator using uiselector -> " + uiselector + ", " + value);
    try {
        return (MobileElement) getDriver().findElementByAndroidUIAutomator("new UiSelector()." + uiselector + "(\"" + value + "\")");
    } catch (Exception e0) {
        System.out.println("try By.id");
        try {
            return element(By.id(value));
        } catch (Exception e1) {
            System.out.println("try By.name");
            try {
                return element(By.name(value));
            } catch (Exception e2) {
                System.out.println("try By.className");
                try {
                    return element(By.className(value));
                } catch (Exception e3) {
                    System.out.println("try By.xpath");
                    try {
                        return element(By.xpath("//*[@content-desc=\"" + value + "\" or @resource-id=\"" + value +
                                "\" or @text=\"" + value + "\"] | //*[contains(translate(@content-desc,\"" + value +
                                "\",\"" + value + "\"), \"" + value + "\") or contains(translate(@text,\"" + value +
                                "\",\"" + value + "\"), \"" + value + "\") or @resource-id=\"" + value + "\"]"));
                    } catch (Exception e4) {
                        System.out.println("Selector not present");
                    }
                }
            }
        }
    }
    return null;
}

编辑:好了,只需使用UiAutomator,UiAutomation时,你可以(在默认情况下客户端库做到这一点 - 因为ID /名称等...-,除了XPath的是慢,所以在使用它的时候你没有选择。

Edit : Well , just use UiAutomator, UiAutomation when you can (by default client-libs do it -for Id/Name etc...-, except for XPath which is slower, so use it when you haven't the choice.

我再也不会使用选择()函数,它的速度太慢时,未找到的元素,而不是我用一个函数,该函数在参数字符串选择器和一个自定义枚举为每种类型(ID,XPath的,甚至自定义类型像针对Android的 parentIdChildEditText 的-still使用UiAutomator-,等...)

I don't use anymore the selector() function, it's too slow when element isn't found, instead I use a function which takes in parameter the string selector and a custom enum for each type (id, xpath, even custom type like for Android parentIdChildEditText-still using UiAutomator-, etc...)

请参阅 https://github.com/appium/java-client/issues/158

这篇关于我应该使用哪种方法(最快),选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆