在自动化的许多努力后,无法找到验证的Android本机应用程序

问题描述:

我正在做自动化的Android本机应用程序,并希望验证与文本的登录验证。当我尝试使用Appium检查员和Uiautomator进行挑选时。无法找到验证部分,所以很难挑选它。在自动化的许多努力后,无法找到验证的Android本机应用程序

enter image description here

我试图从不同的论坛和讨论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) { 

输出:

异常:org.openqa.selenium.WebDriverException:方法尚未 得到落实

代码2

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

输出:

打印=失败

我不知道这是否是JavaScript弹出或本机应用程序弹出或吐司message.But寻找检查它并通过自动化进行验证。

更新

代码用于火灾验证:contactnu.setError("Invalid phone number.");

+0

如果您正在寻找文本,它可能是一个敬酒信息:https://developer.android.com/guide/topics/ui/notifiers/toasts.html。 Appium已知支持Toast消息的问题,请参阅此主题以进行关于此问题的大讨论:https://discuss.appium.io/t/verifying-toast/3676/16 –

+0

您是否在此弹出窗口时尝试使用switchin上下文如果UI自动装置/镀铬检查以某种方式检测到它,则显示该图标以表示我们的情况? – nullpointer

+1

@nullpointer - 是的,也尝试过。 –

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

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"); 
     } 
} 

无论如何,这是不是一个真的很好的解决方案(*在xpath)...只是用它来测试。搜索会很慢...如果你发现元素,你将不得不做出更好的XPATH解决方案...

注意:找到你的元素后,要找出是否是TextView,Toast,... I建议这样做:

System.out.println(invalid.getAttribute("className")); 
//This will print something like: "android.widget.TextView" 
+0

即使我做了** driver.getpagesource()**,我也没有得到那个特定的文本。不过,我会尝试你的答案并更新你。 –

+0

这不起作用。获得例外:** org.openqa.selenium.NoSuchElementException:** –

+0

有可能是“无效的电话号码”。显示为图像? – barbudito

我一直在做Android应用程序测试自动化从过去的2年。我多次遇到同样的问题。不幸的是,uiAutomator没有检测到这些元素,所以我们无法对这些元素执行任何操作。

Appium在内部向uiAutomator发送命令,uiAutomatar在设备上执行操作。如果uiAutomator看到这些元素,则uiAutomator可以执行操作。

因此,我建议你不要花太多时间使用java脚本或xPath,因为我已经尝试了软管解决方案,但都没有工作。

但是,有一种解决方案非常冗长。您可以从设备中截取该屏幕的截图,并在运行时将其存储到您的机器中,并检查文本是否为“无效的电话号码”。在使用SIKULI的图像中可用。或者,您可以要求开发人员将错误消息更改为uiAutomator中的某个可见组件。希望这可以帮助你。

+0

谢谢@Vinod - 你节省了我的时间。我已经花了很多时间为这个解决方案,但现在我不会浪费时间:) –