Appium+python automation (28) - advanced sliding (super detailed explanation)

advanced skating slide

The sliding operation is generally a sliding between two points. This kind of sliding is called low-level skating sliding here; it is shared with the friends in the previous section. However, the user may need to perform some multi-point continuous sliding operations during actual use. Scenes such as Jiugongge sliding operation, continuous dragging of pictures to move, etc. So how does this advanced and gorgeous skating slide simulate such operations in Appium? Let me tell you slowly.

Touch Action

First look at the official documentation

Address: https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html

Touch Action includes a series of operations, such as press, long press, click, move, and pause. A set of actions can be composed of several different operations. To use TochAction, you need to import the corresponding module first

from appium.webdriver.common.touch_action import TouchAction

press

Method: press() starts to press an element or coordinate point (x, y). Press a certain location on the phone screen with your finger. Press can also receive the coordinates (x, y) of the screen.

press(self, el=None, x=None, y=None)

TouchAction(driver).press(x=0,y=308)

Press

Method: longPress() Start pressing an element or coordinate point (x, y). Compared with the press() method, longPress() has one more input parameter. Since it is long pressed, there must be time to press it. duration is in milliseconds. 1000 means press for one second. Its usage is the same as the press() method.

long_press(self, el=None, x=None, y=None, duration=1000)

click

Method: tap() Performs a click operation on an element or control. Refer to press() for usage.

tap(self, element=None, x=None, y=None, count=1)

move

Method: move_to() Moves the pointer from the previous point to the specified element or point.

move_to(self, el=None, x=None, y=None)

Notice:

Moving to the target position is sometimes an absolute coordinate point, and sometimes it is based on the offset of the previous coordinate point. This should be practiced in conjunction with specific apps.

pause

Method: Wait()

wait(self, ms=0)

Pause script execution in milliseconds.

freed

Method: release() The end of the action cancels the pointer on the screen.

release(self)

implement

Method: The action performed by perform() is the command action sent to the server.

perform(self)

TouchAction actual combat - Jiugongge sliding operation

Jiugongge is a relatively common pattern encryption method. At present, many apps support setting pattern locks, and the Android native system also supports setting Jiugongge pattern lock screens. So how do we use Appium for sliding operations?

testing scenarios

Install and start the Notebook App After starting the App, enable the mobile phone password in the password setting option and slide the nine-square grid to set the following "Z"-shaped graphic password.

test environment

1. My system environment is Windows 10 version 64-bit system

2.dk version: "1.8.0_181"

3. Appium version: 1.4.16

4.selenium:3.141.0

Install selenium:

Enter the command pip install selenium

Verify that the installation was successful: pip show selenium

5. Test device: Android 5.1.1 (not lower than version 5.0)

6.Python:3.7.2

Command: python -V, just press Enter.

7. Test App: Suishouji Android app V9.7.1.5

thoughts and ideas

  • Install and start Suijiji APP
  • Code implementation Click "Next" and slide to the left on the home page to guide the page
  • Click "Start notes" to enter the home page
  • Click the "Settings" button, slide up, find the "Advanced" button, click to enter
  • Click "Gesture Passcode" to start setting gesture passcode (advanced sliding)

Code

(1) Notes on installation and startup

(2) Code implementation Click "Next", slide to the left on the homepage guide page

(3) Click "Start Notes" to enter the home page

(4) Click the "Settings" button, slide up, find the "Advanced" button, and click to enter

(5) Click "Gesture Password" to start setting the gesture password and unlock it (advanced sliding)

This place move_to reaches the pressed coordinate point, because otherwise, the actual result ignores the first pressed point. release() is released, and perform() is executed.

Code running results

Running process appium and night god simulator

Reference Code

 1 # coding=utf-8
 2 # 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行
 3 
 4 # 2.注释:包括记录创建时间,创建人,项目名称。
 5 '''
 6 Created on 2019-8-07
 7 @author: 潇潇说测试
 8 Project:学习和使用appium自动化测试-高级滑动
 9 '''
10 # 3.导入模块
11 from appium import webdriver
12 from appium.webdriver.common.touch_action import TouchAction
13 from selenium.webdriver.support.ui import WebDriverWait
14 from selenium.common.exceptions import NoSuchElementException
15 from time import sleep
16 
17 desired_caps={}
18 desired_caps['platformName']='Android'
19 desired_caps['deviceName']='127.0.0.1:62001'
20 desired_caps['platforVersion']='5.1.1'
21 
22 desired_caps['app']=r'C:\Users\DELL\Downloads\mymoney.apk'
23 desired_caps['appPackage']='com.mymoney'
24 desired_caps['appActivity']='com.mymoney.biz.splash.SplashScreenActivity'
25 
26 driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
27 driver.implicitly_wait(5)
28 
29 
30 def get_size():
31     x=driver.get_window_size()['width']
32     y=driver.get_window_size()['height']
33     return x,y
34 
35 def swipeLeft():
36     l=get_size()
37     x1=int(l[0]*0.9)
38     y1=int(l[1]*0.5)
39     x2=int(l[0]*0.1)
40     driver.swipe(x1,y1,x2,y1,1000)
41 
42 def swipeUp():
43     l = get_size()
44     x1 = int(l[0] * 0.5)
45     y1 = int(l[1] * 0.95)
46     y2 = int(l[1] * 0.35)
47     driver.swipe(x1, y1, x1, y2, 1000)
48 
49 #等待启动页面元素,然后向左滑动两次,跳过引导页面
50 WebDriverWait(driver,6).until(lambda x:x.find_element_by_id('com.mymoney:id/next_btn'))
51 for i in range(2):
52     swipeLeft()
53     sleep(0.5)
54 #点击“开始随手记”按钮
55 driver.find_element_by_id('com.mymoney:id/begin_btn').click()
56 #检测是否有活动页面弹窗,如果有就点击关闭
57 try:
58     closeBtn=driver.find_element_by_id('com.mymoney:id/close_iv')
59 except NoSuchElementException:
60     pass
61 else:
62     closeBtn.click()
63 
64 #点击设置
65 driver.find_element_by_id('com.mymoney:id/nav_btn_forth').click()
66 #等待界面菜单加载出来,然后向上滑动
67 WebDriverWait(driver,6).until(lambda x:x.find_element_by_id('android:id/content'))
68 swipeUp()
69 #点击高级菜单
70 driver.find_element_by_android_uiautomator('new UiSelector().text("高级")').click()
71 #点击密码与手势密码菜单
72 driver.find_element_by_id('com.mymoney:id/password_protect').click()
73 #点击手势密码保护
74 driver.find_element_by_id('com.mymoney:id/ll_gesture_psd').click()
75 #连续滑动两次设置图案密码
76 for i in range(2):
77     TouchAction(driver).press(x=212, y=296).wait(100)\
78             .move_to(x=148, y=0).wait(100)\
79             .move_to(x=148,y=0).wait(100)\
80             .move_to(x=-148,y=148).wait(100)\
81             .move_to(x=-148,y=148).wait(100)\
82             .move_to(x=148,y=0).wait(100)\
83             .move_to(x=148,y=0).wait(100) \
84             .release().wait(200).perform()

