Reputation: 5396
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.
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.");
Upvotes: 17
Views: 1612
Reputation: 976
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.
Upvotes: 2
Reputation: 578
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"
Upvotes: 0