在自动化方面做了很多努力后,无法找到Android本机应用程序的验证

Hel*_*nds 17 java validation android selenium-webdriver appium

我正在进行Android原生应用程序的自动化,并希望通过文本验证登录验证.当我尝试使用Appium检查器和Uiautomator选择它时.两者都无法找到验证部分,因此很难选择它.

在此输入图像描述

我尝试了来自不同论坛和Appium讨论的代码,但仍然无法通过自动化找到验证.我尝试过以下方法:

代码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) {
Run Code Online (Sandbox Code Playgroud)

输出:

例外:org.openqa.selenium.WebDriverException:方法尚未实现

代码2

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

输出:

打印=失败

我不确定它是javascript弹出窗口还是本机应用程序弹出窗口或Toast message.But要检查它并通过自动化验证.

更新

用于启动验证的代码是: contactnu.setError("Invalid phone number.");

bar*_*ito 0

为什么不尝试使用正则表达式进行搜索,例如:

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");
       }
}
Run Code Online (Sandbox Code Playgroud)

无论如何,这不是一个真正好的解决方案(* at xpath)...只需使用它来测试即可。搜索会很慢...如果找到该元素,您将不得不制定更好的 XPATH 解决方案...

注意:找到元素后,要查明是否是 TextView、Toast...,我建议这样做:

System.out.println(invalid.getAttribute("className"));
//This will print something like: "android.widget.TextView"
Run Code Online (Sandbox Code Playgroud)