selenium基礎框架的封裝(Python版)

1、經常使用函數的封裝web

  在使用selenium作web自動化測試的過程當中,常常會碰到各類各樣的問題,好比:瀏覽器

  一、頁面加載比較慢時,selenium查找元素拋出異常,致使腳本運行停止app

  二、寫完腳本後發現代碼邏輯不夠清晰,維護困難,下降測試效率框架

  因此頗有必要對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()

 2、使用封裝過的框架和pageobject模式設計登陸腳本ui

 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")

 3、打開博客園新聞、博問等子頁面腳本設計url

 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()

 4、其餘spa

  按照此模式博友可根據公司具體的業務來實現自動化測試腳本,本文拋磚引玉,若有其餘更好的想法能夠一塊兒探討設計

                                                      Thanks!

相關文章
相關標籤/搜索