17

I am doing automation of Android native app and want to verify login validation with text. When I tried to pick it using Appium inspector and Uiautomator. Both unable to locate validation section so hard to pick it.

enter image description here

I tried code from different forums and Appium discussion but still unable to find that validation via automation. I've tried the following:

Code 1

JavascriptExecutor js = (JavascriptExecutor)driver;

WebElement field = driver.findElement(By.xpath("//android.widget.EditText[@text='Enter mobile number']"));
// Boolean is_valid = (Boolean)js.executeScript("return arguments[0].checkValidity();", field);

try {
    String message = (String) js.executeScript("return arguments[0].validationMessage;", field);
} catch (Exception E) {

Output :

Exception : org.openqa.selenium.WebDriverException: Method has not yet been implemented

Code 2

public boolean IsValidationFired() {
    if(driver.getPageSource().contains("Invalid")) {
        System.out.println("PASS");
    } else {
        System.out.println("FAIL");
    }
}

Output :

Print = FAIL

I am not sure if it is javascript popup or native app popup or toast message.But looking to inspect it and verify via automation.

Update

Code used to fire validation is : contactnu.setError("Invalid phone number.");

Naman
  • 27,789
  • 26
  • 218
  • 353
Helping Hands
  • 5,292
  • 9
  • 60
  • 127
  • If you are looking for the text, it might be a toast message: https://developer.android.com/guide/topics/ui/notifiers/toasts.html . Appium has known issues supporting Toast messages, see this thread for a big discussion on this: https://discuss.appium.io/t/verifying-toast/3676/16 – George Pantazes Aug 31 '17 at 16:27
  • Did you try switchin context at the time this pop-up is displayed to figure our if UI automator/chrome inspect detects it somehow? – Naman Sep 21 '17 at 17:53
  • 1
    @nullpointer - Yes tried that as well. – Helping Hands Sep 25 '17 at 10:49
  • Is the content not visible still in either of the ways? – Naman Sep 25 '17 at 10:54
  • @nullpointer -No, Actually it returns me child window/popup size zero. – Helping Hands Sep 25 '17 at 10:57

2 Answers2

2

I have been doing Android App testing automation from last 2 years. I have encountered the same issue many times. Unfortunately, uiAutomator does not detect these kind of elements so we can't perform any actions on such elements.

Appium internally sends command to uiAutomator and uiAutomatar performs the actions on the device. uiAutomator can perform actions if uiAutomator sees those elements.

So I suggest you to don't spend much time on using java script or xPath because I have already tried hose solutions but none of them worked.

However, There is one solution which is very lengthy. You can take screen shot of this screen from the device and store it into your machine at run time and check whether that text "Invalid phone number." is available in the image using SIKULI. OR else you can ask developer to change the error message to some visible component in the uiAutomator. Hope this helps you.

Vinod
  • 956
  • 7
  • 15
  • Thank you @Vinod - You saved my time. I already spent lot of time to get solution for this but now I wont waste time :) – Helping Hands Sep 02 '17 at 17:10
0

Why don't try to search with regular expression such as:

public boolean IsValidationFired()
{           
       try{
           WebElement invalid = driver.findElement(By.xpath("//*[@text='Invalid phone number.']"));
           //You could check if @text contains just invalid Invalid
           //WebElement invalid = driver.findElement(By.xpath("//*[contains(@text, 'Invalid')]]"));
           if(invalid != null)
           {
               System.out.println("PASS");
           }
           else
           {
               System.out.println("FAIL");
           }
       }catch(NoSuchElementException ex){
           System.out.println("Element not found");
       }
}

Anyway, this is not a really good solution (* at xpath)... Just use it to test. Searching will be slow... If you find the element you will have to make a better XPATH solution...

Note: After you find your element, to find out if is a TextView, Toast, ... I suggest to do this:

System.out.println(invalid.getAttribute("className"));
//This will print something like: "android.widget.TextView"
barbudito
  • 548
  • 1
  • 6
  • 16
  • I am not getting that particular text even If I do **driver.getpagesource()**. Still I will try your answer and update you. – Helping Hands Sep 01 '17 at 10:30
  • This is not working. Getting exception : **org.openqa.selenium.NoSuchElementException:** – Helping Hands Sep 01 '17 at 11:16
  • Is it possible that "Invalid phone number." is displayed as an Image? – barbudito Sep 01 '17 at 11:35
  • If you can't access to that element, you will have take a screenshot (you can crop it to have just the region you want) and with a mask compare it to check if you are getting error message or not... For example: OpenCV – barbudito Sep 01 '17 at 11:56
  • No. Actually that display using android code : **contactnu.setError("Invalid phone number.");** – Helping Hands Sep 01 '17 at 12:32
  • Can you export the screen from UI Automator Viewer to a UIX file (is a XML you can open with Notepad...) Then from Notepad search for that string? – barbudito Sep 01 '17 at 12:40
  • Then... As @Vinod said you can't do anything... Sometimes UIAutomator does not detect some elements... – barbudito Sep 15 '17 at 10:42