# -*- coding:utf-8 -*-
# date = 2019/5/13
# name = "yedeng"
# project_name = python
# explain : 从yaml文件中读取返回webelement
import yaml
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from utils.FindWebElemet import FindWebElement
from utils.FindBy import FindBy
from utils.log import Log
from utils.ReadIni import Readini
# yaml文件位置
# Readin 读取配置文件中的等待时间
'''
初始化添加对应的yaml文件位置,然后调用getAllValue方法,调用 getelement方法获取到对应的webelement
改进方法:保证yaml的文件名和test_case的文件名或者类名一致
'''
class ReadyamlToElement:
# 获取当前脚本所在文件夹路径
def __init__(self, driver: WebDriver, yamlpath):
"""
实例化readyaml类
:param driver:
:param yamlpath: 对应的yaml文件名
"""
self.__yamlPath = yamlpath
self.__driver = driver
def __getAllValue(self):
"""
将指定yaml文件转化为dic字典,key为
:return dic字典
"""
'''
curPath = os.path.dirname(os.path.realpath(__file__))
# 获取yaml文件路径
yamlPath = os.path.join(curPath, "cfgyaml.yaml")
print(type(yamlPath))
'''
with open(self.__yamlPath, 'r', encoding='utf-8') as f:
cfg = f.read()
dic_yaml = yaml.load(cfg, Loader=yaml.FullLoader) # 用load方法转字典
return dic_yaml
'''
通过key获取到对应的webelement,改进方法,将其全部遍历后,生成key:element的字典,从里面取对应的数据
'''
def getelement(self, key) -> WebElement:
"""
通过yaml文件的key获取到对应的Webelement
:param key: yaml文件的key
:return: 对应的WebElement对象
:rtype keyelement: selenium.webdriver.remote.webelement
"""
dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict
# print(dic)
if key in dic:
list_value = dic.get(key)
# list_Type_value = list_value[0].split(":")[1] #获取type的值
# list_Value_value = list_value[1].split(":")[1] #获取value的值
list_Type_value = list_value.get("Type") # 获取type的值
list_Value_value = list_value.get("Value") # 获取value的值
keyelement = FindWebElement(self.__driver, list_Type_value,
list_Value_value).find_element(self.__yamlPath, key) # 用于返回webelement对象
Log.info(self.__yamlPath + "中获取到该element,通过该key:" + key)
return keyelement
else:
Log.error(self.__yamlPath + "中不存在该key:" + key + ":关闭程序")
# self.__driver.quit()
def getelements(self, key) -> list:
"""
通过yaml文件的key获取到对应的Webelement
:param key: yaml文件的key
:return: 对应的WebElement对象 类型为WebElement
:rtype keyelements: list
"""
dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict
# print(dic)
if key in dic:
list_value = dic.get(key)
# list_Type_value = list_value[0].split(":")[1] #获取type的值
# list_Value_value = list_value[1].split(":")[1] #获取value的值
list_Type_value = list_value.get("Type") # 获取type的值
list_Value_value = list_value.get("Value") # 获取value的值
keyelementlist = FindWebElement(self.__driver, list_Type_value,
list_Value_value).find_elements(self.__yamlPath, key) # 用于返回webelement对象
Log.info(self.__yamlPath + "中获取到该element,通过该key:" + key)
return keyelementlist
else:
Log.error(self.__yamlPath + "中不存在该key:" + key + ":关闭程序")
# self.__driver.quit()
def getelementsintnum(self, key, intnum=0) -> WebElement:
"""
通过yaml文件的key获取一组WebElemnts,通过数组下标获取对应的WebElemnt
:param key: yaml文件的key
:param intnum 数组下标
:type key : String
:type intnum : int
:return: 对应的WebElement对象
:rtype keyelements[intnum]: selenium.webdriver.remote.webelement
"""
dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict
# print(dic)
list_keyelements = []
if key in dic:
list_value = dic.get(key)
# list_Type_value = list_value[0].split(":")[1] #获取type的值
# list_Value_value = list_value[1].split(":")[1] #获取value的值
list_Type_value = list_value.get("Type") # 获取type的值
list_Value_value = list_value.get("Value") # 获取value的值
list_keyelements = FindWebElement(self.__driver, list_Type_value,
list_Value_value).find_elements(self.__yamlPath, key) # 用于返回webelement对象
Log.info(self.__yamlPath + "中获取到该element,通过该key:" + key)
else:
Log.error(self.__yamlPath + "中不存在该key:" + key + ":关闭程序")
# self.__driver.quit()
# 判断输入的值是否正确,正确则正确找到,否则就报空
if -len(list_keyelements) <= intnum < len(list_keyelements):
return list_keyelements[intnum]
else:
Log.error("当前输入的intnum大于" + str(len(list_keyelements)) + "有问题")
return None
def getelementsstringtext(self, key, strtext: str, attributename=None) -> WebElement:
"""
通过yaml文件的key获取一组WebElemnts,通过指定元素的值获取到对应的WebElemnt
:param key: yaml文件的key
:param strtext 对应元素的值
:type key : str
:type strtext : str
:param attributename 元素的属性值,Name of the attribute/property to retrieve
:type attributename : str
:return: 对应的WebElement对象
:rtype element: selenium.webdriver.remote.webelement
"""
dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict
# print(dic)
list_keyelements = []
if key in dic:
list_value = dic.get(key)
# list_Type_value = list_value[0].split(":")[1] #获取type的值
# list_Value_value = list_value[1].split(":")[1] #获取value的值
list_Type_value = list_value.get("Type") # 获取type的值
list_Value_value = list_value.get("Value") # 获取value的值
list_keyelements = FindWebElement(self.__driver, list_Type_value,
list_Value_value).find_elements(self.__yamlPath, key) # 用于返回webelement对象
Log.info(self.__yamlPath + "中获取到该element,通过该key:" + key)
else:
Log.error(self.__yamlPath + "中不存在该key:" + key + ":关闭程序")
# self.__driver.quit()
# 判断输入的值是否正确,正确则正确找到,否则就报空
booleanfind = False
element = None
# 当没有设备属性值时用元素的text值
if attributename is None:
for webelement in list_keyelements:
if webelement.text == strtext:
element = webelement
booleanfind = True
break
# 当设备属性值时用元素的指定的属性值的内容
else:
for webelement in list_keyelements:
if webelement.get_attribute(attributename) == strtext:
element = webelement
booleanfind = True
break
if booleanfind:
return element
else:
Log.error(self.__yamlPath + "中未找到对应的元素值,请确认" + strtext + "的正确")
return element
def waitgetelement(self, key):
"""
通过yaml文件的key获取到对应的Webelement,加入了显示等待,WeblEment.is_displayed()为真时返回该该webElement
:param key: yaml文件的key
:return: 对应的WebElement对象
"""
try:
inttime = Readini().getvalue("webdriver", "implicit_waitTime")
WebDriverWait(self, int(inttime)).until(lambda x: x.getelement(key).is_displayed())
return self.getelement(key)
except Exception as e:
Log.error(self.__yamlPath + "中该元素不存在:" + key + "导致错误:" + e.__str__())
# self.__driver.quit()
def waitgetelements(self, key):
"""
通过yaml文件的key获取到对应的Webelement,加入了显示等待,WeblEment.is_displayed()为真时返回该该webElement
:param key: yaml文件的key
:return: 对应的WebElement对象
"""
try:
inttime = Readini().getvalue("webdriver", "implicit_waitTime")
WebDriverWait(self, int(inttime)).until(lambda x: x.getelement(key).is_displayed())
return self.getelements(key)
except Exception as e:
Log.error(self.__yamlPath + "中该元素不存在:" + key + "导致错误:" + e.__str__())
# self.__driver.quit()
def waitgetelement_clickable(self, key) -> WebElement:
"""
通过yaml文件的key获取到对应的Webelement,当元素可以点击时返回webelement
:param key: yaml文件的key
:return: 对应的WebElement对象
:rtype keyelement: selenium.webdriver.remote.webelement
"""
dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict
if key in dic:
list_value = dic.get(key)
list_Type_value = list_value.get("Type") # 获取type的值
list_Value_value = list_value.get("Value") # 获取value的值
try:
# 显式等待时间
inttime = Readini().getvalue("webdriver", "implicit_waitTime")
# 配合expected_conditions当元素可以点击时返回element
elemnet = WebDriverWait(self.__driver, int(inttime)).until(EC.element_to_be_clickable((
FindBy(list_Type_value).find_by(), list_Value_value)), (self.__yamlPath + "中No such element : 请检查该‘" + key +
"’对应下的Value值是否能在页面上定位成功"))
return elemnet
except TimeoutException as e:
Log.error(e.__str__())
def getlocator(self, key) -> tuple:
"""
返回location 元组类型的值,主要给EC模块入参使用
:param key : yaml文件的key
:return: tup_locator 元组类型的值如(By.Id,"value")
:rtype tup_locator : tuple
"""
dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict
if key in dic:
list_value = dic.get(key)
# list_Type_value = list_value[0].split(":")[1] #获取type的值
# list_Value_value = list_value[1].split(":")[1] #获取value的值
list_Type_value = list_value.get("Type") # 获取type的值
list_Value_value = list_value.get("Value") # 获取value的值
tup_locator = (FindBy(list_Type_value).find_by(), list_Value_value)
if len(tup_locator) is None:
Log.error(self.__yamlPath + "中请检查该‘" + key + "’的值是否正确")
else:
return tup_locator
当前文章:定位元素操作
当前网址:
http://cdweb.net/article/ihpegj.html