有调不通的或者需要提供帮助的可QQ:835505010
#common/common_ui.py selenium常用的方法封装
from time import sleep
from selenium import webdriver from
selenium.webdriver.common.action_chains import ActionChains from
selenium.webdriver.support.select import Select
from common.MyTool import sql_script from common.config import
get_var from common.log_record import *
parent_dir =
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
download_path = parent_dir + "/TestFile/"
class WebUI(object):
# 初始化driver
def __init__(self):
global driver, ac
options = webdriver.ChromeOptions()
if (get_var("open", "open_browser").lower() == "no"): options.add_argument('headless') # 静默模式《不打开浏览器》
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': download_path}
options.add_experimental_option('prefs', prefs) # 设置下载路径
driver = webdriver.Chrome(chrome_options=options)
driver.implicitly_wait(30)
driver.set_window_size(1920, 1080)
driver.maximize_window()
print(driver.get_window_size(), "\n")
ac = ActionChains(driver)
def web_open(self, url):
driver.get(url)
def web_quit(self):
driver.quit()
def web_refresh(self):
driver.refresh()
def web_close(self):
driver.close()
def find_by(self, by_method, by_element):
element = None
try:
if by_method.lower() == "css": element = driver.find_element_by_css_selector(by_element)
if by_method.lower() == "xpath": element = driver.find_element_by_xpath(by_element)
if by_method.lower() == "name": element = driver.find_element_by_name(by_element)
if by_method.lower() == "id": element = driver.find_element_by_id(by_element)
except Exception as e:
error("find_by fail:" + e)
print("find_by fail:" + e)
assert False
return element
def op_element(self, op, by_method, by_element, value=None):
debug(op + "\t" + by_method + "\t" + by_element + "\t" + str(value))
if op.lower() == "submit":
a = self.find_by(by_method, by_element)
a.submit()
if op.lower() == "click":
sleep(1)
a = self.find_by(by_method, by_element)
try:
a.click()
sleep(1)
except Exception as e:
print("fail:", e)
driver.execute_script('$(arguments[0]).click()', a)
if op.lower() == "js_click":
a = self.find_by(by_method, by_element)
driver.execute_script('$(arguments[0]).click()', a)
sleep(1)
if op.lower() == "ac_sendkey":
a = self.find_by(by_method, by_element)
ac.send_keys_to_element(a, value)
ac.perform()
if op.lower() == "alert":
sleep(2)
a = driver.switch_to.alert
a.accept()
if op.lower() == "radio":
sleep(1)
ac.move_to_element(self.find_by(by_method, by_element)).click().perform()
sleep(1)
if op.lower() == "sendkey":
ele_send = self.find_by(by_method, by_element)
# ele_send.clear()
ele_send.send_keys(value)
if op.lower() == "select": Select(self.find_by(by_method, by_element)).select_by_visible_text(value)
if op.lower() == "get_text":
act_text = self.find_by(by_method, by_element).text
# str_cmp(value, act_text)
if act_text.find(value) == -1:
error("检查点失败:\t预期结果:" + value + "\t 实际结果:" + act_text)
print("检查点失败:\t预期结果:" + value + "\t 实际结果:" + act_text)
assert False
def get_img(self):
print("失败截图吧")
return driver.get_screenshot_as_base64()
def read_case(self, case_no):
case_list = sql_script("SELECT `操作步骤` FROM loan_test WHERE 案例编号='%s';" % (case_no), True)
cases = []
for i in range(len((case_list))):
cases.append(case_list[i][0])
return cases
def get_step(self, case_no, step):
data_list = sql_script("SELECT "
"`案例描述`,`操作步骤`,`操作描述`,`操作类型`,`定位方法`,`定位元素`,`元素值`FROM loan_test WHERE
`案例编号` "
"= '%s' AND `操作步骤` = '%s';" % (case_no, step), False)
return list(data_list)
def run_step(self, case_no, step, value=None, *p):
data_list = self.get_step(case_no, step)
if len(p) != 0:
data_list[5] = data_list[5] % p
if value is None:
info(data_list[0] + "\t" + data_list[1] + "\t" + data_list[2] + "\t" + str(data_list[6]))
debug(data_list[3] + "\t" + data_list[4] + "\t" + data_list[5])
self.op_element(op=data_list[3], by_method=data_list[4], by_element=data_list[5], value=str(data_list[6]))
else:
info(data_list[0] + "\t" + data_list[1] + "\t" + data_list[2] + "\t" + value)
debug(data_list[3] + "\t" + data_list[4] + "\t" + data_list[5])
self.op_element(op=data_list[3], by_method=data_list[4], by_element=data_list[5], value=value)
# common/log_record.py 记录执行的日志
import logging
import os
from HTMLTestRunner import HTMLTestRunner_cn
# logging_demo.basicConfig(stream=HTMLTestRunner.stdout_redirector)
# 创建一个logger
logging.basicConfig(stream=HTMLTestRunner_cn.stdout_redirector)
logger = logging.getLogger('资产系统')
logger.setLevel(logging.INFO)
# 创建handler,用于写入日志文件
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
fh_local = logging.FileHandler(parent_dir + '/report/test.txt', encoding='utf-8')
fh_local.setLevel(logging.INFO)
# 创建handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# 定义handler的输出格式
formatter = logging.Formatter('%(asctime)s-%(name)s-%(levelname)s:%(message)s')
fh_local.setFormatter(formatter)
# ch.setFormatter(formatter)
# 给logger添加handler
logger.addHandler(fh_local)
# logger.addHandler(ch)
# info
def info(msg):
logger.info(msg)
def error(msg):
logger.error(msg)
def debug(msg):
logger.debug(msg)
if __name__ == '__main__':
info("hello111")
# common/MyTool.py 常用工具
import pymysql
import redis as redis
from common.config import get_var
# redis处理
def redis_id_sn():
# 连接redis
red = redis.Redis(host='localhost', port=6379, db=1)
# 读取redis中存的日期,做对比
pass
# 自动生成身份证,需要传参身份证前六位以及三位的序号
def creat_id_number():
pass
def sql_script(sql_script, all=True):
db = pymysql.connect(
host=get_var("loan_host", "DB"),
port=int(get_var("loan_port", "DB")),
db=get_var("auto_test_db", "DB"),
user=get_var("loan_user", "DB"),
passwd=get_var("loan_pwd", "DB"),
charset=get_var("loan_charset", "DB"),
)
cursor = db.cursor()
cursor.execute(sql_script)
result = '未查询到信息,请检查SQL是否有误!!!!'
if all: result = cursor.fetchall()
if not all: result = cursor.fetchone()
db.close()
return result
def num_to_char(num):
"""数字转中文"""
num = str(num)
new_str = ""
num_dict = {"0": "零", "1": "一", "2": "二", "3": "三", "4": "四", "5": "五", "6": "六", "7": "七", "8": "八", "9": "九"}
listnum = list(num)
shu = []
for i in listnum:
shu.append(num_dict[i])
new_str = "".join(shu)
return new_str
if __name__ == '__main__':
data_list = sql_script("SELECT "
"`案例描述`,`操作步骤`,`操作描述`,`操作类型`,`定位方法`,`定位元素`,`元素值`FROM loan_test WHERE `案例编号` "
"= '%s' AND `操作步骤` = '%s';" % ('HYZH001', 'STEP004'), False)
print(data_list)
print(type(data_list))
data_list = list(data_list)
print(type(data_list))
data_list.insert(0, 'aaaaaa')
print(data_list)
# common/config.py
import os
from configobj import ConfigObj
cur_path = os.path.dirname(os.path.realpath(__file__))
parent_path = os.path.dirname(cur_path)
config_ini_path = os.path.join(parent_path, "config/config.ini")
cf = ConfigObj(config_ini_path, encoding="UTF8")
def get_var(key, session="var"):
var = cf[session][key]
return var
def set_var(key, value, session="var"):
cf[session][key] = value
cf.write()
if __name__ == '__main__':
print(config_ini_path)
# common_function_ui.py
from common.config import get_var
from common.common_ui import WebUI
class ZiChan(WebUI):
def open_url(self):
url = get_var("url", "SystemInfo")
self.web_open(url)
def login(self, user, pwd, check):
for case in self.read_case("HYZH001"):
if case in ('STEP001',):
self.run_step("HYZH001", case, user)
continue
if case in ('STEP002',):
self.run_step("HYZH001", case, pwd)
continue
if case in ('STEP004',):
self.run_step("HYZH001", case, check)
continue
self.run_step("HYZH001", case)
def logout(self):
self.web_refresh()
self.run_step("HYZH003", "STEP001")
def menu(self, menu1, menu2=None):
self.run_step("HYZH011", "STEP001", None, menu1)
if menu2 is not None:
self.run_step("HYZH011", "STEP002", None, menu1, menu2)
def assert_False(self):
assert False
if __name__ == '__main__':
zc = ZiChan()
zc.open_url()
zc.login("chaoguan", "654321", "超级管理员")
zc.menu("借款管理", "质检管理")
zc.logout()
zc.web_quit()