Thursday 27 May 2021

Python Appium - How to handle list of Elements

 How to handle list of Elements

There are situations where all the elements on a particular page have same attribute however we have to extract a unique one and click on it.

So let us take a simple example for this SMS a native app on device


Here I want to click on settings So I need to find an unique locator for the same. So let us see how we can capture the it

For this open new session in appium and click on screenshot


So if you see all the elements have same attribute, its a list which have common attribute and here we have to find a unique one

ist_of_values = driver.find_elements_by_xpath(
"//android.widget.TextView[@resource-id='com.google.android.apps.messaging:id/title']")
expected_list =['Archived','Blocked contacts','Messages for web','Settings','Help&feedback']
actual_list = []

print(len(list_of_values))
for values in list_of_values:
ele = values.get_attribute("text")
actual_list.append(ele)

print(actual_list)

So we can identify elements by xpath. Once the element is identified wee can iterate through list of values. Before that we can get the length of list of values. Later append the text in actual_list[].

Let us see what is the output for this.


So from output we can see that length of list is 5 and the actual list is displayed. So let us get back to the point that we want to click on settings now. How to click on that

#Tap on Settings options
driver.find_element_by_xpath("//android.widget.TextView[@text='Settings']").click()

So you need to pass text= Settings

Complete code as below:

import pytest
from appium import webdriver
from pageObject.message.message_app import Message
from utilities.BaseClass import BaseClass
from time import sleep


def __init__(self, driver):
self.driver = driver


def test_send_message_delivery_on():
desired_caps = {
'platformName': 'android',
'udid': 'emulator-5554',
'deviceName': 'Pixel 2 API 28',
'platformVersion': '9.0',
'appPackage': 'com.google.android.apps.messaging',
'appActivity': 'com.google.android.apps.messaging.ui.ConversationListActivity',
'new/commandTimeout': 600
}
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
driver.find_element_by_xpath("//android.widget.ImageView"
"[@content-desc='More options']").click()
sleep(7)
list_of_values = driver.find_elements_by_xpath(
"//android.widget.TextView[@resource-id='com.google.android.apps.messaging:id/title']")
expected_list = ['Archived','Blocked contacts',
'Messages for web','Settings','Help & feedback']
actual_list = []
print("\n ")
print(len(list_of_values))
for values in list_of_values:
ele = values.get_attribute("text")
actual_list.append(ele)

print(actual_list)

assert expected_list == actual_list, "List did not matched..."

#Tap on Settings options
driver.find_element_by_xpath("//android.widget.TextView[@text='Settings']").click()

Here Attaching with video of our scenario.




2 comments:

  1. It was really an enjoyable piece of content! You are very smart at integrating skills into this. Freelancing is a wonderful opportunity. I got a perfect platform to turn my Best coder in India dream into a reality which is Eiliana.com. They are equipped with experts who have the true potential to provide solutions to aspiring freelancers.

    ReplyDelete

Feature Posts

Python Appium - Step by step procedure for capturing screenshot

 Why To capture screenshot? It is as important as your logs. If there is any failure for any test scenario, we can provide screenshot for th...