国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

selenium+python實現基本自動化測試的示例代碼

瀏覽:5日期:2022-06-29 08:38:04
安裝selenium

打開命令控制符輸入:pip install -U selenium

火狐瀏覽器安裝firebug:www.firebug.com,調試所有網站語言,調試功能

Selenium IDE 是嵌入到Firefox 瀏覽器中的一個插件,實現簡單的瀏覽器操 作的錄制與回放功能,IDE 錄制的腳本可以可以轉換成多種語言,從而幫助我們快速的開發腳本,下載地址:https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/

如何使用IDE錄制腳本:點擊seleniumIDE——點擊錄制——開始錄制——錄制完成后點擊文件Export Test Case——python/unittest/Webdriver——保存;

安裝python

安裝的時候,推薦選擇“Add exe to path”,將會自動添加Python的程序到環境變量中。然后可以在命令行輸入 python -V 檢測安裝的Python版本。

瀏覽器內殼: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、斷言,檢查跳轉的網頁是否和實際一致

斷言網址時需注意是否為偽靜態(PATH_INFO)或者GET,前者采用路徑傳參數(sys/user-creat.html),后者通過字符查詢傳參數(sys/index.php?m=user&f=index)

當采用不同方式校驗網址會發現變化。

self.assertEqual('http://localhost:8080/ranzhi/www/s/index.php?m=index&f=index', self.driver.current_url, '登錄跳轉失敗')

selenium+python實現基本自動化測試的示例代碼

3、定位元素,在html里面,元素具有各種各樣的屬性。我們可以通過這樣唯一區別其他元素的屬性來定位到這個元素.

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.時間不夠,采用兩種方式(self.implicitly_wait(30),sleep(2))

b.函數嵌套(<iframe></iframe>)

# 進入嵌套 self.driver.switch_to.frame(’iframe-superadmin’) #退出嵌套 self.driver.switch_to.default_content()

c.flash,驗證碼(關閉驗證碼或使用萬能碼)

d.xpath問題:最好采用最簡xpath,當xpath中出現li[10]等時需注意,有時頁面定位會出現問題

4、采用CSV存數據

CSV:以純文本形式存儲表格數據(數字和文本),CSV文件由任意數目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最常見的是逗號或制表符。大量程序都支持某種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'))# 遍歷整個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標簽

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、模塊化代碼

需要對自動化重復編寫的腳本進行重構(refactor),將重復的腳本抽取出來,放到指定的代碼文件中,作為共用的功能模塊。使用模塊化代碼注意需倒入該代碼。

#模塊化代碼后引用,需導入代碼模塊from ranzhi_lib import RanzhiLibself.lib = RanzhiLib(self.driver)# 點擊后臺管理self.lib.click_admin_app()sleep(2)# 點擊添加用戶self.lib.click_add_user()# 添加用戶self.lib.add_user(user_to_add)sleep(1)# 退出self.lib.logn_out()sleep(2)

class RanzhiLib(): # 構造方法 def __init__(self, driver): self.driver = driver

7、自定義函數運行的先后順序:完整的單元測試很少只執行一個測試用例,開發人員通常都需要編寫多個測試用例才能對某一軟件功能進行比較完整的測試,這些相關的測試用例稱為一個測試用例集,在PyUnit中是用TestSuite類來表示,采用unittest.TestSuite()。

PyUnit使用TestRunner類作為測試用例的基本執行環境,來驅動整個單元測試過程。Python開發人員在進行單元測試時一般不直接使用TestRunner類,而是使用其子類TextTestRunner來完成測試。

詳情請查看:http://www.ibm.com/developerworks/cn/linux/l-pyunit/

# 構造測試集suite = unittest.TestSuite()suite.addTest(RanzhiTest('test_login'))suite.addTest(RanzhiTest('test_ranzhi'))# 執行測試runner = unittest.TextTestRunner()runner.run(suite)

以下代碼為登錄“然之系統”,進入添加用戶,循環添加用戶并檢測添加成功,再退出的過程。以下程序分別為主程序,模塊化程序,執行程序,CSV文件

import csvimport unittestfrom time import sleepfrom selenium import webdriver# 模塊化代碼后引用需導入代碼模塊from ranzhi_lib import RanzhiLibclass Ranzhi(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.lib = RanzhiLib(self.driver) # 主函數 def test_ranzhi(self): # 讀取CSV文件到user_list字典類型變量中 user_list = csv.reader(open('list_to_user.csv', 'r')) # 遍歷整個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, ’登錄跳轉失敗’) # 讀取一行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.lib.click_admin_app() # 進入嵌套 self.lib.driver.switch_to.frame(’iframe-superadmin’) sleep(2) # 點擊添加用戶 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(): # 構造方法 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’]) # 真實姓名 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’]) # 點擊保存 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) # 點擊后臺管理 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

到此這篇關于selenium+python實現基本自動化測試的示例代碼的文章就介紹到這了,更多相關selenium自動化測試內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 久久亚洲欧洲日产国码 | 久久久久久久亚洲精品一区 | 亚洲国产成人综合 | 久久精品二区 | 欧美成人福利 | 国产精品毛片 | 日韩欧美在线一区二区三区 | 女人张开腿让男人 | 有码在线| 欧美专区一区 | 亚洲精品一区二区三区四区手机版 | 亚洲一级高清在线中文字幕 | 免费观看国产网址你懂的 | 国产在线成人一区二区 | 99视频精品免费99在线 | 亚洲国产一成人久久精品 | 玖玖国产在线观看 | 亚洲精品人成网在线播放影院 | 手机在线精品视频每日更新 | 日韩一级片视频 | 美女黄色片免费 | 亚洲欧美中文日韩二区一区 | 天天综合天天看夜夜添狠狠玩 | 亚洲一区欧美二区 | 成人在线第一页 | 亚洲欧洲日本天天堂在线观看 | 性刺激欧美三级在线现看中文 | 亚洲综合色自拍一区 | 欧美成人免费在线 | 久久免费资源 | 美女视频黄的免费视频网页 | 日本a级片免费观看 | 日本亚洲免费 | 精品久久久久久久久久久 | 性生活视频网 | 亚洲天码中文字幕第一页 | 99视频在线精品免费 | 亚洲一区成人 | 99久久国内精品成人免费 | 另类毛片 | 国产中文字幕在线观看 |