Appium+python automation (11) - element positioning - super detailed explanation in the second volume)

1. List positioning

  As the name suggests, List is a list. There is also a list in python. If you don’t understand what a list is, let’s understand it as an array or a collection for the time being. First of all, a list is a set, so its number becomes uncertain, so we need to use plurals here, so we can’t use find_element_by_id and other positioning methods when we locate, we need to use its plural form find_elements_by_id, all positioning methods need to use plural plus s. Here we follow the previous case to talk about how to use the list to locate the element you want to locate. First look at the picture:

We look at the picture and know that we can easily locate the entire grandparent node through the id. What we need to do next is to locate all the "android.widget.RelativeLayout" parent nodes under this grandparent node. Similarly, we first look at a picture :

Here we need to directly use the method of positioning complex numbers to operate, directly look at the code (the grandfather node is positioned to the parent node):

element= driver.find_element_by_id("com.taobao.taobao:id/rv_main_container")
elements = element.find_elements_by_class_name("android.widget.FrameLayout")

Through the above code, we directly locate all android.widget.FrameLayout child nodes under the com.taobao.taobao:id/rv_main_container parent node, but because there are many same android.widgets under this android.widget.FrameLayout child node. LinearLayout grandchild node.

Here we need to directly use the method of positioning complex numbers to operate, directly look at the code (the parent node is positioned to the grandson node):

1 elements = element.find_elements_by_class_name("android.widget.FrameLayout")
2 elements1 = elements[1].find_elements_by_class_name("android.widget.LinearLayout")

Now we need to how to operate this child node, here are two methods:

1. We talked about List earlier, you can understand it as an array or a collection, and all the child nodes located here finally become a list. If we want to access an element in this list, we can access it like the data in the array Access by subscript. The final code looks like this:

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

The final result of the above code is to select the tab page "Juhuasuan", and then click to enter.

Remarks: If beginners don’t understand how to access through subscripts, let’s say that subscripts start from 0. If you want to access the first element in list i, the result is i[0]. For this part of knowledge, you can look at python Base.

2. If you want to access the elements in the List, can we access them sequentially through the for loop statement? This is often used in automation. Next, you can use this idea to practice it yourself to see if you can achieve the desired effect. Look at my code below:

1 element= driver.find_element_by_id("com.taobao.taobao:id/rv_main_container")
2 elements = element.find_elements_by_class_name("android.widget.FrameLayout")
3 elements1 = elements[1].find_elements_by_class_name("android.widget.LinearLayout")
4 for ele in elements1:
5     ele.click()

Looking at the above code, we loop through each element in the list, because each loop gets one of the elements, then we only need to add the operation you want to this element, so we You can click here directly.

If you do this by hand, you will find a problem. The system will report an error shortly after you enter the first tab. Why? You can also try to solve this problem, we will explain this piece of knowledge later.

2. Embedded H5 positioning

2.1 Thoughts on hybrid positioning

In web automation, we will encounter frame problems. After encountering these embedded tags, what we need to do is to switch windows. Then there is a similar situation in app automation testing, which is the embedded html we often see. In our native app Add a page made of html. You can think about how this situation works.

2.2 Analysis of common positioning problems of hybrid

First, let's take a look at the following picture:

Through the structure diagram on the right, we can clearly see that the entire page is a webview. No matter from what angle we can locate it, we cannot do it well. If we need to operate the elements of the page at this time, we need to switch contexts to complete it. But before talking about this knowledge point, everyone should try to process this page according to the knowledge on the Internet to see if it can be successful. Let me talk about the problems you will encounter first:

1. You may see some articles showing that we can complete positioning without switching contexts. There are such cases, but the author only encountered such cases when logging in to Weibo, QQ and other third-party logins. If it is not the case In the case of the above situation, there is no way to complete it through a similar method, so I hope that readers will do it by themselves when encountering this situation, and see which method is more suitable for their own projects.

2. If you need to switch contexts, you need to obtain all the contexts of the page. At this time, you can obtain the knowledge from the official website or other articles through the following methods, and an error may be reported. This situation does not matter much.

1 webview = self.driver.contexts
2 print webview

