def report(): if len(sys.argv) > 1: report_name = BaseUtil().get_root_path( ) + '/report/' + sys.argv[1] + '_result.html' else: now = time.strftime("%Y-%m-%d_%H_%M_%S_") report_name = BaseUtil().get_root_path( ) + '/report/' + now + 'result.html' # report_name = GlobalVar.get_root_path() + '/report/' + 'result.html' return report_name
def __init__(self, logger): """ 将日志保存到指定的路径文件中 指定日志的级别,以及调用文件 """ # 创建logger文件 self.logger = logging.getLogger(logger) self.logger.setLevel(logging.DEBUG) # 创建一个handle,用来写入日志文件 now = time.strftime("%Y-%m-%d_%H_%M_%S") # log_path = os.path.dirname(os.path.abspath('.')) + '/logs/' log_path = BaseUtil().get_root_path() + '/logs/' log_name = log_path + now + '.log' file_handle = logging.FileHandler(log_name, encoding="utf-8") file_handle.setLevel(logging.INFO) # 创建一个handle,用来输入日志到控制台 control_handle = logging.StreamHandler() control_handle.setLevel(logging.INFO) # 将输出的hangdle格式进行转换 formatter = logging.Formatter( '%(asctime)s - %(levelname)s - %(message)s') file_handle.setFormatter(formatter) control_handle.setFormatter(formatter) # 给logger添加handle self.logger.addHandler(file_handle) self.logger.addHandler(control_handle)
def test_homepage(self): # file_path = os.path.dirname(os.path.abspath('.')) file_path = BaseUtil().get_root_path() excel_path = file_path + "/files/website.xlsx" sheet_name = "Sheet1" wb = openpyxl.load_workbook(excel_path) sheet = wb[sheet_name] for i in range(1, sheet.max_row + 1, 1): web_url = sheet.cell(row=i, column=1).value try: if web_url is None: break else: self.driver.get(web_url) title = self.driver.title if "Error" in title: logger.info(web_url + " " + "ERROR") BasePage().get_screent_img() elif title == web_url[7:]: logger.info(web_url + " " + "ERROR") BasePage().get_screent_img() else: logger.info(web_url + " " + "homepage is OK") except Exception as e: logger.info(e)
class BrowserDriver(object): file_path = BaseUtil().get_root_path() name_path = file_path + '/yaml/browser.yaml' with open(name_path, 'r') as f: temp = yaml.load(f.read()) system_name = temp['OperatingSystem']['systemName'] logger.info("当前系统为" + system_name) browser = temp['browserType']['browserName'] logger.info("选择的浏览器为: %s 浏览器" % browser) url = temp['WebUrl']['URL'] if system_name == 'Linux': firefox_driver_path = file_path + '/driver/linux/geckodriver' chrome_driver_path = file_path + '/driver/linux/chromedriver' opera_driver_path = file_path + '/driver/linux/operadriver.exe' elif system_name == 'MacOS': safari_driver_path = file_path + '/driver/MacOS/safaridriver' firefox_driver_path = file_path + '/driver/MacOS/geckodriver' chrome_driver_path = file_path + '/driver/MacOS/chromedriver' opera_driver_path = file_path + '/driver/MacOS/operadriver' elif system_name == 'Windows': firefox_driver_path = file_path + '/driver/Windows/geckodriver.exe' chrome_driver_path = file_path + '/driver/Windows/chromedriver.exe' ie_driver_path = file_path + '/driver/Windows/IEDriverServer.exe' edge_driver_path = file_path + '/driver/Windows/IEDriverServer.exe' def __init__(self, driver): self.driver = driver def open_browser(self, driver): if self.browser == "Firefox": driver = webdriver.Firefox(service_log_path=self.file_path + "/logs/" + "geckodriver.log") logger.info("启动火狐浏览器") elif self.browser == "Chrome": driver = webdriver.Chrome(self.chrome_driver_path) logger.info("启动谷歌浏览器") elif self.browser == "IE": driver = webdriver.Ie(self.ie_driver_path) logger.info("启动IE浏览器") elif self.browser == "Edge": driver = webdriver.Edge(self.edge_driver_path) logger.info("启动Edge浏览器") elif self.browser == "Opera": driver = webdriver.Opera(self.opera_driver_path) logger.info("启动Opera浏览器") elif self.browser == "Safari": driver = webdriver.Safari(self.safari_driver_path) logger.info("启动Safari浏览器") # driver.maximize_window() driver.set_window_size(1440, 900) logger.info("全屏当前窗口") driver.implicitly_wait(5) logger.info("设置5秒隐式等待时间") driver.get(self.url) logger.info("打开URL: %s" % self.url) return driver def quit_browser(self): logger.info("关闭浏览器") self.driver.quit()
def create_suite(): suites = unittest.TestSuite() # 测试集 test_dir = BaseUtil().get_root_path() + '/testcase/' discover = unittest.defaultTestLoader.discover(start_dir=test_dir, pattern='Test_*.py', top_level_dir=None) for test_case in discover: suites.addTests(test_case) return suites
def login(self): for i in range(10): if self.driver.current_url != "http://om.motionglobal.com/index/dashboard": usr = BaseUtil().get_config_value("OM_Account", "username") pwd = BaseUtil().get_config_value("OM_Account", "password") self.clear(self.username) self.send_key(self.username, usr) self.clear(self.password) self.send_key(self.password, pwd) text = self.image_to_string(self.verify) self.clear(self.verify_code) self.send_key(self.verify_code, text) time.sleep(1) try: self.click(self.login_button) time.sleep(2) self.alert_accept() except Exception as e: print(e) else: print("Had try %s times" % i) print("Login successfully") break
class MobileDriver(object): file_path = BaseUtil().get_root_path() name_path = file_path + '/yaml/browser.yaml' with open(name_path, 'r') as f: temp = yaml.load(f.read()) system_name = temp['OperatingSystem']['systemName'] logger.info("当前系统为" + system_name) browser = temp['browserType']['browserName'] logger.info("选择的浏览器为: %s 浏览器" % browser) url = temp['WebUrl']['URL'] logger.info("打开的URL为: %s" % url) if system_name == 'Linux': chrome_driver_path = file_path + '/driver/linux/chromedriver' elif system_name == 'MacOS': chrome_driver_path = file_path + '/driver/MacOS/chromedriver' elif system_name == 'Windows': chrome_driver_path = file_path + '/driver/Windows/chromedriver.exe' def __init__(self, driver): self.driver = driver def open_mobile_browser(self): mobile_emulation = {'deviceName': 'iPhone X'} chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option('mobileEmulation', mobile_emulation) driver = webdriver.Chrome(self.chrome_driver_path, options=chrome_options) driver.implicitly_wait(30) driver.set_window_size(1440, 900) logger.info("Open mobile model successfully") driver.get(self.url) return driver def quit_browser(self): self.driver.quit() logger.info("退出浏览器")
import os import sys import time import unittest curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(curPath)[0] sys.path.append(rootPath) from util.BaseUtil import BaseUtil from runner.HTMLTestRunner import HTMLTestRunner test_suits = unittest.TestSuite() test_dir = BaseUtil().get_root_path() + '/testcase/' discover = unittest.defaultTestLoader.discover(test_dir, pattern='test_*.py', top_level_dir=None) for test_case in discover: test_suits.addTests(test_case) if __name__ == "__main__": now = time.strftime("%Y-%m-%d %H_%M_%S") filename = BaseUtil().get_root_path() + '/report/' + now + '_result.html' fp = open(filename, 'wb') runner = HTMLTestRunner(stream=fp, title='接口自动化测试结果', description='测试环境') runner.run(test_suits) fp.close()
class BasePage(object): path = BaseUtil().get_root_path() def __init__(self, driver): """ :param driver:打开浏览器驱动 """ self.driver = driver self.accept_next_alert = True def get_page_title(self): logger.info("当前页面的title为: %s" % self.driver.title) return self.driver.title def find_element(self, *locator): try: # 元素可见时,返回查找到的元素;以下入参为元组的元素,需要加* WebDriverWait(self.driver, 30, 0.5).until( lambda driver: driver.find_element(*locator).is_displayed()) return self.driver.find_element(*locator) except NoSuchElementException: logger.warning('Can not find element: %s' % locator[1]) raise except TimeoutException: logger.warning('Can not find element: %s' % locator[1]) def get_screent_img(self): """将页面截图下来""" file_path = self.path + '/screenshots/' now = time.strftime("%Y-%m-%d_%H_%M_%S") screen_name = file_path + now + '.png' try: self.driver.get_screenshot_as_file(screen_name) logger.info("页面已截图,截图的路径在项目: /screenshots路径下") except NameError as ne: logger.error("失败截图 %s" % ne) self.get_screent_img() def click(self, locator): logger.info('Click element by %s: %s...' % (locator[0], locator[1])) try: self.find_element(*locator).click() # time.sleep(1) except AttributeError as e: logger.error("无法点击元素: %s" % e) def clear(self, locator): """输入文本框清空操作""" element = self.find_element(*locator) try: element.clear() logger.info('清空文本框内容') except NameError as ne: logger.error("Failed to clear in input box with %s" % ne) self.get_screent_img() def send_key(self, locator, text): logger.info('Clear input-box: %s...' % locator[1]) self.find_element(*locator).clear() # time.sleep(1) logger.info('Input element by %s: %s...' % (locator[0], locator[1])) logger.info('Input: %s' % text) try: self.find_element(*locator).send_keys(text) # time.sleep(1) except Exception as e: logger.error("Failed to type in input box with %s" % e) self.get_screent_img() def move_to_element(self, locator): """ 鼠标悬停操作 Usage: element = ("id","xxx") driver.move_to_element(element) """ element = self.find_element(*locator) ActionChains(self.driver).move_to_element(element).perform() def back(self): """ 浏览器返回窗口 """ self.driver.back() logger.info('返回上一个页面') def forward(self): """ 浏览器前进下一个窗口 """ self.driver.forward() logger.info('前进到下一个页面') def wait(self, seconds): self.driver.implicitly_wait(seconds) logger.info("等待 %d 秒" % seconds) def close(self): """ 关闭浏览器 """ try: self.driver.close() logger.info('关闭浏览器窗口') except NameError as ne: logger.error("关闭浏览器窗口失败 %s" % ne) def quit(self): """ 退出浏览器 """ self.driver.quit() def get_title(self): """获取title""" return self.driver.title def get_url(self): """获取当前的url""" return self.driver.current_url def get_text(self, locator): """获取文本""" element = self.find_element(*locator) return element.text def get_attribute(self, locator, name): """获取属性""" element = self.find_element(*locator) return element.get_attribute(name) def js_execute(self, js): """执行js""" return self.driver.execute_script(js) def js_focus_element(self, locator): """聚焦元素""" target = self.find_element(*locator) self.driver.execute_script("arguments[0].scrollIntoView(true);", target) def js_scroll_top(self): """滚动到顶部""" js = "window.scrollTo(0,0)" self.driver.execute_script(js) def js_scroll_end(self): """滚动到底部""" js = "window.scrollTo(0,document.body.scrollHeight)" self.driver.execute_script(js) def select_by_index(self, locator, index): """通过索引,index是索引第几个,从0开始""" element = self.find_element(*locator) Select(element).select_by_index(index) def select_by_value(self, locator, value): """通过value属性""" element = self.find_element(*locator) Select(element).select_by_value(value) def select_by_text(self, locator, text): """通过文本值定位""" element = self.find_element(*locator) Select(element).select_by_value(text) def is_text_in_element(self, locator, text, timeout=10): """判断文本在元素里,没定位到元素返回False,定位到元素返回判断结果布尔值""" try: result = WebDriverWait(self.driver, timeout, 1).until( EC.text_to_be_present_in_element(locator, text)) except TimeoutException: logger.info("元素没有定位到:" + str(locator)) return False else: return result def is_text_in_value(self, locator, value, timeout=10): """ 判断元素的value值,没定位到元素返回false,定位到返回判断结果布尔值 result = driver.text_in_element(element, text) """ try: result = WebDriverWait(self.driver, timeout, 0.5).until( EC.text_to_be_present_in_element_value(locator, value)) except TimeoutException: print("元素没定位到:" + str(locator)) return False else: return result def is_title(self, title, timeout=10): """判断title完全等于""" result = WebDriverWait(self.driver, timeout, 0.5).until(EC.title_is(title)) return result def is_title_contains(self, title, timeout=10): """判断title包含""" result = WebDriverWait(self.driver, timeout, 0.5).until(EC.title_contains(title)) return result def is_selected(self, locator, timeout=10): """判断元素被选中,返回布尔值,""" result = WebDriverWait(self.driver, timeout, 0.5).until( EC.element_located_to_be_selected(locator)) return result def is_selected_be(self, locator, selected=True, timeout=10): """判断元素的状态,selected是期望的参数true/False 返回布尔值""" result = WebDriverWait(self.driver, timeout, 0.5).until( EC.element_located_selection_state_to_be(locator, selected)) return result def is_alert_present(self, timeout=10): """判断页面是否有alert, 有返回alert(注意这里是返回alert,不是True) 没有返回False""" result = WebDriverWait(self.driver, timeout, 0.5).until(EC.alert_is_present()) return result def alert_accept(self): """接受alert""" try: alert = self.driver.switch_to_alert() logger.info(alert.text) alert.accept() except UnexpectedAlertPresentException as e: print(e) except NoAlertPresentException as e1: print(e1) def is_visibility(self, locator, timeout=10): try: """元素可见返回本身,不可见返回Fasle""" result = WebDriverWait(self.driver, timeout, 0.5).until( EC.visibility_of_element_located(locator)) return result except TimeoutException as e: logger.info(e) def is_invisibility(self, locator, timeout=10): """元素可见返回本身,不可见返回True,没找到元素也返回True""" result = WebDriverWait(self.driver, timeout, 0.5).until( EC.visibility_of_element_located(locator)) return result def is_clickable(self, locator, timeout=10): """元素可以点击is_enabled返回本身,不可点击返回Fasle""" result = WebDriverWait(self.driver, timeout, 0.5).until(EC.element_to_be_clickable(locator)) return result def is_located(self, locator, timeout=10): """判断元素有没被定位到(并不意味着可见),定位到返回element,没定位到返回False""" result = WebDriverWait(self.driver, timeout, 0.5).until( EC.presence_of_element_located(locator)) return result def set_element_wait(self, wait_time, locator): WebDriverWait(self.driver, wait_time, 0.5).until(EC.presence_of_element_located(locator)) def upload_file(self, locator, file_path): """上传文件""" try: self.find_element(*locator).send_keys(file_path) time.sleep(1) except Exception as e: logger.error("Failed to upload file %s" % e) self.get_screent_img() def switch_handle(self, title_name): """根据窗口title切换窗口""" all_handles = self.driver.window_handles() for handle in all_handles: if self.driver.title.find(title_name) == -1: self.driver.switch_to_window(handle) else: print("Can't find the handle") def is_element_present(self, how, what): try: self.driver.find_element(by=how, value=what) except NoSuchElementException as e: logger.error("Element is not present. %s" % e) return False return True def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to.alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = True def get_attribute_text(self, locator): try: text_content = self.find_element( *locator).get_attribute('textContent') except Exception as e: logger.error("Failed to upload file %s" % e) self.get_screent_img() return text_content def _get_dynamic_binary_image(self, file_dir, file_name): filename = self.path + '/out_img/' + file_name.split( '.')[0] + '-binary.jpg' img_name = file_dir + '/' + file_name print(img_name) img = cv2.imread(img_name) # 灰值化 img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化 img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, 1) cv2.imwrite(filename, img) return img # 去除边框 def clear_border(self, img, img_name): filename = self.path + '/out_img/' + img_name.split( '.')[0] + '-clearBorder.jpg' h, w = img.shape[:2] for y in range(0, w): for x in range(0, h): if y < 2 or y > w - 2: img[x, y] = 255 if x < 2 or x > h - 2: img[x, y] = 255 cv2.imwrite(filename, img) return img # 干扰线降噪 def interference_line(self, img, img_name): filename = self.path + '/out_img/' + img_name.split( '.')[0] + '-interferenceline.jpg' h, w = img.shape[:2] # !!!opencv矩阵点是反的 # img[1,2] 1:图片的高度,2:图片的宽度 for y in range(1, w - 1): for x in range(1, h - 1): count = 0 if img[x, y - 1] > 245: count = count + 1 if img[x, y + 1] > 245: count = count + 1 if img[x - 1, y] > 245: count = count + 1 if img[x + 1, y] > 245: count = count + 1 if count > 2: img[x, y] = 255 cv2.imwrite(filename, img) return img # 点降噪 def interference_point(self, img, img_name, x=0, y=0): """ 9邻域框,以当前点为中心的田字框,黑点个数 :param x: :param y: :return: """ filename = self.path + '/out_img/' + img_name.split( '.')[0] + '-interferencePoint.jpg' # 判断图片的长宽度下限 cur_pixel = img[x, y] # 当前像素点的值 height, width = img.shape[:2] for y in range(0, width - 1): for x in range(0, height - 1): if y == 0: # 第一行 if x == 0: # 左上顶点,4邻域 # 中心点旁边3个点 sum = int(cur_pixel) \ + int(img[x, y + 1]) \ + int(img[x + 1, y]) \ + int(img[x + 1, y + 1]) if sum <= 2 * 245: img[x, y] = 0 elif x == height - 1: # 右上顶点 sum = int(cur_pixel) \ + int(img[x, y + 1]) \ + int(img[x - 1, y]) \ + int(img[x - 1, y + 1]) if sum <= 2 * 245: img[x, y] = 0 else: # 最上非顶点,6邻域 sum = int(img[x - 1, y]) \ + int(img[x - 1, y + 1]) \ + int(cur_pixel) \ + int(img[x, y + 1]) \ + int(img[x + 1, y]) \ + int(img[x + 1, y + 1]) if sum <= 3 * 245: img[x, y] = 0 elif y == width - 1: # 最下面一行 if x == 0: # 左下顶点 # 中心点旁边3个点 sum = int(cur_pixel) \ + int(img[x + 1, y]) \ + int(img[x + 1, y - 1]) \ + int(img[x, y - 1]) if sum <= 2 * 245: img[x, y] = 0 elif x == height - 1: # 右下顶点 sum = int(cur_pixel) \ + int(img[x, y - 1]) \ + int(img[x - 1, y]) \ + int(img[x - 1, y - 1]) if sum <= 2 * 245: img[x, y] = 0 else: # 最下非顶点,6邻域 sum = int(cur_pixel) \ + int(img[x - 1, y]) \ + int(img[x + 1, y]) \ + int(img[x, y - 1]) \ + int(img[x - 1, y - 1]) \ + int(img[x + 1, y - 1]) if sum <= 3 * 245: img[x, y] = 0 else: # y不在边界 if x == 0: # 左边非顶点 sum = int(img[x, y - 1]) \ + int(cur_pixel) \ + int(img[x, y + 1]) \ + int(img[x + 1, y - 1]) \ + int(img[x + 1, y]) \ + int(img[x + 1, y + 1]) if sum <= 3 * 245: img[x, y] = 0 elif x == height - 1: # 右边非顶点 sum = int(img[x, y - 1]) \ + int(cur_pixel) \ + int(img[x, y + 1]) \ + int(img[x - 1, y - 1]) \ + int(img[x - 1, y]) \ + int(img[x - 1, y + 1]) if sum <= 3 * 245: img[x, y] = 0 else: # 具备9领域条件的 sum = int(img[x - 1, y - 1]) \ + int(img[x - 1, y]) \ + int(img[x - 1, y + 1]) \ + int(img[x, y - 1]) \ + int(cur_pixel) \ + int(img[x, y + 1]) \ + int(img[x + 1, y - 1]) \ + int(img[x + 1, y]) \ + int(img[x + 1, y + 1]) if sum <= 4 * 245: img[x, y] = 0 cv2.imwrite(filename, img) return img def image_to_string(self, locator): # 截取当前网页,该网页有我们需要的验证码 self.driver.save_screenshot(self.path + "/screenshots/" + "All.png") img_element = self.find_element(*locator) # 获取验证码x,y轴坐标 location = img_element.location # 获取验证码的长宽 size = img_element.size rangle = ( int(location['x']), int(location['y']), int(location['x'] + size['width']), # 写成我们需要截取的位置坐标 int(location['y'] + size['height'])) i = Image.open(self.path + "/screenshots/" + "All.png") # 使用Image的crop函数,从截图中再次截取我们需要的区域 result = i.crop(rangle) result.save(self.path + "/screenshots/" + "result.png") rgb_im = result.convert('RGB') rgb_im.save(self.path + "/screenshots/" + "result.jpg") img = Image.open(self.path + "/screenshots/" + "result.jpg") img.convert("L") # file_dir = self.path + '/screenshots' # img_name = "result.jpg" # img = self._get_dynamic_binary_image(file_dir, img_name) # img = self.clear_border(img, img_name) # img = self.interference_line(img, img_name) # img = self.interference_point(img, img_name) verify_code = pytesseract.image_to_string(img).strip() logger.info("verify code is: " + verify_code) return verify_code
#!/usr/bin/python3 # -*- coding: utf-8 -*- import pymysql from util.BaseUtil import BaseUtil from util.logger import Logger logger = Logger(logger="MysqlDB").get_log() db_host = BaseUtil().get_config_value("MySql", "host") port = BaseUtil().get_config_value("MySql", "port") username = BaseUtil().get_config_value("MySql", "username") password = BaseUtil().get_config_value("MySql", "password") db_name = BaseUtil().get_config_value("MySql", "db_name") charset = BaseUtil().get_config_value("MySql", "charset") class MysqlDB: def __init__(self): try: self.connection = pymysql.connect(host=db_host, port=int(port), user=username, password=password, database=db_name, charset=charset) except pymysql.err.OperationalError as e: logger.info("Mysql Error %d: %s" % (e.args[0], e.args[1])) def execute_sql(self, sql_querry): con = self.connection
import pymysql from util.BaseUtil import BaseUtil host = BaseUtil().get_config_value('DB', 'host') port = BaseUtil().get_config_value('DB', 'port') user = BaseUtil().get_config_value('DB', 'username') password = BaseUtil().get_config_value('DB', 'password') db_name = BaseUtil().get_config_value('DB', 'db_name') charset = BaseUtil().get_config_value('DB', 'charset') class DB: def __init__(self): try: self.connection = pymysql.connect(host=host, port=int(port), user=user, password=password, db=db_name, charset=charset) except pymysql.err.OperationalError as e: print("Mysql Error %d: %s" % (e.args[0], e.args[1])) def execute_sql(self, query): array = [] con = self.connection cur = con.cursor() try: cur.execute(query) result = cur.fetchall() con.commit()
import cv2 import os from PIL import Image from pytesseract import * from fnmatch import fnmatch from queue import Queue from util.BaseUtil import BaseUtil path = BaseUtil().get_root_path() def clear_border(img, img_name): """去除边框 """ filename = path + '/out_img/' + img_name.split('.')[0] + '-clearBorder.jpg' h, w = img.shape[:2] for y in range(0, w): for x in range(0, h): # if y ==0 or y == w -1 or y == w - 2: if y < 4 or y > w - 4: img[x, y] = 255 # if x == 0 or x == h - 1 or x == h - 2: if x < 4 or x > h - 4: img[x, y] = 255 cv2.imwrite(filename, img) return img def interference_line(img, img_name):