[Appium] UI interface of appium automation entry

Daily update ( probably )
this time to talk aboutHow to write code for interface clicks,In this sectionMore code, You can copy and paste it as a template (manually)

Welcome everyone to click and enter the password: CSDN to remind the change, and the reception lady to talk about the reminder, you canMore explosionsOh~~

Book continues from the above: [appium] element of appium automation introduction
API article: [appium] appium automation introduction API (part 1)
environment construction article: [appium] appium automation introduction environment construction (part 1)

text

2.10 Swipe the screen

2.10.1 swipe introduction

  1. View source syntax,Four coordinate parameters of start and end point, Duration is the duration of sliding the screen, the shorter the time, the faster the speed.

The default is None, you can leave it blank, generally set to 500-1000 milliseconds

 swipe(self, start_x, start_y, end_x, end_y, duration=None)
  Swipe from one point to another point, for an optional duration. 
  #从一个点滑动到另外一个点,duration 是持续时间 

 #参数: 
 - start_x - 开始滑动的 x 坐标 
 - start_y - 开始滑动的 y 坐标 
 - end_x - 结束点 x 坐标 
 - end_y - 结束点 y 坐标 
 - duration - 持续时间,单位毫秒 

#:例如: 
driver.swipe(100, 100, 100, 400)
  1. It is worth noting thatThe phone starts at 0 o'clock in the upper left corner, The horizontal axis is the x axis, the vertical axis is the y axis
    Insert picture description here

2.10.2 Get coordinates

  1. Since the resolution of each mobile phone screen is different, the coordinates of the same element on different mobile phones are also different.The coordinates can’t be written dead when sliding

The width and height of the screen must be obtained first, and then calculated by the ratio.

# coding:utf-8 
from appium import webdriver 
desired_caps = {
    
    
 'platformName': 'Android', 
 'deviceName': '30d4e606', 
 'platformVersion': '4.4.2', 
 # apk 包名 
 'appPackage': 'com.taobao.taobao', 
 # apk 的 launcherActivity 
 'appActivity': 'com.taobao.tao.welcome.Welcome' 
 } 
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) 
# 获取屏幕的 size 
size = driver.get_window_size() 
print(size) 
# 屏幕宽度 
width print(size['width']) 
# 屏幕高度 
width print(size['height']) 
  1. Operation result:
    For example: {u'width': 720, u'height': 1280}
    width: 720
    height: 1280

2.10.3 Package sliding method

  1. Encapsulate the four commonly used sliding methods up, down, left and right, so that you can call them directly when you want to slide the screen in the future
    Parameter 1: driver
    Parameter 2: t is the duration
    Parameter 3: Number of swipes
  2. Code reference
#coding:utf-8 
from appium import webdriver 
from time import sleep 
desired_caps = {
    
    
 'platformName': 'Android', 
 'deviceName': '30d4e606', 
 'platformVersion': '4.4.2', 
 # apk 包名 
 'appPackage': 'com.taobao.taobao', 
 # apk 的 launcherActivity  
 'appActivity': 'com.taobao.tao.welcome.Welcome' 
 } 
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub', 
desired_caps) 

def swipeUp(driver, t=500, n=1): 
 '''向上滑动屏幕''' 
 l = driver.get_window_size() 
 x1 = l['width'] * 0.5 
 # x 坐 标 
 y1 = l['height'] * 0.75 
 # 起始 y 坐标 
 y2 = l['height'] * 0.25 
 # 终点 y 坐标 
 for i in range(n):
  driver.swipe(x1, y1, x1, y2, t) 
def swipeDown(driver, t=500, n=1): 
 '''向下滑动屏幕''' 
 l = driver.get_window_size() 
 x1 = l['width'] * 0.5 
 # x 坐 标 
 y1 = l['height'] * 0.25 
 # 起 始 y 坐 标 
 y2 = l['height'] * 0.75 
 # 终 点 y 坐 标 
 for i in range(n):
  driver.swipe(x1, y1, x1, y2,t) 
def swipLeft(driver, t=500, n=1):
 '''向左滑动屏幕''' 
 l = driver.get_window_size() 
 x1 = l['width'] * 0.75 
 y1 = l['height'] * 0.5 
 x2 = l['width'] * 0.05 
 for i in range(n): 
  driver.swipe(x1, y1, x2, y1, t) 
def swipRight(driver, t=500, n=1):
 '''向右滑动屏幕''' l = driver.get_window_size() 
 x1 = l['width'] * 0.05 
 y1 = l['height'] * 0.5 
 x2 = l['width'] * 0.75 
 for i in range(n):
  driver.swipe(x1, y1, x2, y1, t) 
ifname=="main":
 print(driver.get_window_size())
 sleep(5) 
 swipLeft(driver, n=2) 
 sleep(2) 
 swipRight(driver, n=2) 

2.11 Simulate finger tap (tap)

Preface

Sometimes when positioning an element, you still can't locate it after using the eighteenth class martial arts. What should you do?
This question is often asked in interviews, Then teach everyone a unique skill: the coordinates of the location of the point element (Don't use it often

2.11.1 tap usage

1. tap is to simulate a finger click.
Generally, the syntax of elements on the page has two parameters, the first is positions, the other is duration

tap(self, positions, duration=None):
 Taps on an particular place with up to five fingers, holding for a certain time 
#模拟手指点击(最多五个手指),可设置按住时间长度(毫秒)
 :参数: 
 - positions - list 类型
 - #里面对象是元组,最多五个.
 - #例如
 - [(100, 20), (100, 60)] 
 -  duration 
 -  #持续时间,单位毫秒
 - #例如
 - 500 
#:最终样子: 
driver.tap([(100, 20), (100, 60), (100, 100)], 500) 

2.11.2 Coordinate positioning

1. Locate the coordinates of the "Go to see" button as shown below, and you can see the properties of bonds on the right: [374,831] [654,906]
Insert picture description here

2. Click the "Search" button in the upper right corner to view the properties of bonds: [615,52][690,146]
Insert picture description here

2.13.3 Reference case

# coding:utf-8 
from appium import webdriver 
from time import sleep
desired_caps = {
    
    
 'platformName': 'Android', 
 'deviceName': '127.0.0.1:62001', 
 'platformVersion': '4.4.2', 
 'appPackage': 'com.baidu.yuedu', 
 'appActivity':
'com.baidu.yuedu.splash.SplashActivity' 
 } 
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
 sleep(5) 
 # 点弹出框去看看 
driver.tap([(374, 831), (654, 906)], 500) 

# 返回上一页 
driver.back() 
sleep(2) 

# 点右上角搜索按钮 driver.tap([(615, 52), (690, 146)], 500)

Positioning by coordinates is element positioningMake the next move, There is really no way to use this. One is troublesome, and the other is that this positioning method is not commonly used.
But with such a test thinking, maybe it can be used to fight the fire in an emergency.

At last

If you encounter a problem with the above code, you can click and enter the password: CSDN screenshots good your questions, there will be bosses in the group who will come out to answer from time to time, you can also check it out if you need some automated test materials or interview materialsInsert picture description here

Guess you like

Origin blog.csdn.net/Chaqian/article/details/109310493