If you debug through the above code but report an error, but other information is fine, don’t worry. Here you need to confirm two things: (1) When the app is packaged, you need to enable the debug attribute of the webview setWebContentDebuggingEnabled(true) , let the development add it directly. Generally, it is turned on, after all, they also need to be debugged. (2) You use a lot of mobile phones to debug, and you find that some can and some can’t, but you can use the emulator. According to the official answer, you need to root the mobile phone at this time, and then try again. At present, the author has encountered these two situations, and I also debugged for a long time to find the reason for the second one.

2.3 hybrid positioning explanation

After these two problems are solved, it is easy to locate the webview, just look at the code:

1 webview = driver.contexts
2 driver.switch_to.context(webview[1])
3 driver.find_element_by_link_text('PHP').click()

For beginners, the above code may not be very understandable, let's look at the log below:

Don’t worry about the difference between my code execution and the previous one (an extra self), let’s look at the output of the console below. The output is a list. As I said before, a list is similar to an array. There are two elements in this list “NATIVE_APP ", "WEBVIEW_cn_com_open_mooc", the first element is the contexts of our native app, and the latter is the context of our webview, so when we need to get the context of the webview, we only need to access it through the following table of this list.

After we get the context of the webview, we only need to switch it through driver.switch_to.context(). After switching, we can locate it like we locate the web.

Look at the picture below and we open the h5 page through the browser:

Through the above pictures, we can easily locate like the web, and we can also use some positioning methods of the web. Seeing this, do you feel that a problem has been solved? Go for it.

2.4 Hybrid problem actual combat

Through the previous study, I believe that you already have some practical capabilities. Here is a question for everyone. Are the contexts we get must be two each time? If there are not two, then our script above can't be used? You can think about how to solve it here, and look at our ideas and solutions below.

No matter what page we go to get contexts, no matter how many there are, but is his type definitely a list? Since it is a list, can we get every value in it, and then judge each value, just find our webview? Look at the code below:

1 #获取当前页面所有的contexts
2 webview = driver.contexts
3 #在获取到的contexts list里面去挨个循环
4 for context in webview:
5   #判断循环中单个的context是否是webview,如果是就进行切换,并且跳出循环
6   if 'WEBVIEW' in context:
7     driver.switch_to.context(context)
8     break
9 driver.find_element_by_link_text('PHP').click()

Through the above code, have we perfectly solved the positioning problem of embedded H5? do it

3. Sliding positioning

3.1 Sliding positioning method

In app automation, we often encounter a problem. The element we need to find is not on the currently displayable screen. As for where we don’t know, if we keep using the current page to find at this time, the system will report an error. In order to solve For this problem, we need to use sliding search.

The first idea is that we search for the element on the page where we need to find the object, and judge whether the element is on the current page. If the element is not on the page, then we need to go to the interactive screen, go to our next screen, and then search again. And so on until found.

3.2 Analysis of sliding positioning ideas

We have the method, then we need to know what points should be used to realize this function. Let's analyze it with me:

1. Do we need to know what the element we need to find is? This needs to be determined first

2. We are not sure whether the page we are looking for is above, below, left or right of our current page, so do we need to determine the direction we need to slide?

3. There are elements and directions, but do you know how much we need to slide the screen each time? So do we need to get the size of the screen first, and then calculate a sliding value for different directions?

Everything is ready only owes Dongfeng, let's practice according to this idea.

3.3 Sliding Positioning Actual Combat

1. According to the above ideas, we can first determine the elements we need to find, see the following picture:

We need to find the "Change" button behind the actual combat recommendation, and then click it. First, let's check his location information

Finally, we find the positioning information code of the element as follows:

1 self.driver.find_element_by_id('cn.com.open.mooc:id/tv_replace').click()

  People with a certain foundation may think this is very low, but have you ever thought about a problem, we can execute it through this code, but when there is no such button, an error will be reported, and there is no way to execute it, so what do we need? How to deal with it? So at this time we need to have some python fault-tolerant knowledge, even if our code execution goes wrong, let it continue to execute according to our meaning. try.......except......., this is the fault-tolerant processing in our python, let's look at the added code below:

