Appium+python automation (10) - element positioning cheats to help you get through the two veins of Ren Du - Volume 1 (super detailed explanation)

1. Explanation of common positioning methods

  Object positioning is a very critical step in automated testing, and it can also be said to be the most critical step. After all, if your object is not positioned, you can't do it if you want to. Therefore, Brother Hong hopes that the knowledge in this chapter will be more hands-on and practical, not just the knowledge in books. After all, I can only illustrate this with examples. Let's take a look at some of the positioning methods we commonly use.

1.1 ID positioning

  Whether it is in web automation or app automation, the id is unique. Some friends may have doubts when they see this, because some information says that it is unique through name positioning. Why do you have an id here? In fact, this is in There was no conflict before, but if you are using a newer version of appium, it will not work. In the new version, the name positioning is removed, so there will be no name positioning in the future positioning. Usually we prefer Use id to locate. Friends who are just learning here may have questions, sometimes why your app does not have an id, or why it is on this phone but not on other phones. 1. Development is not added. 2. The android version is below 4.4.

Let's just look at the picture below

 

The id of the object circled in red on the left side of the above picture can be seen in the properties on the right. Its id is also circled in red. If we need to enter the input box "Please enter the Taobao account" Information, we only need to operate the id on the right, let's look directly at the code below.

1 driver.find_element_by_id("com.taobao.taobao:id/aliuser_login_account_et").send_keys("潇潇说测试")

Through the above code, we can directly enter the account information in the account information input box Beijing Xiaoxiao said test. It may be a little confusing for those who have no foundation. Where does this driver come from? The driver has been initialized when we configure it to start. We only need to call its method find_element_by_id. If your ide has an auto-completion function, then you will find a problem when you enter the following method, why is there a find_elements_by_id? This will be explained later, and interested friends can think about it.

1.2 className positioning

In actual work, className positioning will be used relatively less. When you often look at the class, you will find that many classNames are the same, and you have no way to uniquely locate them. Let’s look at the following two pictures

We can take a closer look at the className in the two input boxes of Taobao account and password in these two pictures. If you use the method below to locate in this case, you will find that you will never be able to locate the password column. ,Why is this? Because when designing, if there are multiple elements on the page that you are looking for, the system will automatically select the first one for you, so you will never be able to operate the latter, so how to solve such problems in actual work or actual combat? What about this kind of problem? Will explain later.

driver.find_element_by_class_name("android.widget.EditText").send_keys("潇潇说测试")

1.3 xpath positioning

XPath positioning is the most common and most effective in web automation. Using xpath positioning avoids the problem of not finding elements and causing errors, but using xpath positioning in app is a very low thing. Why do you say that? Because in the experience of people who have been there, as long as they use xpath to locate elements, their reaction will be slower. The purpose of automation is to improve efficiency, but using xpath will reduce efficiency, so it is very low here. But many times we have to understand, let's briefly explain it below. First of all, we need to familiarize ourselves with the xpath positioning of the web.

1) Before talking about the xpath of the web, please install the fireFox browser first, and then install the fireBug and FirePath plug-ins in the browser. As shown below:

These two plug-ins are essential when automating or learning xpath. Here we will talk about xpath directly. Let's look at the picture below to understand

Circle the input box with a dotted line in red and let's take a look at the positioning given to us by xFirePath. The xpath displayed in the positioning is ".//*[@id='kw']", what does this mean? Let's explain step by step. 1. //* Select all elements in the document. 2. @id='kw'] matches the node whose attribute is id and the value is kw. Some friends here may not understand it very well, saying that it is enough to directly use the id for positioning here. In fact, it is, but what about when there is no such attribute? Let's look at the picture below

  If the name positioning is invalid, when you see this picture, how can you locate it if you don’t use xpath? Feeling a little crazy. Friends can try to use xpath to locate by themselves. Some people may find that the positioning in xpath is not very clear. Why? .//*[@id='u1']/a[4] In this xpath, we don't have as clear thinking as before. He has more hierarchical relationships, which we will talk about later. In this xpath, the first step 1, @id='u1' is the same as before, matching the node whose id value is ul, and then positioning under it. The second step 2, /a[4] means from Select the fourth a element under the root node. Is this step-by-step analysis easier to understand? Let's take a look at some grammars that are often used in xpath positioning, and let's practice a lot.

This is what we often use, and it is the most basic knowledge. Only these can't meet many weird needs, so there are more difficult ones. Let's look at the list below

The above knowledge is all in http://www.w3school.com.cn/xpath/xpath_examples.asp, you can read more and practice more.

Let's look directly at the use of xpath in the app

In the above two pictures, we can clearly see that their id and className are the same. In this case, we can only use xpath to locate without hierarchical positioning. First, you can think about how to do it according to the previous web learning. position. Let's look directly at the code

1 driver.find_element_by_xpath("//android.widget.TextView[@text='聚划算']").click()

In xpath, our syntax is "//android.widget.TextView[@text='JavaScript']", which is the same as our previous web xpath, which means to find all nodes in which the node is android.widget.TextView (here The className is used, and the id can also be used, and the system will search for it in turn) and its text attribute value is JavaScript. Is this easier to understand? Come down and practice more. This positioning method is not recommended, and the efficiency is very slow.

2. Hierarchical positioning

2.1 What is hierarchical positioning

  We have mentioned hierarchical positioning in the previous chapters, but we just don’t know how to do it. In many automations, if only relying on simple positioning, there is no way to complete the automation. Just like xpath positioning, some elements have the same id, name, and className, and xpath positioning is inefficient. At this time, most of us will Use hierarchical targeting.

