Thursday, 16 February 2017

Appium - Finding WebElements



Hello Guys,
In my last blog, I discussed about the launching of any of the pre-installed App through Appium. I hope you didn’t face any difficulty in that. In this blog, I will take you a step closer towards Appium. Okay ! So let’s try to understand how appium actually works. In my recent blog, we had seen the launching of Calculator App automatically and we had performed some actions as well. What do you think, how did we able to press the keys ?? or clicked the “=” button for the result ? Well, the answer of this question lies in the title of this blog. Yes, you got it Right ! It’s the “Web Elements”. Ohh !! great, What’s the "web-element" by the way ? Well, WebElements are used for the identification of Web Based elements using WebDriver. By saying web-based elements, I mean the buttons, editText boxes, checkboxes, radio buttons etc. with which we interact often.

If you use “uiautomatorviewer” you can easily recognize your web elements. I have not used anything else for this purpose as of now. 

 
In the above screenshot you can see that the numeric 9 is selected and on the right pannel every details regarding that key is been reflected such as index = 2, text = 9, etc. But unfortunetely, life is not that easy everytime. In some situations, you might not be able to get these data. For e.g., like in the above screenshot we couldn’t get the “content-desc” details. Similarly we might not get the details related to the feilds like...index, text, resource-id, etc. What to do in such situations then ? Well, we can use the combinations. Like (class_name + index), (class_name + text), (class_name + content-desc) or (class_name + bounds).

We can locate web elements in may ways, one of the way is by using “xpath”. I personally use xpath more like anything in case of lacking of the resource-id’s. If you are getting the resource-id, there is no need to do extra things. Simply use findElements(By.id(“”)).
for e.g.,
WebElement back_button;
back_button.findElement(By.id("resource-id of the back Button"));
In case, you are not able to find the resource-id but the class name and index. You can still locate the element by using “xpath[@index]

driver.findElement(By.xpath("element’s Class Name[@index='index value']"));
for e.g.,
WebElement search_editText;
search_editText = driver.findElement(By.xpath("//android.widget.EditText[@index='1']"));

In case, you want to access your elements using Bounds,
WebElement back_button = driver.findElement(By.xpath ("//className[@bounds='[10,58][110,126]']"));
[ use your element’s bound value. ]

I personally don’t recommend to locate elements via bound value, as it might vary upon the size of mobiles.




No comments:

Post a Comment