2014-07-16 29 views
0

我对自动化,Android,硒,appium和xpath也很陌生。我知道这很吸引人。用于XPath的Android层次结构与特定项目进行交互

我为Android设备编写测试,但我必须测试的应用程序有很多costum视图。我发现与这些自定义项目交互的最佳方式是在视图中放置“android:contentDescription”字段。我唯一的问题是如何获得具有指定contentDescription的元素的访问权限?这是az android的具体问题,我甚至不确定content-desc是我正在寻找的字段。

我有Android的UI动画浏览器提供的层次结构:

http://i.imgur.com/NUGc56o.png

我已经试过的方法:

  • 的XPath:// * [包含(@android:contentDescription, 'example text')]
  • 我能够通过查找它们作为ImageView来访问,但正如我所提到的,我需要使用自定义视图

我的代码看起来像somtihng这样的:

driver.findElementByXPath("//*[constains(@content-desc,'Login')]").click();

感谢您的帮助!

回答

1

您也可以尝试使用辅助功能标签或UIAutomator定位器策略。 Here's Appium's documentation on those.

您的xpath不正确。它应该是:"//android.widget.ImageView[@content-desc='Login']"

下面是你应该做的一些伪代码:

login_image = driver.findElementByXPath("//android.widget.ImageView[@content-desc='Login']"); // Gets you the WebElement 

print login_image.getClass(); // Just for debugging, make sure it's not nil/null 

login_image.click(); // Click on it! 
+0

感谢您的回答,但是这不是我的事:(我敢肯定我XPath是正确的它是你的只是一个普通版本。我的问题是我不知道android如何在这种情况下工作,它是否甚至使用这个(我猜)字段(content-desc或其他),或者我应该选择其他方式来访问元素 – stsatlantis

+1

编辑添加其他定位器策略 – sheeptest

+0

感谢您的编辑帮助了很多! 如果其他人可能需要此操作,请将其留在此处: 这是关键文档:https://code.google.com/p/seleni微米/源/浏览/ spec-draft.md?回购=移动&R = 1b90597cfde5f2deab58c506d4867fa1d165709a – stsatlantis