1. 自动化测试中如何标注定位元素
# 实现对定位到的dom标注蓝色
#dr:webdriver 实例对象
# EM:定位到的DOM节点
self.dr.execute_script("arguments[0].setAttribute('style',arguments[1]);", EM, "border:2px solid blue;")
# 恢复到原来样式:
self.dr.execute_script("arguments[0].setAttribute('style',arguments[1]);", EM, "")
2. 通过js修改dom的属性方便selenium使用
def editEM(self, EM: WebElement, keys: str, values='test'):
# 编辑元素:
try:
if not keys:
log.logger.error("dom元素没有的编辑方式")
key = keys.lstrip().lower()
if key in ['set_to_display']:
# 修改元素属性可见
js = "arguments[0].style.display = 'block';"
self.dr.execute_script(js, EM)
log.logger.info("修改display = 'block'的结果:{}".format(EM.is_displayed()))
elif key in ['add_id']:
# 元素增加id
js = "arguments[0].setAttribute('id', arguments[1]);"
self.dr.execute_script(js, EM, values)
log.logger.info("增加id:{}".format(values))
elif key in ['value']:
# 增加value值
js = "arguments[0].setAttribute('value', arguments[1]);"
self.dr.execute_script(js, EM, values)
log.logger.info("增加value:{}".format(values))
elif key in ['style']:
# 增加style
js = "arguments[0].setAttribute('style', arguments[1]);"
self.dr.execute_script(js, EM, values)
log.logger.info("增加style:{}".format(values))
elif key in ['attr']:
# 增加属性
js = "arguments[0].setAttribute(values[0], arguments[1]);"
self.dr.execute_script(js, EM, values[1])
log.logger.info("增加{}:{}".format(values[0], values[1]))
elif key in ['back']:
# 恢复到修改前
if values:
self.dr.execute_script("arguments[0].setAttribute('style',arguments[1]);", EM, values)
else:
self.dr.execute_script("arguments[0].setAttribute('style',arguments[1]);", EM, "")
else:
log.logger.error("不支持的的编辑方式:{}".format(keys))
except(TimeoutException, NoSuchElementException) as e:
log.logger.error("editEM元素编辑异常:{}".format(e))
3. 滚动页面
def bscroll(self, keys: str = 'to_view', EM: WebElement = None):
# 页面滚动,顶部,底部,滑动指定dom元素到可见
# js实现滚动鼠标
# log.logger.info("执行滚动:{}".format(keys))
if not keys:
log.logger.error("没有定义的滚动方式")
key = keys.lstrip().lower()
if key in ['up']:
# log.logger.info("向上滚动")
js = "window.scrollTo(0, 0)"
self.dr.execute_script(js)
elif key in ['down']:
# log.logger.info("向下滚动")
js = "window.scrollTo(0, 10000)"
self.dr.execute_script(js)
elif key in ['to_view'] and EM:
# log.logger.info("滚动到元素可见")
js = "arguments[0].scrollIntoView();"
self.dr.execute_script(js, EM)
elif key in ['until_em'] and EM:
# log.logger.info("滚动一直到元素可见")
self.bscroll('up')
count = 10
tag = True
while tag:
js = "window.scrollTo(0, 10)"
self.dr.execute_script(js)
log.logger.info("滑动一次")
ems = self.findEMs('xpath', '{}'.format(EM))#自定义的元素查找
if ems is None:
continue
if self.isEM(ems[0], 'is_displayed'): # 自定一个元素是否可见的方法
return ems[0]
else:
if count < 1000:
count = count + 1
else:
break
elif key in ['get_po_em']:
# 获取当前坐标的元素信息
js = '''
window.hovered_element = null
function track_mouse(event){
var x = event.clientX, y = event.clientY
var element = document.elementFromPoint(x, y)
if (!element) {
window.hovered_element = null
return // 当前位置没有元素
}
window.hovered_element = element
}
window.onmousemove = track_mouse
'''
self.dr.execute_script(js)
element = self.dr.execute_script('return window.hovered_element')
if element:
return element.tag_name, element.text
else:
log.logger.info("当前坐标没有元素")
time.sleep(1)
else:
log.logger.error("不支持的滚动方式")
4. 常用截图
def getImg(self, keys=1, caseName=None, EM=None, is_ocr=False):
# 失败截图 caseName 失败测试用例的名称
# 1.本地截图 2.元素抠图 3.base64截图 4.自带元素截图
failCasePath = os.path.join(self.root, 'failImg', '{}.png'.format(caseName))
if keys == 1:
if os.path.exists(failCasePath):
os.remove(failCasePath)
self.dr.save_screenshot(failCasePath)
return failCasePath
elif keys == 2 and EM: # 元素截图/抠图
if os.path.exists(failCasePath):
os.remove(failCasePath)
left = EM.location['x']
top = EM.location['y']
right = EM.location['x'] + EM.size['width']
bottom = EM.location['y'] + EM.size['height']
self.dr.get_screenshot_as_file(failCasePath)
im = Image.open(failCasePath)
im = im.crop((left, top, right, bottom))
im.save(failCasePath)
return failCasePath
elif keys == 3:
return self.dr.get_screenshot_as_base64()
elif keys == 4 and EM:
# selenium 自带截图,截取元素图片
a = EM.screenshot_as_base64 # base64加密,无括号
b = base64.b64decode(a) # 解密
with open(failCasePath, mode="wb") as f:
f.write(b)
return failCasePath