最近在做UI自動(dòng)化膛锭,寫了幾個(gè)小工具,需要自取
__author__ = 'xiaoj'
import time
import shutil#強(qiáng)制刪除
import os
class Tools:
#初始化傳參,傳入driver
def __init__(self,driver):
self.driver = driver
#-----------------------------------上下左右滑動(dòng)功能-------------------------------
#獲取屏幕大小
def getSize(self):
x = self.driver.get_window_size()['width']
y = self.driver.get_window_size()['height']
return (x,y)
#向左滑動(dòng)
def swipeLeft(self,t):
l = self.getSize()
x1 = int(l[0]*0.9)
y1 = int(l[1]*0.5)
x2 = int(l[0]*0.1)
self.driver.swipe(x1,y1,x2,y1,t)
#向右滑動(dòng)
def swipeRight(self,t):
l = self.getSize()
x1 = int(l[0]*0.25)
y1 = int(l[1]*0.5)
x2 = int(l[0]*0.75)
self.driver.swipe(x1,y1,x2,y1,t)
#向上滑動(dòng)
def swipeUp(self,t):
l = self.getSize()
x1 = int(l[0]*0.5)
y1 = int(l[1]*0.8)
y2 = int(l[1]*0.4)
self.driver.swipe(x1,y1,x1,y2,t)
time.sleep(5)
#向下滑動(dòng)
def swipeDown(self,t):
l = self.getSize()
x1 = int(l[0]*0.5)
y1 = int(l[1]*0.25)
y2 = int(l[1]*0.75)
self.driver.swipe(x1,y1,x1,y2,t)
#------------------------------帶時(shí)間戳的截圖功能-------------------------------
def getTime(self):
tamp = time.strftime('%H%M%S',time.localtime())#時(shí)分秒
return tamp
#截圖功能
def getScreenShot(self,path,filename):
timeA = self.getTime()
filename = path + filename
filename = filename + '%s.png'%timeA
self.driver.get_screenshot_as_file(filename)
#-----------------------------強(qiáng)制刪除文件夾再覆蓋截圖-------------------------
def coverUpdate(self,path):
#強(qiáng)制刪除文件夾
shutil.rmtree(path)
#重新創(chuàng)建文件夾
os.mkdir(path)
#-----------------------------------------查找元素蝗岖,沒找到滑動(dòng)---------------------------------------
def findLocal(self):
x = 1
while x ==1:
if self.fact() ==1:
self.swipeUp(2000)
time.sleep(3)
self.fact()
else:
print("找到了")
x = 2
#遞歸
def fact(self):
n = 1
try:
self.driver.find_element_by_id('').click()
except Exception as e:
return n