App automated testing | dom structure and element positioning

First look at a few terms and explanations:

  • dom: Document Object Model Document Object Model
  • dom application: the earliest application to the interaction between html and js. The structured description of the interface, the common format is html, xml. The core elements are nodes and attributes
  • xpath: xml path language, used for node positioning in xml, XPath can traverse elements and attributes in xml documents. Let's
    look at the dom of an App as follows:

The basic knowledge of controls is the same as that of selenium. appium abstracts a control model for the mobile terminal, called dom structure; it understands all controls as xml files, and in xml files, each control has its own type and attribute;

Now that we have types and attributes, we can naturally locate elements based on these, and because the entire model is xml, we can also locate the information of each control through xpath, does it seem familiar? The related element positioning method was also introduced during the web-side automation. For details, you can review the first article at the end of the article and click to view it.

  • position
  • interact
  • Affirmation

Through uiautomatorviewer's analysis of Snowball App's homepage, the following results are obtained:

By parsing the results, we can see that the attributes and types of elements are:

  • node
  • attribute
  • clickable
  • content-desc
  • resource-id
  • text
  • bounds

IOS and Android are slightly different in terms of control properties (here is a summary first, and a separate article on IOS will be explained later, welcome to pay attention):

  • dom attributes are similar to node structures
  • The names and properties are named differently

Appium supports a subset of WebDriver's location strategies:

2.21 Lookup by "class" (eg, type of UI component) - generally not recommended

这种就是通过判断控件类型来查找,例如TextView、ImageView等


In actual work, this positioning method is hardly used, because there may be many TextViews, ImageViews, etc. in a page;

appiumdriver.findElementByClassName("android.widget.TextView");

As mentioned above, xpath is not only able to locate elements on the mobile side, but also one of the most commonly used positioning methods. In web-side automation, we will first recommend CSS positioning, while in mobile terminal positioning, we will first recommend xpath positioning. A good xpath positioning syntax will bring accuracy and convenience to our positioning, and the impact on speed will also be within our acceptable range

In the following dom structure, there are multiple controls of the same type on an interface, and these controls have the same id or attribute, which is not unique, so the positioning operation of the specified control cannot be directly performed. At this time, it is time for xpath to show its talents

For example, if we want to locate the second RelativeLayout followed by "drawing a closed circle", the specific writing method is as follows:

//下面两种写法均可实现
By.xpath("((//*[@text='画好一个封闭的圆'])[2]/following-sibling::android.widget.RelativeLayout)[2]")
By.xpath("((//*[@text='画好一个封闭的圆'])[2]/following-sibling::*[@class='android.widget.RelativeLayout'])[2]")

Many controls have text attributes, but appium does not support direct positioning of text, and in actual work, we often use text for positioning, which is due to xpath. By encapsulating xpath syntax, we can customize a method for locating elements based on text:

public By ByText(String text){
        return By.xpath("//*[@text='"+ text + "']");
    }

appiumdriver.findElement(ByText("关注"));

In addition, when it is necessary to locate the Toast bullet box, there is and can only be achieved through xpath: sometimes
we will pop up a message prompt after we perform an operation, for example, after clicking a button or pulling down to refresh, a prompt similar to "Refresh successfully" may appear, and then disappear after a few seconds;

The pop-up message is likely to be the Toast that comes with the Android system. When the Toast pops up, the node android.widget.Toast will appear on the current interface, and disappear with the disappearance of the message; at this time, if we need to locate the pop-up message and test it, we can use the positioning xpath method.

System.out.println( appiumdriver.findElementByXPath("//*[@class='android.widget.Toast']").getText());

result:

For more xpath introduction, please refer to the blog:
Pushing open the door of Web automation to reach the "criminal phenomenon" - detecting selenium architecture, environment installation and common element positioning methods
or W3C:
https://www.w3school.com.cn/xpath/xpath_syntax.asp

Students who have studied web automation know that elements in HTML have their own ids. On the mobile side, elements still have their own id values, but the name is resource-id, as follows:

Note: We see that the value of id is very long. In fact, you only need to take the part after the slash/ in actual use, as follows:

By.id("statusTitle")

There is a special positioning method in mobile automation, which is based on accessibilityId, which is expressed in the dom as the value of the attribute content-desc. If a value is written in content-desc in Android, it can be located through it:


It's awkward here. . . Because research and development are often lazy and don’t write, I haven’t been able to find an example after searching for a long time. Everyone knows how to use it.
Another thing to note is that if you want to write in the form of "By.xxx", you need to use MobileBy

MobileBy.AccessibilityId("AccessibilityId");
appiumdriver.findElementByAccessibilityId("AccessibilityId");

Sometimes we need to perform certain operations on the interface to find the elements we want, such as sliding the list to search, etc. At this time, we can use the android uiautomator. Here, we use the API Demo in the emulator to demonstrate, enter Views in APIDemo, and then swipe the screen to find "Popup Menu" to
click

You can use Android's UIAutomator to perform sliding screen operations. At this time, you need to use AndroidDriver. In addition, you can use UiScrollable for positioning elements:


There are examples written in ruby ​​in the uiautomator UiSelector on the official website, but the positioning method is the same, so you can directly refer to the java code

driver.findElementByXPath("//*[@text='Views']").click();
((AndroidDriver<MobileElement>)driver).
                findElementByAndroidUIAutomator
                 ("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"Popup Menu\").instance(0))")
                 .click();

In actual operation, AndroidUIAutomator occasionally fails to locate, and there may be a little deviation in the position of the positioned element. Here is a slight modification to avoid this occasional failure;

    By departmentName = MobileBy.AndroidUIAutomator(
                "new UiScrollable(new UiSelector().scrollable(true).instance(0))." +
                        "scrollIntoView(new UiSelector().text(\""+ departName +"\").instance(0))");
        find(departmentName);
//        click(departmentName); 原来直接操作滑动查找的元素结果
        click(ByText(departName));//现在利用xpath重新定位确认后再操作,成功率大大提升

Running effect demo:

In a previous article, we introduced the various engines used at the bottom of appium. You can review the first article at the end of the article and click to view it.
Take a brief look at the picture below:

The latest version we are using now supports uiautomator2 first. If you are using a relatively earlier version, you may support uiautomator. So what impact do these two engines have on the positioning described above? Look at the source code:

The latest version we are using now supports uiautomator2 first. If you are using a relatively earlier version, you may support uiautomator. So what impact do these two engines have on the positioning described above? Look at the source code:

  • Uiautomator source code

Taking id positioning as an example, it can be seen from the source code of Uiautomator that its id positioning is more extensive. When we use By.id, it will match resourceId, accessibility id, and id at the same time

  • Uiautomator2 source code

In Uiautomator2, the positioning of id is subdivided, and the corresponding id is judged before operation. Therefore, when using Uiautomator2, our writing method should be more rigorous.

technical strength

Finally, I share my summary of software testing learning materials and routes, including testing theory, Linux basics, MySQL basics, Web testing, interface testing, App testing, Python basics, Selenium related, performance testing, LordRunner related, etc. [Click on the small card at the end of the article to get it for free]

Resource acquisition method:

 

Guess you like

Origin blog.csdn.net/HUA1211/article/details/131667140