python selenium 基础框架
Posted yye_2010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python selenium 基础框架相关的知识,希望对你有一定的参考价值。
base_page.py
1 # coding=utf-8 2 import time 3 from selenium.common.exceptions import NoSuchElementException 4 import os.path 5 from unittest3.framework.logger import Logger 6 7 # create a logger instance 8 logger = Logger(logger="BasePage").getlog() 9 10 11 class BasePage(object): 12 """ 13 定义一个页面基类,让所有页面都继承这个类,封装一些常用的页面操作方法到这个类 14 """ 15 16 def __init__(self, driver): 17 self.driver = driver 18 19 # quit browser and end testing 20 21 def quit_browser(self): 22 self.driver.quit() 23 24 # 浏览器前进操作 25 26 def forward(self): 27 self.driver.forward() 28 logger.info("Click forward on current page.") 29 30 # 浏览器后退操作 31 32 def back(self): 33 self.driver.back() 34 logger.info("Click back on current page.") 35 36 # 隐式等待 37 38 def wait(self, seconds): 39 self.driver.implicitly_wait(seconds) 40 logger.info("wait for %d seconds." % seconds) 41 42 # 点击关闭当前窗口 43 44 def close(self): 45 try: 46 self.driver.close() 47 logger.info("Closing and quit the browser.") 48 except NameError as e: 49 logger.error("Failed to quit the browser with %s" % e) 50 51 # 保存图片 52 53 def get_windows_img(self): 54 """ 55 在这里我们把file_path这个参数写死,直接保存到我们项目根目录的一个文件夹.\\Screenshots下 56 """ 57 file_path = os.path.dirname(os.path.abspath(\'.\')) + \'/screenshots/\' 58 rq = time.strftime(\'%Y%m%d%H%M\', time.localtime(time.time())) 59 screen_name = file_path + rq + \'.png\' 60 try: 61 self.driver.get_screenshot_as_file(screen_name) 62 logger.info("Had take screenshot and save to folder : /screenshots") 63 except NameError as e: 64 logger.error("Failed to take screenshot! %s" % e) 65 self.get_windows_img() 66 67 # 定位元素方法 68 69 def find_element(self, selector): 70 """ 71 这个地方为什么是根据=>来切割字符串,请看页面里定位元素的方法 72 submit_btn = "id=>su" 73 login_lnk = "xpath => //*[@id=\'u1\']/a[7]" # 百度首页登录链接定位 74 如果采用等号,结果很多xpath表达式中包含一个=,这样会造成切割不准确,影响元素定位 75 :param selector: 76 :return: element 77 """ 78 element = \'\' 79 if \'=>\' not in selector: 80 return self.driver.find_element_by_id(selector) 81 selector_by = selector.split(\'=>\')[0] 82 selector_value = selector.split(\'=>\')[1] 83 84 if selector_by == "i" or selector_by == \'id\': 85 try: 86 element = self.driver.find_element_by_id(selector_value) 87 logger.info("Had find the element \\\' %s \\\' successful " 88 "by %s via value: %s " % (element.text, selector_by, selector_value)) 89 except NoSuchElementException as e: 90 logger.error("NoSuchElementException: %s" % e) 91 self.get_windows_img() # take screenshot 92 elif selector_by == "n" or selector_by == \'name\': 93 element = self.driver.find_element_by_name(selector_value) 94 elif selector_by == "c" or selector_by == \'class_name\': 95 element = self.driver.find_element_by_class_name(selector_value) 96 elif selector_by == "l" or selector_by == \'link_text\': 97 element = self.driver.find_element_by_link_text(selector_value) 98 elif selector_by == "p" or selector_by == \'partial_link_text\': 99 element = self.driver.find_element_by_partial_link_text(selector_value) 100 elif selector_by == "t" or selector_by == \'tag_name\': 101 element = self.driver.find_element_by_tag_name(selector_value) 102 elif selector_by == "x" or selector_by == \'xpath\': 103 try: 104 element = self.driver.find_element_by_xpath(selector_value) 105 logger.info("Had find the element \\\' %s \\\' successful " 106 "by %s via value: %s " % (element.text, selector_by, selector_value)) 107 except NoSuchElementException as e: 108 logger.error("NoSuchElementException: %s" % e) 109 self.get_windows_img() 110 elif selector_by == "s" or selector_by == \'selector_selector\': 111 element = self.driver.find_element_by_css_selector(selector_value) 112 else: 113 raise NameError("Please enter a valid type of targeting elements.") 114 115 return element 116 117 # 输入 118 119 def type(self, selector, text): 120 121 el = self.find_element(selector) 122 el.clear() 123 try: 124 el.send_keys(text) 125 logger.info("Had type \\\' %s \\\' in inputBox" % text) 126 except NameError as e: 127 logger.error("Failed to type in input box with %s" % e) 128 self.get_windows_img() 129 130 # 清除文本框 131 132 def clear(self, selector): 133 134 el = self.find_element(selector) 135 try: 136 el.clear() 137 logger.info("Clear text in input box before typing.") 138 except NameError as e: 139 logger.error("Failed to clear in input box with %s" % e) 140 self.get_windows_img() 141 142 # 点击元素 143 144 def click(self, selector): 145 146 el = self.find_element(selector) 147 try: 148 el.click() 149 logger.info("The element \\\' %s \\\' was clicked." % el.text) 150 except NameError as e: 151 logger.error("Failed to click the element with %s" % e) 152 153 # 或者网页标题 154 155 def get_page_title(self): 156 logger.info("Current page title is %s" % self.driver.title) 157 return self.driver.title 158 159 @staticmethod 160 def sleep(seconds): 161 time.sleep(seconds) 162 logger.info("Sleep for %d seconds" % seconds)
browser_engine.py
1 # coding=utf-8 2 from selenium import webdriver 3 4 5 class BrowserEngine(object): 6 """ 7 定义一个浏览器引擎类,根据browser_type的值去,控制启动不同的浏览器,这里主要是IE,Firefox, Chrome 8 9 """ 10 11 def __init__(self, driver): 12 self.driver = driver 13 14 browser_type = "Chrome" # maybe Firefox, Chrome, IE 15 16 def get_browser(self): 17 """ 18 通过if语句,来控制初始化不同浏览器的启动,默认是启动Chrome 19 :return: driver 20 """ 21 22 if self.browser_type == \'Firefox\': 23 driver = webdriver.Firefox() 24 elif self.browser_type == \'Chrome\': 25 driver = webdriver.Chrome() 26 elif self.browser_type == \'IE\': 27 driver = webdriver.Ie() 28 else: 29 driver = webdriver.Chrome() 30 31 # driver.maximize_window() 32 driver.implicitly_wait(10) 33 driver.get("https://www.baidu.com") 34 return driver
logger.py
1 # _*_ coding: utf-8 _*_ 2 import logging 3 import os.path 4 import time 5 6 7 class Logger(object): 8 def __init__(self, logger): 9 """ 10 指定保存日志的文件路径,日志级别,以及调用文件 11 将日志存入到指定的文件中 12 :param logger: 13 """ 14 # 创建一个logger 15 self.logger = logging.getLogger(logger) 16 self.logger.setLevel(logging.DEBUG) 17 18 # 创建一个handler,用于写入日志文件 19 rq = time.strftime(\'%Y%m%d%H%M\', time.localtime(time.time())) 20 log_path = os.path.dirname(os.getcwd()) + \'/Logs/\' 21 log_name = log_path + rq + \'.log\' 22 fh = logging.FileHandler(log_name) 23 fh.setLevel(logging.INFO) 24 25 # 再创建一个handler,用于输出到控制台 26 ch = logging.StreamHandler() 27 ch.setLevel(logging.INFO) 28 29 # 定义handler的输出格式 30 formatter = logging.Formatter(\'%(asctime)s - %(name)s - %(levelname)s - %(message)s\') 31 fh.setFormatter(formatter) 32 ch.setFormatter(formatter) 33 34 # 给logger添加handler 35 self.logger.addHandler(fh) 36 self.logger.addHandler(ch) 37 38 def getlog(self): 39 return self.logger
baidu_homepage.py
# coding=utf-8 from unittest3.framework.base_page import BasePage class HomePage(BasePage): input_box = "id=>kw" search_submit_btn = "xpath=>//*[@id=\'su\']" def type_search(self, text): self.type(self.input_box, text) def send_submit_btn(self): self.click(self.search_submit_btn)
测试类代码 baidu_search.py
# coding=utf-8 import time import unittest from selenium import webdriver from unittest3.page_object.baidu_homepage import HomePage from unittest3.framework.browser_engine import BrowserEngine class BaiduSearch(unittest.TestCase): def setUp(self): """ 测试固件的setUp()的代码,主要是测试的前提准备工作 :return: """ # 添加如下代码,可运行 # ------------------------------------------- # browse = BrowserEngine(self) # self.driver = webdriver.Firefox() # self.driver.get(\'https://www.baidu.com\') #------------------------------------------- # 修改代码可以执行 browse = BrowserEngine(self) self.driver=browse.get_browser() def tearDown(self): """ 测试结束后的操作,这里基本上都是关闭浏览器 :return: """ self.driver.quit() def test_baidu_search(self): """ 这里一定要test开头,把测试逻辑代码封装到一个test开头的方法里。 :return: """ homepage = HomePage(self.driver) homepage.type_search(\'selenium\') # 调用页面对象中的方法 homepage.send_submit_btn() # 调用页面对象类中的点击搜索按钮方法 time.sleep(2) homepage.get_windows_img() # 调用基类截图方法 try: assert \'selenium\' in homepage.get_page_title() # 调用页面对象继承基类中的获取页面标题方法 print(\'Test Pass.\') except Exception as e: print(\'Test Fail.\', format(e)) if __name__ == \'__main__\': unittest.main()
以上是关于python selenium 基础框架的主要内容,如果未能解决你的问题,请参考以下文章
小白学 Python 爬虫(28):自动化测试框架 Selenium 从入门到放弃(下)