一、常用函数的封装
在使用selenium做web自动化测试的过程中,经常会碰到各种各样的问题,比如:
1、页面加载比较慢时,selenium查找元素抛出异常,导致脚本运行中止
2、写完脚本后发现代码逻辑不够清晰,维护困难,降低测试效率
所以很有必要对selenium进行封装使框架更适合公司的需求,下面是基类的封装,主要使用单例模式,可以将业务逻辑代码存放在不同的Python文件中
注:BASE_URL为博客园的登录地址
1 # -*- coding:utf-8 -*- 2 from selenium import webdriver 3 import time 4 5 OVER_TIME = 5 6 BASE_URL = "https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F" 7 8 9 class Driver(object): 10 11 def __new__(cls, *args, **kw): 12 """ 13 使用单例模式将类设置为运行时只有一个实例,在其他Python类中使用基类时, 14 可以创建多个对象,保证所有的对象都是基于一个浏览器 15 """ 16 if not hasattr(cls, '_instance'): 17 orig = super(Driver, cls) 18 cls._instance = orig.__new__(cls, *args, **kw) 19 return cls._instance 20 21 def start(self, url=BASE_URL, driver_name="Chrome"): 22 """ 23 启动浏览器 24 :param url: 测试地址 25 :param driver_name: 在启动时设置浏览器的类型 26 :return: 27 """ 28 if driver_name == "Firefox": 29 self.driver = webdriver.Firefox() 30 elif driver_name == "Ie": 31 self.driver = webdriver.Ie() 32 else: 33 self.driver = webdriver.Chrome() 34 self.driver.implicitly_wait(OVER_TIME) 35 self.driver.get(url) 36 self.driver.maximize_window() 37 38 def get_url(self): 39 """返回浏览器的地址""" 40 return BASE_URL 41 42 def find_element(self, by, value): 43 """ 44 这里添加了一个OVER_TIME作为查找元素的超时次数,根据系统的实际情况设置OVER_TIME的大小 45 """ 46 for i in range(OVER_TIME): 47 try: 48 return self.driver.find_element(by=by, value=value) 49 except Exception, e: 50 print e 51 52 def find_elements(self, by, value): 53 """与find_element一致""" 54 for i in range(OVER_TIME): 55 try: 56 return self.driver.find_elements(by=by, value=value) 57 except Exception, e: 58 print e 59 60 def find_display_elements(self, by, value): 61 """ 62 查找状态为displayed的元素集合,当查找一类元素时, 63 经常出现有些元素是不可见的情况,此函数屏蔽那些不可见的元素 64 """ 65 for i in range(OVER_TIME): 66 try: 67 elements = self.driver.find_elements(by=by, value=value) 68 num = elements.__len__() 69 except Exception, e: 70 print e 71 time.sleep(1) 72 if num >= 1: 73 break 74 display_element = [] 75 # 将可见的元素放到列表中, 并返回 76 for j in range(num): 77 element = elements.__getitem__(j) 78 if element.is_displayed(): 79 display_element.append(element) 80 return display_element 81 82 def is_element_present(self, By, Value): 83 """判断元素是否存在""" 84 try: 85 self.driver.find_element(by=By, value=Value) 86 return True 87 except Exception, e: 88 print e 89 return False 90 91 def close(self): 92 self.driver.close() 93 94 def quit(self): 95 u"""退出浏览器""" 96 self.driver.quit() 97 98 if __name__ == "__main__": 99 page = Driver() 100 page.start()
二、使用封装过的框架和pageobject模式设计登录脚本
1 # -*- coding:utf-8 -*- 2 from webdriver import Driver 3 from selenium.webdriver.common.by import By 4 5 6 class Login(object): 7 8 def __init__(self, user_name="xxx", pwd="xxx"): 9 self.page = Driver() 10 page = self.page 11 page.start(url=page.get_url()) 12 self.user_name = page.find_element(By.ID, "input1") 13 self.pwd = page.find_element(By.ID, "input2") 14 self.login_btn = page.find_element(By.ID, "signin") 15 self.login(user_name, pwd) 16 17 def login(self, user_name, pwd): 18 self.user_name.clear() 19 self.user_name.send_keys(user_name) 20 self.pwd.clear() 21 self.pwd.send_keys(pwd) 22 self.login_btn.click() 23 24 25 if __name__ == "__main__": 26 Login("xxx", "xxx")
三、打开博客园新闻、博问等子页面脚本设计
1 # -*- coding:utf-8 -*- 2 from webdriver import Driver 3 from login import Login 4 from selenium.webdriver.common.by import By 5 6 7 class SubPage(object): 8 9 def __init__(self): 10 self.page = Driver() 11 page = self.page 12 self.home_page = page.find_element(By.LINK_TEXT, u"园子") 13 self.news_page = page.find_element(By.LINK_TEXT, u"新闻") 14 self.q_page = page.find_element(By.LINK_TEXT, u"博问") 15 self.ing_page = page.find_element(By.LINK_TEXT, u"闪存") 16 17 def open_home_page(self): 18 self.home_page.click() 19 20 def open_news_page(self): 21 self.news_page.click() 22 23 def open_q_page(self): 24 self.q_page.click() 25 26 def open_ing_page(self): 27 self.ing_page.click() 28 29 30 if __name__ == '__main__': 31 Login("xxx", "xxx") 32 sub = SubPage() 33 sub.open_q_page()
四、其他
按照此模式博友可根据公司具体的业务来实现自动化测试脚本,本文抛砖引玉,如有其他更好的想法可以一起探讨
Thanks!