import time
driver = webdriver.firefox()
driver.implicitly_wait(30)
driver.get("")
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
time.sleep(5)
driver.quit()
從上述**來看,我們所能做的就是定位到元素,然後進行鍵盤輸入或滑鼠動作。就這個小程式而已,維護起來看起來是很容易的。但隨著時間的遷移,測試套件將持續的增長。指令碼也將變得越來越臃腫龐大。如果變成我們需要維護10個頁面,100個頁面,甚至1000個呢?那頁面元素的任何改變都會讓我們的指令碼維護變得繁瑣複雜,而且變得耗時易出錯。
那怎麼解決呢?
在自動化測試中,引入了page object model(pom):頁面物件模式來解決,pom能讓我們的測試**變得可讀性更好,高可維護性,高復用性。
下圖為非pom和pom對比圖:
下面我們看看pom的**目錄組織示例:
pom提供了一種在ui層操作、業務流程與驗證分離的模式,這使得測試**變得更加清晰和高可讀性
物件庫與用例分離,使得我們更好的復用物件,甚至能與不同的工具進行深度結合應用
可復用的頁面方法**會變得更加優化
更加有效的命名方式使得我們更加清晰的知道方法所操作的ui元素。例如我們要回到首頁,方法名命名為: gotohomepage(),通過方法名即可清晰的知道具體的功能實現。
# basepage.py**如下
# _*_ coding:utf-8 _*_
__author__ = '苦葉子'
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
# pages基類
class page(object):
"""page基類,所有page都應該繼承該類
"""def __init__(self, driver, base_url=u""):
self.driver = driver
self.base_url = base_url
self.timeout = 30
def find_element(self, *loc):
return self.driver.find_element(*loc)
def input_text(self, loc, text):
self.find_element(*loc).send_keys(text)
def click(self, loc):
self.find_element(*loc).click()
def get_title(self):
return self.driver.title
# searchpage.py **如下
# _*_ coding:utf-8 _*_
__author__ = '苦葉子'
import sys
from selenium.webdriver.common.by import by
from pages.basepage import page
reload(sys)
sys.setdefaultencoding("utf-8")
class searchpage(page):
# 元素集
# 搜尋輸入框
print u"開啟首頁: ", self.base_url
self.driver.get(self.base_url)
def input_search_text(self, text=u"開源優測"):
print u"輸入搜尋關鍵字: 開源優測 "
self.input_text(self.search_input, text)
def click_search_btn(self):
self.click(self.search_button)
# testsearchpage.py**如下
# _*_ coding:utf-8 _*_
__author__ = '苦葉子'
import unittest
import sys
from selenium import webdriver
from pages.searchpage import searchpage
reload(sys)
sys.setdefaultencoding("utf-8")
class testsearchpage(unittest.testcase):
def setup(self):
self.driver = webdriver.ie()
def testsearch(self):
driver = self.driver
url = u""
# 搜尋文字
text = u"開源優測"
# 期望驗證的標題
# 輸入 搜尋詞
search_page.input_search_text(text)
search_page.click_search_btn()
# 驗證標題
self.assertequal(search_page.get_title(), assert_title)
def teardown(self):
self.driver.quit()
# 主入口程式**如下
# _*_ coding:utf-8 _*_
__author__ = '苦葉子'
import unittest
import sys
from common import htmltestrunner
from testcase.testsearchpage import testsearchpage
reload(sys)
sys.setdefaultencoding("utf-8")
if __name__ == '__main__':
testunit = unittest.testsuite()
testunit.addtest(testsearchpage('testsearch'))
# 定義報告輸出路徑
htmlpath = u"page_demo_report.html"
fp = file(htmlpath, "wb")
runner = htmltestrunner.htmltestrunner(stream=fp,
description=u"測試用例結果")
runner.run(testunit)
fp.close()
按照如圖所示組織**結構,輸入如上**,執行以下命令執行,會在當前目錄生成測試報告:
python main.py
最後做個總結,所有**請手動輸入,不要直接拷貝。再次對pom進行小結
pom是selenium webdriver自動化測試實踐物件庫設計模式
pom使得測試指令碼更易於維護
pom通過物件庫方式進一步優化了元素、用例、資料的維護組織
Python Selenium環境搭建
安裝python 設定 python 的環境變數 安裝目錄 安裝目錄 scripts 使用 pip安裝 selenium pip install selenium 安裝完python pip工具,在安裝目錄的 scripts 目錄下。在 dos下直接執行 pip install selenium 即...
Python Selenium 學習筆記
1 判斷元素是否存在 try driver.find element.xx a true except a false if a true print 元素存在 elif a false print 元素不存在 2 判斷元素是否顯示 driver.find element by id outputb...
Python Selenium錯誤小結
因為要使用web應用,所以開始用起了,selenium包,安裝倒是挺容易的,但就是出了很多bug。filenotfounderror winerror 2 系統找不到指定的檔案。通過錯誤反饋發現是要把該軟體加到路徑裡面,但是,設定了系統環境變數後發現還是不行,最後,使用了乙個非常原始的方法 brow...