首页 > 解决方案 > NativeScript-Dev-Appium : UIElement.text() / UIElement.getAttribute("value") 返回自动化文本而不是显示的文本

问题描述

我正在使用 NativeScript-Dev-Appium@5.0.0 并希望检查标签上显示的文本。我将automationText属性分配给并使用Driver.findElementByAccessibilityId(automationTextValue)来获取元素。找到了该元素,但是当我尝试对其使用 UIElement.text() / UIElement.getAttribute("value") 时,它会返回automationText 属性的值,而不是实际显示的文本。有没有其他方法可以做到,因为我找不到。

标签: appiumnativescript

解决方案


不幸的是,这是 NativeScript 本身的限制,因为当使用automationText 属性时,它将设置元素的所有属性,如值、名称、标签等。我建议您将相同的文本设置为automationText 属性,然后您可以访问或使用以下方法测试元素:

  1. 使用 XPath:
const el = await driver.findElementByXPath("//*[@name=\"My automation text\"]");
  1. 仅适用于安卓
const el = await driver.findElementByText("My automation text");
const text =  await el.getAttribute("text");
  1. 使用 imageVerification 类型,例如:
await driver.compareRectangle
await driver.compareElement
await driver.compareScreen 

这非常方便,但这里的缺点是这在某些时候需要更高的维护。


推荐阅读