Appium常用方法封装

2016-11-19  本文已影响0人  One2Three

1、屏幕滑动方法

def swipe_Down(self):
    window_size = self.driver.get_window_size()
    width = window_size.get("width")
    height = window_size.get("height")
    self.driver.swipe(width/2,height/4,width/2,height*3/4,500)

   def swipe_Up(self):
    window_size = self.driver.get_window_size()
    width = window_size.get("width")
    height = window_size.get("height")
    self.driver.swipe(width/2,height*3/4,width/2,height/4,500)

def swipe_Left(self):
    window_size = self.driver.get_window_size()
    width = window_size.get("width")
    height = window_size.get("height")
    self.driver.swipe(width/4,height/2,width*3/4,height/2,500)

def swipe_Rigth(self):
    window_size = self.driver.get_window_size()
    width = window_size.get("width")
    height = window_size.get("height")
    self.driver.swipe(width*4/5,height/2,width/5,height/2,500)

2、封装查找元素及自动等待方法

def find_elements(self,loc):
    '''封装一组元素定位方法'''
    try:
        if len(self.driver.find_elements(*loc)):
            return self.driver.find_elements(*loc)
    except Exception as e:
        print(u"%s 页面中未能找到 %s 元素" %(self,loc))
        return False
def find_element(self,loc):
    '''封装单个元素定位方法'''
    try:          
      WebDriverWait(self.driver,15.).until(lambdadriver:driver.find_element(*loc).is_displayed())
        return self.driver.find_element(*loc)
    except Exception as e:
        print(u"%s 页面中未能找到 %s 元素" %(self,loc))
        return False

3、保存图片

def take_Shot(self,name):
    day = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    fp = "..\\Result\\" + day
    tm = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))
    type = '.png'
    
    filename = ''
    if os.path.exists(fp):
        filename = fp+"\\" + tm +'_'+ name + type
    else:
        os.makedirs(fp)
        filename = fp+"\\" + tm +'_'+ name + type
    self.driver.save_screenshot(filename)
上一篇下一篇

猜你喜欢

热点阅读