python+selenium+appium元素定位和各种操作方法的封装

显示等待

显示等待方法可提高自动化测试效率,和用例稳定性;
显示等待实现原理:写个死循环每次获取当前时间去和需等待时间对比,如果在等待时间内找到元素或者当前时间大于等待时间则跳出死循环

# 代码实现
import time

def wait(element,timeout):
    end_time = time.time() + timeout # 结束时间,timeout-需等待时间单位秒
    while True:
        # 需执行的条件
        if element:
            break #等待时间内达到条件跳出循环
        if time.time() > end_time:
            break # 如果当前时间大于等待时间跳出循环   

重新封装find_element方法实现显示等待

使用框架:selenium包

  • WebDriverWait -显示等待
  • expected_conditions.visibilty_of_element_located -查询页面元素

1、新建.py文件,文件名自定

# 导入方法
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as Es

class base_page():
    def __init__(self,driver):
        self.driver = driver # 传入实例化webdriver
        
    # 重新封装find_element,传参传入元祖
    def find_element(self,*loc):
        try:
            WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(*loc))
            return self.driver.find_element(*loc)
        except Exception as e
            raise e
    
    def send_keys(self,values,*loc):
        try:
            self.find_element(*loc).clear()
            self.find_element(*loc).send_keys(values)
        except AttributeError as e:
            raise e

封装元素操作方法

封装好一些常用的操作方便调用,比如点击、输入等事件
1、新建一个operation.py文件,文件名自定

# 导入方法
from base_page import base_page # 上面封装好的find_element

class operation(base_page.base_page):
    
    # 点击事件
    def click(self,target):
        self.find_element(*target).click()

    # 输入事件
    def input(self,target,content):
        self.send_keys(content,*target)
        
    # 单次滑屏操作适用安卓 i- 1上滑    2下滑    3左滑    其他右滑
    def swipe_screen_android(self,i):
        x = self.driver.get_window_size()['width'] # 获取屏幕宽
        y = self.driver.get_window_size()['height'] # 获取屏幕高
        if i == 1:
            self.driver.swipe(x/2,y*3/4,x/2,y/4) # 上滑
        elif i == 2:
            self.driver.swipe(x/2,y/4,x/2,y*3/4) # 下滑
        elif i == 3:
            self.driver.swipe(x*3/4,y/4,x/4,y/4) # 左滑
        else:
            self.driver.swipe(x/4,y/4,x*3/4,y/4) # 右滑
    
    # 单次滑屏适用于iOS i- 1上滑    2下滑    3左滑    其他右滑
        def swipe_screen_iOS(self,i):
	        if i  == 1:
	            self.driver.execute_script('mobile: swipe', {'direction': 'up'})
	        elif i  == 2:
	            self.driver.execute_script('mobile: swipe', {'direction': 'down'})
	        elif i == 3:
	            self.driver.execute_script('mobile: swipe', {'direction': 'left'})
	        else:
	            self.driver.execute_script('mobile: swipe', {'direction': 'right'})

	  # 通过屏幕坐标点击
	    def touch_tap(self,x,y,duration=100):
	        screen_width  = self.driver.get_window_size()['width'] # 获取当前屏幕宽度
	        screen_height = self.driver.get_window_size()['height'] # 获取当前屏幕的高
	        a = (float(x)/screen_width)*screen_width
	        x1 = int(a)
	        b = (float(y)/screen_height)*screen_height
	        y1 = int(b)
	        return self.driver.tap([(x1,y1),(x1,y1)],duration)
	    
	    # 截图,
	    def screenshot(self,picture_name):
	        pircture_path = f'截图存放路径/{pircture_name}' # 截图存放路径
	        self.driver.get_screenshot_as_file(pircture_path)

2、页面元素的整理,一些常用的按钮或输入框可以封装好,方便调用
新建一个login_page文件,存放登录相关元素

from appium.webdriver.common import mobileby

class login_page():
    by = mobileby.Moblieby()
    
    login_button = (by.XPATH,'//*[@text = "登录"]') 
    account_inputbox = (by.ID,"请输入账号")
    pwd_inputbox = (by.XPATH, '//*[@resoutce-id = "输入密码"]')

用例编写

1.编写一个简单的登录操作

import dirver_config # 导入webdriver
from operation import operation # 操作
from login_page import login_page #元素定位
import unittest

class login_test(unittest.TestCase):
    
    # 整个类用例运行会前运行一次
    @classmethod
    class setUpClass(cls):
        cls.driver = driver_config # 启动app
        cls.ot = operation(cls.driver)
        cls.login_page = login_page()
        
    # 整个类用例结束后会运行一次
    @classmethod
    class tearDownClass(cls):
        cls.driver.quit() # 关闭app
    
    def test_login():
        cls.ot.input(cls.login_page.account_inputbox,123456) # 输入账号
        cls.ot.input(cls.login_page.pwd_inputbox,123456) # 输入密码
        cls.ot.click(cls.login_page.login_button) # 点击登录账号按钮

if __name__ == "__main__":
    unittest.main() # 运行文件虽所有以test开头或结尾的用例

你可能感兴趣的:(appium,自动化,python,python,selenium,开发语言)