一、常用函數(shù)的封裝
在使用selenium做web自動(dòng)化測(cè)試的過程中,經(jīng)常會(huì)碰到各種各樣的問題,比如:
1、頁(yè)面加載比較慢時(shí),selenium查找元素拋出異常,導(dǎo)致腳本運(yùn)行中止
2、寫完腳本后發(fā)現(xiàn)代碼邏輯不夠清晰,維護(hù)困難,降低測(cè)試效率
所以很有必要對(duì)selenium進(jìn)行封裝使框架更適合公司的需求,下面是基類的封裝,主要使用單例模式,可以將業(yè)務(wù)邏輯代碼存放在不同的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 使用單例模式將類設(shè)置為運(yùn)行時(shí)只有一個(gè)實(shí)例,在其他Python類中使用基類時(shí),
14 可以創(chuàng)建多個(gè)對(duì)象,保證所有的對(duì)象都是基于一個(gè)瀏覽器
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 啟動(dòng)瀏覽器
24 :param url: 測(cè)試地址
25 :param driver_name: 在啟動(dòng)時(shí)設(shè)置瀏覽器的類型
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 這里添加了一個(gè)OVER_TIME作為查找元素的超時(shí)次數(shù),根據(jù)系統(tǒng)的實(shí)際情況設(shè)置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 查找狀態(tài)為displayed的元素集合,當(dāng)查找一類元素時(shí),
63 經(jīng)常出現(xiàn)有些元素是不可見的情況,此函數(shù)屏蔽那些不可見的元素
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模式設(shè)計(jì)登錄腳本
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")
三、打開博客園新聞、博問等子頁(yè)面腳本設(shè)計(jì)
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()
四、其他
按照此模式博友可根據(jù)公司具體的業(yè)務(wù)來實(shí)現(xiàn)自動(dòng)化測(cè)試腳本,本文拋磚引玉,如有其他更好的想法可以一起探討