Equivalent to button('text') in java

Hi,

I’m migrating from ruby client to java client and there are lots of intuitive stuff in ruby that I’m struggling in java.

To start from something simple, in ruby I can make:

button('button_text')
buttons('button_text')

In java I can make:

By.name("button_text");

But how can I make sure its a button without having two queries or xpath?

By.name(“button_text”); // this simply means get any element with attribute name value as “button_text”

If you preassume “button_text” is unique text present only on button then above will work

else you can use
HTML uses [button type]="button"Click Me [/button] so we use xpath = //input[@type=button]
Android native uses class “android.widget.Button” to create buttons so we can use this class name.

Using UISelectorClass as
return (MobileElement) driver.findElementByAndroidUIAutomator(“new UiSelector().” + className + “(”" + value + “”)" + “.enabled(true).instance(1)”);

MobileElement Button = driver.findElements(By.className(“android.widget.Button”)).get(1);
ButtonElement.getText();

Thanks for reply.

I’m using

((AndroidDriver) driver).findElementByAndroidUIAutomator("new UiSelector().className(\"android.widget.Button\").text(\"" + value + "\")")

Now my problem is that dont know why, but its not respecting the wait. I have:

WebDriverWait wait60 = new WebDriverWait(driver, 60);
wait60.until(ExpectedConditions.visibilityOf(((AndroidDriver) driver).findElementByAndroidUIAutomator("new UiSelector().className(\"android.widget.Button\").text(\"" + value + "\")")));

When I normally did:

return wait30.until(ExpectedConditions.presenceOfElementLocated(By.name(value)));

problem is that findElementByAndroidUIAutomator it seems I always have to use driver again and that may be causing wait not to be respected.

Any help?

Not sure this is my observation

Now AndroidDriver is a generic class, while creating it’s handle “driver” we need to specify what type of element it returns …

like
AndroidDriver [WebElement] driverWebElement;
AndroidDriver [MobileElement] driverMobileElement;

This is functional definition of “visibilityOf” function , it expects WebElement, but you might be passing MobileElement driver.

ExpectedCondition.visibilityOf(WebElement element)

Even with the cast, wait is still not respected.

Anyone ever used findElementByAndroidUIAutomator with WebDriverWait ?

I was able to make this work. Will post solution because it may be useful to others.

I changed to FluentWait (WebDriverWait extends FluentWait anyway).

Final code (so far):

public Wait<WebDriver> wait60_1 = new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
wait60_1.until(webDriver -> ((AndroidDriver) driver).findElementByAndroidUIAutomator(value2)); 

without using the lambda (easier to read)

wait60_1.until(new Function<WebDriver, WebElement>() {
            @Override
            public WebElement apply(WebDriver webDriver) {
                return ((AndroidDriver) driver).findElementByAndroidUIAutomator(value2);
            }
        });

Thanks amitjaincoer191 for support