Appium element Positioning

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/today520/article/details/37815087

1, about No name, no ID of the element positioning---general article
Problem Solving Ideas:
Because there is no name,id, there is not much left to choose from, either XPath or classname. XPath Wood has a good impression (stability is not high, plus 1.0x need to write the full path, quite collapsed), I decided very personal color recommendations using classname.
Specific methods:
Use classname to get all elements of that type;
Determines the index of an element based on the position of the element;
Gets the target element.
Example:
list<webelement> lis = Driver.findelementsbyclassname ("Android.widget.ImageView");//Get all elements of ImageView
Webeelement Targetele = lis.get (0);//Gets the first element in a list

Advanced:
You can define a classname constant for yourself, and then encapsulate a method that provides you with:
Public list<webelement> getallimges () {
list<webelement> lis = Driver.findelementsbyclassname (Androidclassname.imageview);
return LIS;
}
When you use it, you just need to call this method.

The benefits of this approach: take-all approach, the basic can solve the problem of positioning, can solve the problems of multi-language (natural language);
Disadvantage: Because the system needs to get the list, for the time being, the running efficiency is not very high, it is directly can be seen with the naked eye to the speed of running ...

2, about the location of the element without name, no ID---Special article
because it is special, you need to further analyze the page elements to find out the features. These features are mainly Classname,index, and clickable properties.
Scenario 1: There are 10 ImageView objects on the same page, and our target element has an index of 4, while the ImageView and index 4 pages have only 2. Then we can combine the query conditions according to these 2 conditions to improve efficiency.
Example:
list<webelement> lis = Driver.findelementsbyandroiduiautomator ("New Uiselector (). ClassName (" + " Android.widget.ImageView "+"). Index (4) ");
Based on this scenario, you can encapsulate a method:
Public list<webelement> getelementsbyclassandindex (String classname,int index) {
list<webelement> Lis =null;
lis = Driver.findelementsbyandroiduiautomator ("New Uiselector (). ClassName (" +classname+ "). Index (" +index+ ").
return lis;
}

Scenario 2: There are 10 ImageView objects on the same page, where Index 4 has 5, and we find that our target element is clickable. Then the review page found that only 2 of these conditions were met. So...
Example:
list<webelement> lis = Driver.findelementsbyandroiduiautomator ("New Uiselector (). ClassName (" + " Android.widget.ImageView "+"). Index (4). Clickable (true) ");
You can also encapsulate a method:
Public list<webelement> getelementsbyclassandindexandclickable (String classname,int index) {
list<webelement> Lis =null;
Lis = Driver.findelementsbyandroiduiautomator ("New Uiselector (). ClassName (" +classname+ "). Index (" +index+ "). Clickable (true) ");
return LIS;
}

Scenario 3: When analyzing page elements, the page is relatively simple, and only the target element's index is 4.
Example:
Driver.findelementbyandroiduiautomator ("New Uiselector (). Index (" +index+ ")");
Also get a method
Public webelement getelementbyindex (int index) {
Return Driver.findelementbyandroiduiautomator ("New Uiselector (). Index (" +index+ ")");
}

3, about the input box has the default text, and when you select, will not empty the empty processing.
in a similar situation, using element.clear () is ineffective. What about that? No hurry, Appium provides a lot of operation, straight hit No, we on the combo fist.
Example:
public void Clear (Webelement el) {
El.click ();///Check input box
Driver.sendkeyevent (123);//move cursor to last
String txt = el.gettext (); Gets the string length
for (int i=0;i<txt.length (); i++) {
Driver.sendkeyevent (67);//delete Each ...
}
}

4, about the Uiautomator unrecognized elements
Overall idea: Don't forget that our phone is clickable.
Get relative coordinates, click, it's that simple. (no absolute coordinates are used to ensure that the program operates at different resolutions)
The following is a discussion of the relative coordinates of the acquisition: Find a page, the page has the relevant elements of the location similar to the target location, or according to these elements can get the approximate position of the target.
The previous section of code to help understand:
Public list<point> Getfilemngpoint () {
list<point> list = new arraylist<point> ();
Webelement Scrol = Driver.findelementbyclassname (Androidclassname.horizontalscroll);
List<webelement> IMGs = scrol.findelements (By.classname (Androidclassname.imageview));
The above only gets elements of a similar position
Point E0 = Getcenterpoint (imgs.get (0));
Point e1 = Getcenterpoint (Imgs.get (1), Imgs.get (2));
Point e2 = Getcenterpoint (Imgs.get (3));
Point e4 = Getcenterpoint (Imgs.get (2), Imgs.get (3));
Point e5 = Getcenterpoint (getelementsbyclassandindexandclickable (androidclassname.imageview, 0). Get (4));
The above gets the center coordinate of the element, the related method is written by oneself
List.add (E0);
List.add (E1);
List.add (E2);
List.add (E4);
List.add (E5);

return list;
}

Appium element Positioning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.