定位元素封装find_element(增加显性等待等)
封装
# find_elementUtil.py import os import time from selenium.webdriver.support import expected_conditions as EC from appium.webdriver.common.appiumby import AppiumBy from selenium.webdriver.support.wait import WebDriverWait # from base import driver_configure # from selenium.webdriver.support.wait import WebDriverWait # from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException class ElementClass: def __init__(self, driver): # 构造函数。实例化每个页面的时候,都可以获取到java对象 self.driver = driver def find_element(self, location_type, locator_expression): # 元素定位 """ 重写find_element方法,显式等待 """ try: element = WebDriverWait(self.driver, 10, 1).until( EC.visibility_of_element_located((getattr(AppiumBy, location_type), locator_expression))) return element except NoSuchElementException as msg: print(u"查找元素异常: %s" % msg) # self.driver.back() # raise msg # 抛出异常 return False def is_element(self, location_type, locator_expression): # 判断元素是否存在 e = self.driver.find_element(location_type, locator_expression) == [] if e: return False else: return True def send_keys(self, location_type, locator_expression, value): try: self.find_element(location_type, locator_expression).clear() self.find_element(location_type, locator_expression).send_keys(value) except AttributeError as e: raise e def element_click(self, location_type, locator_expression): # 单击操作 try: self.find_element(location_type, locator_expression).click() except AttributeError as e: raise e def get_attributes(self, location_type, locator_expression, text_value): # 获取值 try: attr_ = self.find_element(location_type, locator_expression).get_attribute(text_value) return attr_ except AttributeError as e: raise e # 保存图片 def get_windows_img(self): """ 在这里我们把file_path这个参数写死,直接保存到我们项目根目录的一个文件夹'Screenshots'下 """ # 获取到当前文件的目录,并检查是否有screenshots文件夹,如果不存在则自动新建screenshots文件 file_path = os.path.dirname(os.path.abspath("."))+'/screenshots/' if not os.path.exists(file_path): os.makedirs(file_path) rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time())) screen_name = file_path+rq+'.png' try: self.driver.get_screenshot_as_file(screen_name) except NameError as e: print(e) self.get_windows_img()
(测试用例读取写入excel)appium+pytest数据驱动 的action文件部分重新如下:(excel文件模式也有所改变)
# ren_excel_step.py from util.excel_readUtil import HandleExcel # from appium.webdriver import WebElement from config.driver_configure import DriverClinet from util.assertUtil import AssertUtil import allure from util.find_elementUtil import ElementClass # from util.loggerUtil import Logger class Context: pass def step_case(fimename, sheetname): driver = DriverClinet().get_driver() class_driver = ElementClass(driver) case = HandleExcel(fimename, sheetname).get_cases() # 遍历测试用例下的具体步骤 for step in case: action_ = step.get('action', None) # 如果用step['action'],当step没有action会抛异常,action指操作有定位元素、点击、输入等操作 desc_d = step.get('description', None) # 获取描述内容 ele_ = step.get('element_operator', None) # find_ = step.get('find', None) # 定位元素,是find_element、还是find_elements selector_ = step.get('selector', None) # 定位元素的类型 selector_value_ = step.get('selector_value', None) # 定位元素的值 save_val_ = step.get('save_val', None) # 将操作的结果保存的对象名 # operate_obj_ = step.get('operate_object', None) # 对哪个对象进行操作 inputtext_ = step.get('inputtext', None) # 输入操作的驶入内容 attribute_value_ = step.get('attribute_value', None) # 获取对象什么属性 if action_ == 'desc': allure.dynamic.title(desc_d) if action_ == 'click': if ele_: if selector_ and selector_value_: class_driver.__getattribute__(ele_)(selector_, selector_value_) else: raise ValueError('在用例文件{}行缺少定义selector_或selector_value_的值'.format(case.index(step))) else: raise ValueError('在用例文件{}行缺少定义ele_的值'.format(case.index(step))) if action_ == 'send_keys': if selector_ and selector_value_: if inputtext_: class_driver.__getattribute__(ele_)(selector_, selector_value_, inputtext_) else: raise ValueError('在用例文件{}行缺少定义inputtext_的值'.format(case.index(step))) else: raise ValueError('在用例文件{}行缺少定义selector_或selector_value_的值'.format(case.index(step))) if action_ == 'get_attribute': if ele_ == 'get_attributes': if attribute_value_: setattr(Context, save_val_, class_driver.__getattribute__(ele_)(selector_, selector_value_, attribute_value_)) # class_driver.__getattribute__(ele_)(selector_, selector_value_, attribute_value_) else: raise ValueError('在用例文件{}行缺少定义attribute_value_的值'.format(case.index(step))) else: raise ValueError('在用例文件{}行缺少定义ele_的值'.format(case.index(step))) if action_ == 'assert': step_ = step.get('step_name', None) # 获取步骤名 assert_type_ = step.get('assert_type', None) assert_value_ = step.get('assert_value', None) expect_value_ = step.get('expect_value', None) if step_: # 判断步骤名称,设置为必填的情况,要不然无法执行assert指令,如果不需要allure报告的话,该判断可以不用 with allure.step(step_): if assert_value_: if str(assert_value_).find('$') == 0: # 需要取变量 assert_value_ = getattr(Context, str(assert_value_).lstrip('$')) if str(expect_value_).find('$') == 0: expect_value_ = getattr(Context, str(expect_value_).lstrip('$')) if assert_type_ and assert_value_: AssertUtil(assert_type_, getattr(Context, assert_value_), expect_value_) else: raise ValueError('在用例文件{}行缺少定义assert_type_的值'.format(case.index(step))) else: raise ValueError('在用例文件{}行缺少定义step_的值'.format(case.index(step)))
运行后正常