2.2 How to use hierarchical positioning in the project

Let's take a simple example to understand hierarchical positioning.

From the above picture, we can see that there are many android.widget.FrameLayout under the node whose id is com.taobao.taobao:id/rv_main_container

From the picture below, we can see that the node with the id of android.widget.FrameLayout contains a lot of android.widget.LinearLayout

From this picture, we can easily see that if we want to locate this element, we cannot locate it. In this case, most of us use hierarchical positioning and xpath. Here we will see how to use hierarchical positioning.

First of all, we can see the difference in the structure of the three pictures. The elements of the third picture are in the second picture, and the elements of the second picture are in the first picture. Here we call the first The node whose image id (com.taobao.taobao:id/rv_main_container) is android.widget.FrameLayout is the parent node of the second image element, and the second image id (android.widget.FrameLayout) is android.widget.FrameLayout The node of the third picture element is the parent node of the third picture element, the first picture id (com.taobao.taobao:id/rv_main_container) is the node of android.widget.LinearLayout the third picture element's grandparent node; we only need to first Locate the grandparent node by id, and then locate it sequentially from the grandparent node to the bottom. Now you can practice it and see the same result as mine? Look at the code:

1 element= driver.find_element_by_id("com.taobao.taobao:id/rv_main_container")
2 element1 = element.driver.find_elements_by_class_name("android.widget.FrameLayout")
3 element2 = element1[1].find_element_by_class_name("android.widget.LinearLayout")
4 element2.click()

According to our thinking, our code will be the above result, but if you run it, you will find that no error is reported, but you will not click. Why is this? Let's look at the picture below (pro-test, it will click on the first Tmall, it may be the first one by default)

The className of all child nodes under the grandparent node is "android.widget.FrameLayout", and the className of all child nodes under the parent node is "android.widget.LinearLayout". In this case, how to click What about the operation? So in this case, a new positioning problem will be triggered, which is the List positioning that Brother Hong will talk about next.

2.3 Reference code

 1 # coding=utf-8
 2 # 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行
 3 
 4 # 2.注释:包括记录创建时间,创建人,项目名称。
 5 '''
 6 Created on 2019-7-01
 7 @author: 潇潇说测试
 8 Project:学习和使用定位元素
 9 '''
10 # 3.导入模块
11 from appium import webdriver
12 import time
13 desired_caps = {}
14 desired_caps['platformName'] = 'Android'   #android的apk还是IOS的ipa
15 desired_caps['platformVersion'] = '8.0'  #android系统的版本号
16 desired_caps['deviceName'] = '127.0.0.1:62001'    #手机设备名称,通过adb devices  查看
17 desired_caps['appPackage'] = 'com.taobao.taobao'  #apk的包名
18 desired_caps['appActivity'] = 'com.taobao.tao.welcome.Welcome'  #apk的launcherActivity
19 #desired_caps['unicodeKeyboard'] = True   #使用unicodeKeyboard的编码方式来发送字符串
20 #desired_caps['resetKeyboard'] = True   #将键盘给隐藏起来
21 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) #启动服务器地址,后面跟的是手机信息
22 # 休眠五秒等待页面加载完成
23 time.sleep(5)
24 
25 element= driver.find_element_by_id("com.taobao.taobao:id/rv_main_container")
26 element1 = element.find_elements_by_class_name("android.widget.FrameLayout")
27 element2 = element1[1].find_element_by_class_name("android.widget.LinearLayout")
28 element2.click()
29 
30 # driver.quit()

summary

1. I introduced the Firefox browser earlier. Here, Brother Hong will summarize how Google Chrome uses xpath to locate. Of course, masters and masters can ignore it, because you can directly write xpath to locate. In fact, looking at their grammar is also very simple. You can also try handwriting in the later stage to see the difference from tools, which can help you improve and improve.

2. Google browser xpath plug-in download address https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl

3. For the installation method, use the Google browser to directly access the above address, and then add it.

 4. Take a look at how to use and its effect

(1) Google Chrome, F12, if not, you can open "Developer Tools"

(2) Click the "arrow" on the left to find the element to be located

(3) Select the element of the console, right mouse button, in the copy, select "Copy XPath"

(4) Content of copy: (//*[@id="kw"])

5. Pay attention to the difference between find_element and find_elements! ! !


              [The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled]


1. From entry to mastery of Python programming

2. Interface automation project actual combat

3. Actual Combat of Web Automation Project


4. Actual Combat of App Automation Project

5. Resume of first-tier manufacturers


6. Test and develop DevOps system

7. Commonly used automated testing tools


Eight, JMeter performance test

9. Summary (little surprise at the end)

life is long so add oil. Every effort will not be let down, as long as you persevere, there will be rewards in the end. Cherish your time and pursue your dreams. Don't forget the original intention, forge ahead. Your future is in your hands!

Life is short, time is precious, we cannot predict what will happen in the future, but we can grasp the present moment. Cherish every day and work hard to make yourself stronger and better. Firm belief, persistent pursuit, success will eventually belong to you!

Only by constantly challenging yourself can you constantly surpass yourself. Persist in pursuing your dreams and move forward bravely, and you will find that the process of struggle is so beautiful and worthwhile. Believe in yourself, you can do it!

Information acquisition method:
This document and video information should be the most comprehensive and complete preparation warehouse for friends who want to engage in [software testing]. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. Hope Can help you too! All of the above can be shared, click the small card below to enter the group to receive.

Guess you like

Origin blog.csdn.net/NHB456789/article/details/131765036