如何使用selenium& amp;没有唯一ID,类,包或资源ID的Appium?

时间:2018-02-02 22:40:05

标签: javascript android selenium appium appium-android

我试图检测是否从短信中获得了预期的响应。所以我需要访问收到的文本的文本视图。我遇到的问题是我无法找到让司机知道我需要收到的文本而不是发送文本的方法。

this first image of the ui selector shows that the sent text is highlighted here it shows the id, class, package, and resourse-id (which is out of frame)

in the second image of the ui selector shows that the received text is highlighted here it shows the id, class(out of frame), package, and resourse-id (which is out of frame)

所有属性值都与文本值匹配。我怎么能让appium知道我需要检查哪一个? 我正在考虑访问另一个元素中的元素的东西,代码id假设是这样的:

await driver.waitForElementById('com.android.mms:id/msg_list_item_recv', 30000, function (err, data) {})
        .elementById('com.android.mms:id/text_view', function (err, data){})
        .textPresent('key text value', function (err, data) {});

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

If you can ask developers to update id of send text it will be best to do this, as currently there is same id for sent and received text . If this is not possible you can try this .

@FindBy(id = "com.android.mms:id/text_view")
    private List<WebElement> messagelist;

for (int i=0;i<messagelist.size();i++) 
{ 
    if(messagelist.get(i).gettext().contentEquals("sendtext"))
    {
       // then i+1 is your received text
        messagelist.get(i+1).gettext().contentEquals("recievedText")
    }
}

答案 1 :(得分:0)

您可以采取哪些措施来检测是收到消息(左)还是发送(右)是与他们的界限进行比较......

对于我的例子,我将使用这两个界限:

Received message

Sent message

在Xpath中我的例子可能是:

"//android.widget.TextView[@resource-id='com.android.mms:id/msg_list_item_recv' and contains(@bounds, '[163,')]"

如果你在[163,100] [100,100]有一个元素,一切都会好的。但是......这个方法的问题是,如果你有一个[100,100] [163,100]的元素,它会检测到它不是......所以下一个方法可能会更好,因为没有选项......

另一种获取左侧消息的方法可能是:

"//android.widget.TextView[@resource-id='com.android.mms:id/msg_list_item_recv' and not(contains(@bounds, '][942,'))]"

另一方面,这将返回任何不包含“]的文本视图[942,”因此不会有错误

如果有效,请告诉我。我这样做是为了了解它。