【问题标题】:How to creath xpath using appium uiautomatorviewer in android?如何在android中使用appium uiautomatorviewer创建xpath?
【发布时间】:2015-12-21 05:19:03
【问题描述】:

如何使用 UI 自动查看器创建 XPath。目前我正在测试移动网络应用程序。在这里我需要识别对象属性,我尝试使用由 firebug 和相关 XPath 识别的属性,但它无法锻炼。 你能指导我如何在android中使用appium UI automator viewer创建XPath吗?

我在 Windows 机器上使用 android。我想在移动网络浏览器中获取对象的 ID、名称和 XPath。

以下设置:

public void Appium() throws MalformedURLException{

            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("device", "Android");
            capabilities.setCapability(CapabilityType.BROWSER_NAME, "Browser");
            capabilities.setCapability(CapabilityType.VERSION, "5.1.1");
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("deviceName","097f2163010ad15f");

            capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");

            capabilities.setCapability("app", "chrome");
           driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

【问题讨论】:

  • 用鼠标点击复选框,你会得到那个ID,使用那个ID,要找到Xpath,你需要在MAC上设置appium inspector和appium
  • 我有 appium 检查器,但我在 windows 机器上使用 android。我可以在 windows 机器上使用 appium 检查器创建 xpath 吗?

标签: android mobile xpath automation appium


【解决方案1】:

在 Appium 框架中,XPath 语法与 @Gaurav 提到的 Web 中使用的语法有点不同。

如果您要打印 XML 页面源代码,您会更容易理解其结构。这是一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<hierarchy rotation="0">
    <android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1776]" resource-id="" instance="0">
        <android.widget.LinearLayout index="0" text="" class="android.widget.LinearLayout" package="com.test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1776]" resource-id="" instance="0">
            <android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][1080,1776]" resource-id="" instance="1">
                <android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][1080,1776]" resource-id="com.test:id/action_bar_root" instance="2">
                    <android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][1080,1776]" resource-id="android:id/content" instance="3">
                        <android.widget.LinearLayout index="0" text="" class="android.widget.LinearLayout" package="com.test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][1080,1776]" resource-id="" instance="1"/>
                    </android.widget.FrameLayout>
                </android.widget.FrameLayout>
            </android.widget.FrameLayout>
        </android.widget.LinearLayout>
    </android.widget.FrameLayout>
</hierarchy>

因此,在 您的 情况下,要查找元素,代码如下: driver.findElement(MobileBy.xpath("//android.widget.CheckBox[@index='1']"))

@Gaurav 的例子在逻辑上也是正确的,但星星不应该是这样的: driver.findElementsByXPath("//[@class='android.widget.CheckBox' and @index='1']");

【讨论】:

  • No.It didn't work.Getting following error: FAILED: testCal org.openqa.selenium.NoSuchElementException: no such element (Session info: chrome=45.0.2454.94) (Driver info: chromedriver= 2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3),platform=Windows NT 6.1 SP1 x86_64) (警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:10.12 秒
  • 当然,因为我看到您使用的是 Chrome 驱动程序。您必须使用 AndroidDriver。 :) 你能提供你的驱动程序设置吗?
  • 谢谢。更新了我在代码中的设置。请看看我原来的问题。
  • 如果您正在测试应用程序,为什么要使用 Web 驱动程序?对我来说没有意义。使用 AndroidDriver!参数“app”采用.apk 文件的路径,而不仅仅是名称。更多信息:code.google.com/p/selenium/wiki/DesiredCapabilities
  • 更新了屏幕截图。实际上我们的应用程序可用于本机和网络。所以我认为两者都是相同的 xpath。很抱歉造成混淆。添加了我的网络应用屏幕截图,因为我正在尝试识别“否”选项。感谢您的支持。
【解决方案2】:

如果你想在上面的窗口中创建 xpath,它会是这样的:

driver.findElementsByXPath("//*[@content-desc='No' and @index='16']");

只需根据需要不断更改参数即可。

【讨论】:

  • 没有 Gaurav。它没有解决。仍然面临这个问题。我在 appium(chrome 浏览器)中使用 android web 应用程序。以下是我的错误。失败:testCal org.openqa.selenium.NoSuchElementException:没有这样的元素(会话信息:chrome=45.0.2454.94)(驱动程序信息:chromedriver=2.15.322448(52179c1b310fec1797c81ea9a20326839860b7d3),平台=Windows NT 6.1 SP1 x86_64(WAR)没有提供任何堆栈跟踪信息)命令持续时间或超时:10.05 秒有关此错误的文档,请访问:seleniumhq.org/exceptions/no_such_element.h
猜你喜欢
  • 2016-04-23
  • 2021-03-02
  • 2014-10-01
  • 2015-05-08
  • 2014-10-27
  • 2022-07-21
  • 2016-10-08
  • 1970-01-01
  • 2016-01-11
相关资源
最近更新 更多