logging模块封装
import logging
import os
'''
logging.debug('debug级别,一般用来打印一些调试信息,级别最低')
logging.info('info级别,一般用来打印一些正常的操作信息')
logging.warning('waring级别,一般用来打印警告信息')
logging.error('error级别,一般用来打印一些错误信息')
logging.critical('critical级别,一般用来打印一些致命的错误信息,等级最高')
'''
class MyLog:
def __init__(self):
logging.basicConfig(level=logging.INFO,
filename=os.getcwd() + '/log/test.log',
filemode='a',
format=
'%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
)
self.my_log = logging.getLogger('test')
def error_log(self, value):
self.my_log.error(value)
def warning_log(self, value):
self.my_log.warning(value)
def info_log(self, value):
self.my_log.info(value)
def debug_log(self, value):
self.my_log.debug(value)
def critical_log(self, value):
self.my_log.critical(value)
selenium 动作封装
import configparser
import time
import pyautogui
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.wait import WebDriverWait
from module.log import *
from selenium.webdriver.support.select import Select
my_log = MyLog()
conf = configparser.ConfigParser()
conf.read(os.getcwd() + '/config/config.ini', encoding='utf-8')
class BrowserAction:
def __init__(self):
try:
my_log.info_log("-------------------------测试开始-------------------------")
print("-------------------------测试开始-------------------------")
browser_autotest_tip = conf.get('browser', 'browser_autotest_tip')
browser_stealth = conf.get('browser', 'browser_stealth')
browser_javascript = conf.get('browser', 'browser_javascript')
browser_image = conf.get('browser', 'browser_image')
browser_show = conf.get('browser', 'browser_show')
browser_scrollbar = conf.get('browser', 'browser_scrollbar')
browser_maximized = conf.get('browser', 'browser_maximized')
implicitly_wait = conf.get('browser', 'implicitly_wait')
self.options = webdriver.ChromeOptions()
self.options.add_argument('lang=zh_CN.UTF-8')
self.all_url = os.getcwd() + "/download"
self.prefs = {'profile.default_content_settings.popups': 0,
'download.default_directory': '%s' % self.all_url}
self.options.add_experimental_option('prefs', self.prefs)
if browser_autotest_tip == 'True':
self.options.add_experimental_option(
"excludeSwitches", ['enable-automation'])
self.options.add_argument('--no-sandbox')
self.options.add_argument('--disable-gpu')
if browser_stealth == 'True':
self.options.add_argument('--incognito')
if browser_javascript == 'True':
self.options.add_argument(
'--disable-javascript')
if browser_maximized == 'True':
self.options.add_argument('--start-maximized')
if browser_scrollbar == 'True':
self.options.add_argument(
'--hide-scrollbars')
if browser_image == 'True':
self.options.add_argument(
'blink-settings=imagesEnabled=false')
if browser_show == 'True':
self.options.add_argument('--headless')
self.driver = webdriver.Chrome(executable_path=os.getcwd() + '/driver/chromedriver.exe',
chrome_options=self.options)
self.driver.implicitly_wait(implicitly_wait)
except Exception as e:
my_log.error_log(e)
print(e)
def open_url(self, url):
try:
self.driver.get(url)
my_log.info_log("打开地址:%s" % url)
print("打开地址:%s" % url)
except Exception as e:
my_log.error_log(e)
print(e)
def element_click(self, elementId=None, elementName=None, elementXpath=None):
try:
if elementId is not None:
self.driver.find_element_by_id(elementId).click()
my_log.info_log("鼠标点击元素id:%s" % elementId)
print("鼠标点击元素id:%s" % elementId)
elif elementName is not None:
self.driver.find_element_by_name(elementName).click()
my_log.info_log("鼠标点击元素name:%s" % elementName)
print("鼠标点击元素name:%s" % elementName)
elif elementXpath is not None:
self.driver.find_element_by_xpath(elementXpath).click()
my_log.info_log("鼠标点击元素xpath:%s" % elementXpath)
print("鼠标点击元素xpath:%s" % elementXpath)
except Exception as e:
my_log.error_log(e)
print(e)
def input_value(self, value=None, elementId=None, elementName=None, elementXpath=None):
try:
if elementId is not None:
element = self.driver.find_element_by_id(elementId)
element.clear()
element.clear()
element.clear()
element.send_keys(value)
my_log.info_log("元素id:%s,输入:%s" % (elementId, value))
print("元素id:%s,输入:%s" % (elementId, value))
elif elementName is not None:
element = self.driver.find_element_by_name(elementName)
element.clear()
element.clear()
element.clear()
element.send_keys(value)
my_log.info_log("元素name:%s,输入:%s" % (elementName, value))
print("元素name:%s,输入:%s" % (elementName, value))
elif elementXpath is not None:
element = self.driver.find_element_by_xpath(elementXpath)
element.clear()
element.clear()
element.clear()
element.send_keys(value)
my_log.info_log("元素xpath:%s,输入:%s" % (elementXpath, value))
print("元素xpath:%s,输入:%s" % (elementXpath, value))
except Exception as e:
my_log.error_log(e)
print(e)
def element_clear(self, elementId=None, elementName=None, elementXpath=None):
try:
if elementId is not None:
element = self.driver.find_element_by_id(elementId)
element.clear()
element.clear()
my_log.info_log("清空元素id:%s" % elementId)
print("清空元素id:%s" % elementId)
elif elementName is not None:
element = self.driver.find_element_by_name(elementName)
element.clear()
element.clear()
my_log.info_log("清空元素name:%s" % elementName)
print("清空元素name:%s" % elementName)
elif elementXpath is not None:
element = self.driver.find_element_by_xpath(elementXpath)
element.clear()
element.clear()
my_log.info_log("清空元素xpath:%s" % elementXpath)
print("清空元素xpath:%s" % elementXpath)
except Exception as e:
my_log.error_log(e)
print(e)
def element_double_click(self, elementId=None, elementName=None, elementXpath=None):
try:
if elementId is not None:
self.driver.find_element_by_id(elementId).double_click()
my_log.info_log("鼠标双击元素id:%s" % elementId)
print("鼠标双击元素id:%s" % elementId)
elif elementName is not None:
self.driver.find_element_by_name(elementName).double_click()
my_log.info_log("鼠标双击元素name:%s" % elementName)
print("鼠标双击元素name:%s" % elementName)
elif elementXpath is not None:
self.driver.find_element_by_xpath(elementXpath).double_click()
my_log.info_log("鼠标双击元素xpath:%s" % elementXpath)
print("鼠标双击元素xpath:%s" % elementXpath)
except Exception as e:
my_log.error_log(e)
print(e)
def browser_forward(self):
try:
self.driver.forward()
my_log.info_log("浏览器前进")
print("浏览器前进")
except Exception as e:
my_log.error_log(e)
print(e)
def browser_back(self):
try:
self.driver.back()
my_log.info_log("浏览器后退")
print("浏览器后退")
except Exception as e:
my_log.error_log(e)
print(e)
def browser_maxsize(self):
try:
self.driver.maximize_window()
my_log.info_log("浏览器最大化")
print("浏览器最大化")
except Exception as e:
my_log.error_log(e)
print(e)
def go_to_element_click(self, elementId=None, elementName=None, elementXpath=None):
try:
if elementId is not None:
ele = self.driver.find_element_by_id(elementId)
self.driver.execute_script("arguments[0].click();", ele)
my_log.info_log("滚动到元素id:%s,并点击" % elementId)
print("滚动到元素id:%s,并点击" % elementId)
elif elementName is not None:
ele = self.driver.find_element_by_name(elementName)
self.driver.execute_script("arguments[0].click();", ele)
my_log.info_log("滚动到元素name:%s,并点击" % elementName)
print("滚动到元素name:%s,并点击" % elementName)
elif elementXpath is not None:
ele = self.driver.find_element_by_xpath(elementXpath)
self.driver.execute_script("arguments[0].click();", ele)
my_log.info_log("滚动到元素xpath:%s,并点击" % elementXpath)
print("滚动到元素xpath:%s,并点击" % elementXpath)
except Exception as e:
my_log.error_log(e)
print(e)
def element_right_click(self, elementId=None, elementName=None, elementXpath=None):
try:
if elementId is not None:
self.driver.find_element_by_id(elementId).context_click()
my_log.info_log("鼠标双击元素id:%s" % elementId)
print("鼠标双击元素id:%s" % elementId)
elif elementName is not None:
self.driver.find_element_by_name(elementName).context_click()
my_log.info_log("鼠标双击元素name:%s" % elementName)
print("鼠标双击元素name:%s" % elementName)
elif elementXpath is not None:
self.driver.find_element_by_xpath(elementXpath).context_click()
my_log.info_log("鼠标双击元素xpath:%s" % elementXpath)
print("鼠标双击元素xpath:%s" % elementXpath)
except Exception as e:
my_log.error_log(e)
print(e)
def mouse_move_to_element(self, elementId=None, elementName=None, elementXpath=None):
try:
if elementId is not None:
move = self.driver.find_element_by_id(elementId)
ActionChains(self.driver).move_to_element(move).perform()
ActionChains(self.driver).move_to_element(move).perform()
ActionChains(self.driver).move_to_element(move).perform()
my_log.info_log("鼠标移动到元素id:%s" % elementId)
print("鼠标移动到元素id:%s" % elementId)
elif elementName is not None:
move = self.driver.find_element_by_name(elementName)
ActionChains(self.driver).move_to_element(move).perform()
ActionChains(self.driver).move_to_element(move).perform()
ActionChains(self.driver).move_to_element(move).perform()
my_log.info_log("鼠标移动到元素name:%s" % elementName)
print("鼠标移动到元素name:%s" % elementName)
elif elementXpath is not None:
move = self.driver.find_element_by_xpath(elementXpath)
ActionChains(self.driver).move_to_element(move).perform()
ActionChains(self.driver).move_to_element(move).perform()
ActionChains(self.driver).move_to_element(move).perform()
my_log.info_log("鼠标移动到元素xpath:%s" % elementXpath)
print("鼠标移动到元素xpath:%s" % elementXpath)
except Exception as e:
my_log.error_log(e)
print(e)
def drag_element_to_element(self, startElementId=None, endElementId=None,
startElementName=None, endElementName=None,
startElementXpath=None, endElementXpath=None):
try:
start = None
end = None
if startElementId is not None:
start = self.driver.find_element_by_id(startElementId)
elif startElementName is not None:
start = self.driver.find_element_by_name(startElementName)
elif startElementXpath is not None:
start = self.driver.find_element_by_xpath(startElementXpath)
if endElementId is not None:
end = self.driver.find_element_by_id(endElementId)
elif endElementName is not None:
end = self.driver.find_element_by_name(endElementName)
elif endElementXpath is not None:
end = self.driver.find_element_by_xpath(endElementXpath)
ActionChains(self.driver).drag_and_drop(start, end).perform()
my_log.info_log("将元素 %s 拖动到元素 %s" % (start, end))
print("将元素 %s 拖动到元素 %s" % (start, end))
except Exception as e:
my_log.error_log(e)
print(e)
def time_sleep(self, num):
time.sleep(num)
my_log.info_log("强制等待%s秒" % num)
print("强制等待%s秒" % num)
def wait_element_exist(self, wait_time, elementId=None, elementName=None, elementXpath=None):
try:
if elementId is not None:
WebDriverWait(self.driver, wait_time).until(
self.driver.find_element_by_id(elementId))
my_log.info_log("等待元素出现:%s" % elementId)
print("等待元素出现:%s" % elementId)
elif elementName is not None:
WebDriverWait(self.driver, wait_time).until(
self.driver.find_element_by_name(elementName))
my_log.info_log("等待元素出现:%s" % elementName)
print("等待元素出现:%s" % elementName)
elif elementXpath is not None:
WebDriverWait(self.driver, wait_time).until(
self.driver.find_element_by_xpath(elementXpath))
my_log.info_log("等待元素出现:%s" % elementXpath)
print("等待元素出现:%s" % elementXpath)
except Exception as e:
my_log.error_log(e)
print(e)
def browser_refresh(self):
try:
self.driver.refresh()
my_log.info_log("刷新页面")
print("刷新页面")
except Exception as e:
my_log.error_log(e)
print(e)
def switch_browser_tab(self, num):
try:
all_handles = self.driver.window_handles
self.driver.switch_to.window(all_handles[num - 1])
my_log.info_log("切换到标签页%s" % num)
print("切换到标签页%s" % num)
except Exception as e:
my_log.error_log(e)
print(e)
def scroll_bar_move(self, num):
try:
self.driver.execute_script("window.scrollBy(0, %d)" % num)
my_log.info_log("滚动条移动:%s" % num)
print("滚动条移动:%s" % num)
except Exception as e:
my_log.error_log(e)
print(e)
def close_current_tab(self):
try:
self.driver.close()
my_log.info_log("关闭当前标签页")
print("关闭当前标签页")
except Exception as e:
my_log.error_log(e)
print(e)
def close_browser(self):
try:
self.driver.quit()
my_log.info_log("关闭浏览器")
print("关闭浏览器")
my_log.info_log("-------------------------测试结束-------------------------")
print("-------------------------测试结束-------------------------")
except Exception as e:
my_log.error_log(e)
print(e)
def screen_shot(self, name):
try:
current_time = time.strftime(
'%Y%m%d%H%M%S', time.localtime(time.time()))
self.driver.get_screenshot_as_file(
"images/" + name + '_' + current_time + ".png")
my_log.info_log("保存屏幕截图成功")
print("保存屏幕截图成功")
except Exception as e:
my_log.error_log(e)
print(e)
def element_screen_shot(self, elementId=None, elementName=None, elementXpath=None):
try:
current_time = time.strftime(
'%Y%m%d%H%M%S', time.localtime(time.time()))
if elementId is not None:
element = self.driver.find_element_by_id(elementId)
element.screenshot('images/元素截图_' + current_time + ".png")
elif elementName is not None:
element = self.driver.find_element_by_name(elementName)
element.screenshot('images/元素截图_' + current_time + ".png")
elif elementXpath is not None:
element = self.driver.find_element_by_xpath(elementXpath)
element.screenshot('images/元素截图_' + current_time + ".png")
my_log.info_log("保存元素截图成功")
print("保存元素截图成功")
except Exception as e:
my_log.error_log(e)
print(e)
def point_click(self, x, y):
try:
pyautogui.click(x, y)
my_log.info_log("鼠标点击屏幕坐标点:%s,%s" % (x, y))
print("鼠标点击屏幕坐标点:%s,%s" % (x, y))
except Exception as e:
my_log.error_log(e)
print(e)
def point_double_click(self, x, y):
try:
pyautogui.click(x, y, clicks=2)
my_log.info_log("鼠标双击屏幕坐标点:%s,%s" % (x, y))
print("鼠标双击屏幕坐标点:%s,%s" % (x, y))
except Exception as e:
my_log.error_log(e)
print(e)
def keyboard_click(self, button):
try:
pyautogui.press(button)
my_log.info_log("按下键盘:%s" % button)
print("按下键盘:%s" % button)
except Exception as e:
my_log.error_log(e)
print(e)
def hot_key(self, key1, key2):
try:
pyautogui.hotkey(key1, key2)
my_log.info_log("按下键盘:%s + %s" % (key1, key2))
print("按下键盘:%s + %s" % (key1, key2))
except Exception as e:
my_log.error_log(e)
print(e)
def get_current_page_title(self):
try:
my_log.info_log("获取当前页面标题为:%s" % self.driver.title)
print("获取当前页面标题为:%s" % self.driver.title)
return self.driver.title
except Exception as e:
my_log.error_log(e)
print(e)
def get_element_attributes(self, attributes, elementId=None, elementName=None, elementXpath=None):
try:
value = None
if elementId is not None:
value = self.driver.find_element_by_id(
elementId).get_attribute(attributes)
my_log.info_log("获取元素 %s 属性 %s 值为: %s" % (elementId, attributes, value))
print("获取元素 %s 属性 %s 值为: %s" % (elementId, attributes, value))
elif elementName is not None:
value = self.driver.find_element_by_name(
elementName).get_attribute(attributes)
my_log.info_log("获取元素 %s 属性 %s 值为: %s" % (elementName, attributes, value))
print("获取元素 %s 属性 %s 值为: %s" % (elementName, attributes, value))
elif elementXpath is not None:
value = self.driver.find_element_by_xpath(
elementXpath).get_attribute(attributes)
my_log.info_log("获取元素 %s 属性 %s 值为: %s" % (elementXpath, attributes, value))
print("获取元素 %s 属性 %s 值为: %s" % (elementXpath, attributes, value))
return value
except Exception as e:
my_log.error_log(e)
print(e)
def get_element_text(self, elementId=None, elementName=None, elementXpath=None):
try:
value = None
if elementId is not None:
value = self.driver.find_element_by_id(elementId).text
my_log.info_log("获取元素 %s 文本为: %s" % (elementId, value))
print("获取元素 %s 文本为: %s" % (elementId, value))
elif elementName is not None:
value = self.driver.find_element_by_name(elementName).text
my_log.info_log("获取元素 %s 文本为: %s" % (elementName, value))
print("获取元素 %s 文本为: %s" % (elementName, value))
elif elementXpath is not None:
value = self.driver.find_element_by_xpath(elementXpath).text
my_log.info_log("获取元素 %s 文本为: %s" % (elementXpath, value))
print("获取元素 %s 文本为: %s" % (elementXpath, value))
return value
except Exception as e:
my_log.error_log(e)
print(e)
def find_element(self, elementId=None, elementName=None, elementXpath=None):
try:
result = None
if elementId is not None:
try:
self.driver.find_element_by_id(elementId)
result = True
except:
result = False
my_log.info_log("判断元素 %s 是否存在: %s" % (elementId, result))
print("判断元素 %s 是否存在: %s" % (elementId, result))
elif elementId is not None:
try:
self.driver.find_element_by_name(elementName)
result = True
except:
result = False
my_log.info_log("判断元素 %s 是否存在: %s" % (elementName, result))
print("判断元素 %s 是否存在: %s" % (elementName, result))
elif elementId is not None:
try:
self.driver.find_element_by_xpath(elementXpath)
result = True
except:
result = False
my_log.info_log("判断元素 %s 是否存在: %s" % (elementXpath, result))
print("判断元素 %s 是否存在: %s" % (elementXpath, result))
return result
except Exception as e:
my_log.error_log(e)
print(e)
def alert_click(self):
try:
alert = self.driver.switch_to.alert
alert.accept()
my_log.info_log("点击浏览器弹框确定按钮")
print("点击浏览器弹框确定按钮")
except Exception as e:
my_log.error_log(e)
print(e)
def alert_close(self):
try:
alert = self.driver.switch_to.alert
alert.dismiss()
my_log.info_log("点击浏览器弹框取消按钮")
print("点击浏览器弹框取消按钮")
except Exception as e:
my_log.error_log(e)
print(e)
def alert_input(self, key):
try:
prompt = Alert(self.driver)
prompt.send_keys(key)
my_log.info_log("浏览器弹框输入:%s" % key)
print("浏览器弹框输入:%s" % key)
except Exception as e:
my_log.error_log(e)
print(e)
def get_alert_text(self):
try:
my_log.info_log("获取浏览器弹窗内容:%s" % self.driver.switch_to.alert.text)
print("获取浏览器弹窗内容:%s" % self.driver.switch_to.alert.text)
return self.driver.switch_to.alert.text
except Exception as e:
my_log.error_log(e)
print(e)
def enter_iframe(self, elementId=None, elementName=None, elementXpath=None):
try:
if elementId is not None:
self.driver.switch_to.frame(
self.driver.find_element_by_id(elementId))
my_log.info_log("进入iframe:%s" % elementId)
print("进入iframe:%s" % elementId)
elif elementName is not None:
self.driver.switch_to.frame(
self.driver.find_element_by_name(elementName))
my_log.info_log("进入iframe:%s" % elementName)
print("进入iframe:%s" % elementName)
elif elementXpath is not None:
self.driver.switch_to.frame(
self.driver.find_element_by_xpath(elementXpath))
my_log.info_log("进入iframe:%s" % elementXpath)
print("进入iframe:%s" % elementXpath)
except Exception as e:
my_log.error_log(e)
print(e)
def out_iframe(self):
try:
self.driver.switch_to.parent_frame()
my_log.info_log("跳出iframe")
print("跳出iframe")
except Exception as e:
my_log.error_log(e)
print(e)
def element_is_enabled(self, elementId=None, elementName=None, elementXpath=None):
try:
element = None
if elementId is not None:
element = self.driver.find_element_by_id(elementId)
elif elementName is not None:
element = self.driver.find_element_by_name(elementName)
elif elementXpath is not None:
element = self.driver.find_element_by_xpath(elementXpath)
my_log.info_log("判断元素是否可用:%s" % element.is_enabled())
print("判断元素是否可用:%s" % element.is_enabled())
return element.is_enabled()
except Exception as e:
my_log.error_log(e)
print(e)
def select_list(self, text, elementId=None, elementName=None, elementXpath=None):
try:
if elementId is not None:
opt = self.driver.find_element_by_id(elementId)
Select(opt).select_by_visible_text(text)
my_log.info_log("选择下拉列表选项:%s" % text)
print("选择下拉列表选项:%s" % text)
elif elementName is not None:
opt = self.driver.find_element_by_name(elementName)
Select(opt).select_by_visible_text(text)
my_log.info_log("选择下拉列表选项:%s" % text)
print("选择下拉列表选项:%s" % text)
elif elementXpath is not None:
opt = self.driver.find_element_by_xpath(elementXpath)
Select(opt).select_by_visible_text(text)
my_log.info_log("选择下拉列表选项:%s" % text)
print("选择下拉列表选项:%s" % text)
except Exception as e:
my_log.error_log(e)
print(e)
def excute_script(self, jscode):
try:
self.driver.execute_script(jscode)
my_log.info_log("执行javascript代码: %s" % jscode)
print("执行javascript代码: %s" % jscode)
except Exception as e:
my_log.error_log(e)
print(e)