selenium+python實(shí)現(xiàn)基本自動化測試的示例代碼
打開命令控制符輸入:pip install -U selenium
火狐瀏覽器安裝firebug:www.firebug.com,調(diào)試所有網(wǎng)站語言,調(diào)試功能
Selenium IDE 是嵌入到Firefox 瀏覽器中的一個(gè)插件,實(shí)現(xiàn)簡單的瀏覽器操 作的錄制與回放功能,IDE 錄制的腳本可以可以轉(zhuǎn)換成多種語言,從而幫助我們快速的開發(fā)腳本,下載地址:https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/
如何使用IDE錄制腳本:點(diǎn)擊seleniumIDE——點(diǎn)擊錄制——開始錄制——錄制完成后點(diǎn)擊文件Export Test Case——python/unittest/Webdriver——保存;
安裝python安裝的時(shí)候,推薦選擇“Add exe to path”,將會自動添加Python的程序到環(huán)境變量中。然后可以在命令行輸入 python -V 檢測安裝的Python版本。
瀏覽器內(nèi)殼:IE、chrome、FireFox、Safari
1、webdriver:用unittest框架寫自動化用例(setUp:前置條件,tearDown清場)import unittestfrom selenium import webdriverclass Ranzhi(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() #選擇火狐瀏覽器 def test_ranzhi(self): pass def tearDown(self): self.driver.quit()#退出瀏覽器2、斷言,檢查跳轉(zhuǎn)的網(wǎng)頁是否和實(shí)際一致
斷言網(wǎng)址時(shí)需注意是否為偽靜態(tài)(PATH_INFO)或者GET,前者采用路徑傳參數(shù)(sys/user-creat.html),后者通過字符查詢傳參數(shù)(sys/index.php?m=user&f=index)
當(dāng)采用不同方式校驗(yàn)網(wǎng)址會發(fā)現(xiàn)變化。
self.assertEqual('http://localhost:8080/ranzhi/www/s/index.php?m=index&f=index', self.driver.current_url, '登錄跳轉(zhuǎn)失敗')
WebDriver提供了一系列的元素定位方法。常見的有以下幾種:id,name,link text,partial link text,xpath,css seletor,class,tag.
self.driver.find_element_by_xpath(’//*[@id='s-menu-superadmin']/button’).click() self.driver.find_element_by_id(’account’).send_keys(’admin’) self.driver.find_element_by_link_text(u’退出’).click()
定位元素需注意的問題:
a.時(shí)間不夠,采用兩種方式(self.implicitly_wait(30),sleep(2))
b.函數(shù)嵌套(<iframe></iframe>)
# 進(jìn)入嵌套 self.driver.switch_to.frame(’iframe-superadmin’) #退出嵌套 self.driver.switch_to.default_content()
c.flash,驗(yàn)證碼(關(guān)閉驗(yàn)證碼或使用萬能碼)
d.xpath問題:最好采用最簡xpath,當(dāng)xpath中出現(xiàn)li[10]等時(shí)需注意,有時(shí)頁面定位會出現(xiàn)問題
4、采用CSV存數(shù)據(jù)CSV:以純文本形式存儲表格數(shù)據(jù)(數(shù)字和文本),CSV文件由任意數(shù)目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最常見的是逗號或制表符。大量程序都支持某種CSV變體,至少是作為一種可選擇的輸入/輸出格式。
melody101,melody101,m,1,3,123456,@qq.com melody102,melody101,f,2,5,123456,@qq.com melody103,melody101,m,3,2,123456,@qq.com
import csv# 讀取CSV文件到user_list字典類型變量中user_list = csv.reader(open('list_to_user.csv', 'r'))# 遍歷整個(gè)user_listfor user in user_list: sleep(2) self.logn_in(’admin’, ’admin’) sleep(2) # 讀取一行csv,并分別賦值到user_to_add 中 user_to_add = {’account’: user[0], ’realname’: user[1], ’gender’: user[2], ’dept’: user[3], ’role’: user[4], ’password’: user[5], ’email’: user[0] + user[6]} self.add_user(user_to_add)5、對下拉列表的定位采用select標(biāo)簽
from selenium.webdriver.support.select import Select# 選擇部門dp =self.driver.find_element_by_id(’dept’)Select(dp).select_by_index(user[’dept’])# 選擇角色Select(self.driver.find_element_by_id(’role’)).select_by_index(user[’role’])6、模塊化代碼
需要對自動化重復(fù)編寫的腳本進(jìn)行重構(gòu)(refactor),將重復(fù)的腳本抽取出來,放到指定的代碼文件中,作為共用的功能模塊。使用模塊化代碼注意需倒入該代碼。
#模塊化代碼后引用,需導(dǎo)入代碼模塊from ranzhi_lib import RanzhiLibself.lib = RanzhiLib(self.driver)# 點(diǎn)擊后臺管理self.lib.click_admin_app()sleep(2)# 點(diǎn)擊添加用戶self.lib.click_add_user()# 添加用戶self.lib.add_user(user_to_add)sleep(1)# 退出self.lib.logn_out()sleep(2)
class RanzhiLib(): # 構(gòu)造方法 def __init__(self, driver): self.driver = driver
7、自定義函數(shù)運(yùn)行的先后順序:完整的單元測試很少只執(zhí)行一個(gè)測試用例,開發(fā)人員通常都需要編寫多個(gè)測試用例才能對某一軟件功能進(jìn)行比較完整的測試,這些相關(guān)的測試用例稱為一個(gè)測試用例集,在PyUnit中是用TestSuite類來表示,采用unittest.TestSuite()。
PyUnit使用TestRunner類作為測試用例的基本執(zhí)行環(huán)境,來驅(qū)動整個(gè)單元測試過程。Python開發(fā)人員在進(jìn)行單元測試時(shí)一般不直接使用TestRunner類,而是使用其子類TextTestRunner來完成測試。
詳情請查看:http://www.ibm.com/developerworks/cn/linux/l-pyunit/
# 構(gòu)造測試集suite = unittest.TestSuite()suite.addTest(RanzhiTest('test_login'))suite.addTest(RanzhiTest('test_ranzhi'))# 執(zhí)行測試runner = unittest.TextTestRunner()runner.run(suite)
以下代碼為登錄“然之系統(tǒng)”,進(jìn)入添加用戶,循環(huán)添加用戶并檢測添加成功,再退出的過程。以下程序分別為主程序,模塊化程序,執(zhí)行程序,CSV文件
import csvimport unittestfrom time import sleepfrom selenium import webdriver# 模塊化代碼后引用需導(dǎo)入代碼模塊from ranzhi_lib import RanzhiLibclass Ranzhi(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.lib = RanzhiLib(self.driver) # 主函數(shù) def test_ranzhi(self): # 讀取CSV文件到user_list字典類型變量中 user_list = csv.reader(open('list_to_user.csv', 'r')) # 遍歷整個(gè)user_list for user in user_list: sleep(2) self.lib.logn_in(’admin’, ’admin’) sleep(2) # 斷言 self.assertEqual('http://localhost:8080/ranzhi/www/sys/index.html', self.driver.current_url, ’登錄跳轉(zhuǎn)失敗’) # 讀取一行csv,并分別賦值到user_to_add 中 user_to_add = {’account’: user[0], ’realname’: user[1], ’gender’: user[2], ’dept’: user[3], ’role’: user[4], ’password’: user[5], ’email’: user[0] + user[6]} # 點(diǎn)擊后臺管理 self.lib.click_admin_app() # 進(jìn)入嵌套 self.lib.driver.switch_to.frame(’iframe-superadmin’) sleep(2) # 點(diǎn)擊添加用戶 self.lib.click_add_user() # 添加用戶 self.lib.add_user(user_to_add) # 退出嵌套 self.driver.switch_to.default_content() sleep(1) # 退出 self.lib.logn_out() sleep(2) # 用新賬號登錄 self.lib.logn_in(user_to_add[’account’], user_to_add[’password’]) sleep(2) self.lib.logn_out() sleep(2) def tearDown(self): self.driver.quit()
from time import sleepfrom selenium.webdriver.support.select import Selectclass RanzhiLib(): # 構(gòu)造方法 def __init__(self, driver): self.driver = driver # 模塊化添加用戶 def add_user(self, user): driver = self.driver # 添加用戶名 ac = driver.find_element_by_id(’account’) ac.send_keys(user[’account’]) # 真實(shí)姓名 rn = driver.find_element_by_id(’realname’) rn.clear() rn.send_keys(user[’realname’]) # 選擇性別 if user[’gender’] == ’m’: driver.find_element_by_id(’gender2’).click() elif user[’gender’] == ’f’: driver.find_element_by_id(’gender1’).click() # 選擇部門 dp = driver.find_element_by_id(’dept’) Select(dp).select_by_index(user[’dept’]) # 選擇角色 role = driver.find_element_by_id(’role’) Select(role).select_by_index(user[’role’]) # 輸入密碼 pwd1 = driver.find_element_by_id(’password1’) pwd1.clear() pwd1.send_keys(user[’password’]) pwd2 = driver.find_element_by_id(’password2’) pwd2.send_keys(user[’password’]) # 輸入郵箱 em = driver.find_element_by_id(’email’) em.send_keys(user[’email’]) # 點(diǎn)擊保存 driver.find_element_by_id(’submit’).click() sleep(2) # 登錄賬號 def logn_in(self, name, password): driver = self.driver driver.get(’http://localhost:8080/ranzhi/www’) sleep(2) driver.find_element_by_id(’account’).clear() driver.find_element_by_id(’account’).send_keys(name) driver.find_element_by_id(’password’).clear() driver.find_element_by_id(’password’).send_keys(password) driver.find_element_by_id(’submit’).click() sleep(2) # 退出賬號 def logn_out(self): self.driver.find_element_by_id(’start’).click() sleep(4) self.driver.find_element_by_link_text(u’退出’).click() sleep(3) # 點(diǎn)擊后臺管理 def click_admin_app(self): self.driver.find_element_by_xpath(’//*[@id='s-menu-superadmin']/button’).click() sleep(1) def click_add_user(self): self.driver.find_element_by_xpath(’//*[@id='shortcutBox']/div/div[1]/div/a/h3’).click() sleep(3)
import unittestfrom ranzhi import Ranzhiclass RanzhiTestRunner(): def run_tests(self): suite = unittest.TestSuite() suite.addTest(Ranzhi(’test_ranzhi’)) runner = unittest.TextTestRunner() runner.run(suite)if __name__ == '__main__': ranzhi_test_runner = RanzhiTestRunner() ranzhi_test_runner.run_tests()
melody109,melody101,m,1,3,123456,@qq.commelody106,melody101,f,2,5,123456,@qq.commelody107,melody101,m,3,2,123456,@qq.com
到此這篇關(guān)于selenium+python實(shí)現(xiàn)基本自動化測試的示例代碼的文章就介紹到這了,更多相關(guān)selenium自動化測試內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)2. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼3. 如何使用瀏覽器擴(kuò)展篡改網(wǎng)頁中的JS 文件4. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除5. jsp中sitemesh修改tagRule技術(shù)分享6. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)7. 爬取今日頭條Ajax請求8. JavaWeb Servlet中url-pattern的使用9. ASP基礎(chǔ)知識VBScript基本元素講解10. jsp EL表達(dá)式詳解