1 try:
2 self.driver.find_element_by_id('cn.com.open.mooc:id/tv_replace').click()
3 except Exception,e:
4 print e

The meaning of try is to tell the compiler to try to execute the following piece of code. If an error is reported, then you will print out the error message in except.

2. Now that we have elements, what we need to know is how to slide the interface? First, let's take a look at the picture below:

In the process of using the app, there are several sliding situations above. We regard the entire interface as a coordinate system (x, y). If we need to slide up, do we just keep the x-axis stationary and the y-axis from the bottom What about moving up? Downward means the x-axis does not move, but what about the y-axis from top to bottom? In the same way, does sliding left and right mean that the y-axis should not move and the x-axis should slide left and right? You can experience it well and have a picture in your mind.

The method we need to use to slide in appium is the swipe function. As for which direction to slide, we need to look at the values ​​​​of x and y in us. If we need to slide down and up, then we should be:

1 self.driver.swipe(x1,y1,x1,y2,t)

In the above code, the value of the x-axis remains unchanged, and the value of the y-axis changes, so it slides up and down, from y2 to y1. t represents how much time to complete this action, or how long this time lasts.

Remarks: What needs to be noted here is that the x and y values ​​of the screen are taken from the upper left corner, the upper left corner is (0, 0), and the lower right corner is the maximum.

3. The method of sliding above seems to be easy to use, but we can’t fill in a coordinate every time, it’s too low, so we need to get the screen size, just look at the code:

1 x = self.driver.get_window_size()['width']
2 y = self.driver.get_window_size()['height']

  The above code is the x, y axis we got. Through the idea, we have all the codes. What we need to do next is to modify the original code and make a package. Let's look at the code below. It doesn't matter if you don't understand it for the time being. Later, we can understand it after learning the basics of python'. Think first, then understand.

 1 #获取屏幕大小
 2          
 3 def getSize(self):
 4   x = self.driver.get_window_size()['width']
 5   y = self.driver.get_window_size()['height']
 6   return (x,y)
 7          
 8          
 9 #向左滑动
10 def swipeLeft(self,t):
11   l=self.getSize()
12   x1=int(l[0]*0.9)
13   y1=int(l[1]*0.5)
14   x2=int(l[0]*0.1)
15   self.driver.swipe(x1,y1,x2,y1,t)
16                
17 #向右滑动
18 def swipeRight(self,t):
19   l=self.getSize()
20   x1=int(l[0]*0.25)
21   y1=int(l[1]*0.5)
22   x2=int(l[0]*0.75)
23   self.driver.swipe(x1,y1,x2,y1,t)
24                
25 #向上滑动
26 def swipeUp(self,t):
27   l=self.getSize()
28   x1=int(l[0]*0.5)
29   y1=int(l[1]*0.8)
30   y2=int(l[1]*0.4)
31   self.driver.swipe(x1,y1,x1,y2,t)
32   time.sleep(5)
33          
34 #向下滑动
35 def swipeDown(self,t):
36   l=self.getSize()
37   x1=int(l[0]*0.5)
38   y1=int(l[1]*0.25)
39   y2=int(l[1]*0.75)
40   self.driver.swipe(x1,y1,x1,y2,t)
41          
42 #查找元素,没找到滑动
43 def findLocal(self):
44   x = 1
45   while x==1:
46     if self.fact() == 1:
47       self.swipeUp(2000)
48       time.sleep(3)
49       self.fact()
50     else:
51       print "找到了"
52       x=2
53              
54          
55          
56 #递归
57 def fact(self):
58   n =1
59   try:
60     self.driver.find_element_by_id('cn.com.open.mooc:id/tv_replace').click()
61   except Exception,e:
62     return n

  By looking at the entire logic of the above code is 1. First, find the element, and if I find it, I will click it directly. 2. If the element is not found, then I will slide up (here you can choose by yourself), and then search again after sliding. If you find it, click it. If you don’t find it, continue to slide. Do it yourself, the knowledge points here are very important! Although there will be some alternative methods later, ideas and algorithms are very important.

4. Summary

   Well, element positioning, the common ones are roughly these, let’s share this here for now, if you encounter it in the future, I will make it up for my friends! ! !


              [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!

Guess you like

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