James
James

Reputation: 129

How to section a element with selenium & Appium that does not have a unique id, class, package, or resource id?

I'm trying to detect if I get the expect response from a text message. so I need to access the text view of the received text. the problem I'm having is I cant figure out a way to let the driver know I need the received text rather than the sent text.

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)

all the attributes values match expect for the text value. how can i let appium know which one i need to check? i was thinking something that accessed a element within another element, code id assume would be something like this:

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

Any help would be greatly appreciated.

Upvotes: 1

Views: 749

Answers (2)

barbudito
barbudito

Reputation: 578

What you can do to detect if message is received (left) or sent (right) is to compare with their bounds...

For my example I will use these two bounds:

Received message

Sent message

In Xpath with my example could be:

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

If you have an element at [163,100][100,100] everything will be ok. But... The problem with this method is that if you have an element at [100,100][163,100] it will detect it as received when is not... So the next method could be better because there is no option...

Another way to get just left messages could be:

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

In the other hand this will return any textview that NOT contains "][942," so there can not be mistakes

Let me know if worked. I do it this way to know it.

Upvotes: 0

Shiv
Shiv

Reputation: 505

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

Upvotes: 1

Related Questions