Swipe continuously to set gesture password coordinate calculation

First use the tool to obtain the coordinate position of the element, you can see that the starting position is [138, 218], and the end position is [581, 661]

analyze:

The graph can be divided horizontally and vertically into six equal parts

Then the coordinates of the center point of the first circle:

x=138+(581-138)/6

y=218+(661-218)/6

By analogy, if you want to draw a z shape, you need to calculate the coordinates of the center points of the circles 1, 2, 3, 5, 7, 8, and 9

Then use the press and moveto methods of TouchAction to link several steps together. code show as below

 1 #引入包 
 2 from appium.webdriver.common.touch_action import TouchAction   
 3 
 4 def settingPassword(self):
 5 
 6         #[138,218][581,661]夜神上的元素坐标  
 7 
 8         xxx = (581-138) / 6
 9         one_x = 138 + xxx 
10         one_y = 218 + xxx
11         two_x = 138 + xxx * 3
12         two_y = 218 + xxx #与第二个纵坐标相等
13         three_x=138 + xxx*5
14         three_y=218 + xxx #与第二个纵坐标相等
15         five_x=138 + xxx * 3
16         five_y=218+xxx*3
17         seven_x=138 + xxx
18         seven_y=218+xxx*5
19         eight_x=138 + xxx * 3
20         eight_y=218+xxx*5
21         nine_x=138 + xxx*5
22         nine_y=218+xxx*5
23 
24         TouchAction(self.driver).press(x=one_x, y=one_y).wait(300).move_to(x=two_x, y=two_y).wait(300).move_to(x=three_x,y=three_y).wait(300).move_to(x=five_x,y=five_y).wait(300).move_to(x=seven_x,y=seven_y).wait(300).move_to(x=eight_x,y=eight_y).wait(300).move_to(x=nine_x,y=nine_y).release().perform()

summary

1. Always report an error when implementing:
The coordinates provided to an interactions operation are invalid.

Solution: Just add wait after release. This is a solution that I found after searching a lot of information. The specific reason is a bit confusing. When I find the reason, I will make it up later, or someone who knows can share my experience.
I didn’t want to use the for loop during the period. I wrote two identical TouchActions and still reported an error. Then I couldn’t figure it out. The next night I decided that since I could draw a zigzag, I decided to change the article about setting the password into an article about unlocking the password. The former needs to draw two zigzags, and the latter needs to draw one zigzag. And just to unlock it, you only need one. The little devil in your heart is fighting. In the end, rationality defeated trickery and willfulness, and bit the bullet to solve all kinds of problems encountered. Only then did this article come into being. This is also a side reaction that learning and work cannot be cheated.                                                                                   

                

1 for i in range(2):
2     TouchAction(driver).press(x=212, y=296).wait(100)\
3             .move_to(x=148, y=0).wait(100)\
4             .move_to(x=148,y=0).wait(100)\
5             .move_to(x=-148,y=148).wait(100)\
6             .move_to(x=-148,y=148).wait(100)\
7             .move_to(x=148,y=0).wait(100)\
8             .move_to(x=148,y=0).wait(100) \
9             .release().wait(100).perform()                                                                                                                                                                                           

2. There are two pitfalls in this code, uh, key points:
(1) The coordinates in the press method are absolute coordinates, and the coordinates in the move_to method are relative coordinates. Specifically, the coordinates in the first move_to are relative to The coordinates in the press method, the coordinates in the second move_to method are relative to the coordinates in the first move_to method. By analogy, it didn't seem to be like this before. It can be used directly after calculation. Mistakes Hong Ge made during the period:

  a. It is to use the result to report an error directly after the calculation; then check the data and say that it is a relative coordinate;

  b. Then the coordinates of move_to are relative to the first absolute coordinates. As you can imagine, the result is still an error.

Continue to search information, only to find the above solution.

(2) wait is necessary: ​​here ms is expressed as milliseconds, ms=100 means waiting for 100 milliseconds. If you don't use it, it will go wrong too soon

3. Divide into six equal parts, my friends are a bit confused, let me show you a more intuitive picture

4. Well, finally got it done and got a new life.


              [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/131980462