python+Selenium自動(dòng)化測試——輸入,點(diǎn)擊操作
這是我的第一個(gè)真正意思上的自動(dòng)化腳本。
1、練習(xí)的測試用例為:
打開百度首頁,搜索“胡歌”,然后檢索列表,有無“胡歌的新浪微博”這個(gè)鏈接 2、在寫腳本之前,需要明確測試的步驟,具體到每個(gè)步驟需要做什么,既拆分測試場景,考慮好之后,再去寫腳本。
此測試場景拆分如下:
1)啟動(dòng)Chrome瀏覽器
2)打開百度首頁,https://www.baidu.com
3)定位搜索輸入框,輸入框元素XPath表達(dá)式://*[@id=”kw”]
4)定位搜索提交按鈕(百度一下)://*[@id=”su”]
5)在搜索框輸入“胡歌”,點(diǎn)擊百度一下按鈕
6)在搜索結(jié)果列表判斷是否存在“胡歌的新浪微博”這個(gè)鏈接
7)退出瀏覽器,結(jié)束測試
【注】chrome獲取XPath路徑步驟如下:
1)在你打開的網(wǎng)頁(如:百度首頁),按F12,彈出如下窗口
2)點(diǎn)擊左上角箭頭按鈕(或Ctrl + Shift + C),此時(shí)可以在頁面上移動(dòng)光標(biāo),查看對應(yīng)的代碼,如移動(dòng)到百度搜索框,顯示如下:
點(diǎn)擊一下,對應(yīng)代碼就會(huì)選中
然后,右擊copy?>copy path 復(fù)制到XPath路徑。
3、代碼如下:
import timefrom selenium import webdriver’’’測試用例:打開百度首頁,搜索“胡歌”,然后檢索列表,有無“胡歌的新浪微博”這個(gè)鏈接場景拆分: 1)啟動(dòng)Chrome瀏覽器 2) 打開百度首頁,https://www.baidu.com 3)定位搜索輸入框,輸入框元素XPath表達(dá)式://*[@id='kw'] 4)定位搜索提交按鈕(百度一下)://*[@id='su'] 5)在搜索框輸入“胡歌”,點(diǎn)擊百度一下按鈕 6)在搜索結(jié)果列表判斷是否存在“胡歌的新浪微博”這個(gè)鏈接 7)退出瀏覽器,結(jié)束測試’’’driver = webdriver.Chrome()driver.maximize_window()driver.implicitly_wait(8) # 設(shè)置隱式等待時(shí)間driver.get('https://www.baidu.com') # 地址欄里輸入網(wǎng)址driver.find_element_by_xpath(’//*[@id='kw']’).send_keys('胡歌') # 搜索框輸入胡歌driver.find_element_by_xpath(’//*[@id='su']’).click() # 點(diǎn)擊百度一下按鈕time.sleep(2) # 等待2秒# 通過元素XPath來確定該元素是否顯示在結(jié)果列表,從而判斷“壁紙”這個(gè)鏈接是否顯示在結(jié)果列表# find_element_by_link_text當(dāng)找不到此鏈接時(shí)報(bào)錯(cuò),程序停止driver.find_element_by_link_text(’胡歌的新浪微博’).is_displayed()driver.quit()
補(bǔ)充知識(shí):python + selenium自動(dòng)化測試--頁面操作
1、刷新當(dāng)前頁面
.refresh()
# 刷新當(dāng)前頁面driver.refresh()
2、獲取本頁面的URL
.current_url
用處:
一般URL可以幫助我們判斷跳轉(zhuǎn)的頁面是否正確,或者URL中部分字段可以作為我們自動(dòng)化測試腳本期待結(jié)果的一部分。
print(driver.current_url)
3、頁面標(biāo)題
獲取當(dāng)前頁面標(biāo)題
.title
# 獲取當(dāng)前頁面標(biāo)題顯示的字段print(driver.title)
斷言頁面標(biāo)題
# 1) 包含判斷# assert:斷言,聲稱try: assert '百度一下' in driver.title print('斷言測試成功.')except Exception as e: print('斷言失敗.',format(e))# 2) 完全相等判斷if '百度一下,你就知道' == driver.title: print('成功.')else: print('失敗.')print(driver.title)
4、新建標(biāo)簽頁
用js實(shí)現(xiàn)如下:
try: # 新標(biāo)簽頁,此處用js實(shí)現(xiàn),在有些博客上顯示使用 # driver.find_element_by_tag_name(’body’).send_keys(Keys.CONTROL, ’t’) # 我這測試無效,原因不知,于是采用如下方法 js = 'window.open(’http://www.acfun.cn/’)' driver.execute_script(js) # 切換到新的窗口 handles = driver.window_handles # 獲取窗口句柄 driver.switch_to.window(handles[-1]) # 切換到最后一個(gè)既最新打開的窗口 # 先切換窗口再打開新網(wǎng)址,才是在新窗口打開網(wǎng)址,不然還是在原來的百度頁面打開此網(wǎng)址 driver.get(’http://map.baidu.com/’)except Exception as e: print('發(fā)現(xiàn)異常,',format(e))
5、頁面前進(jìn)、后退
前進(jìn): .forward()
后退: .back()
driver.get('https://www.baidu.com')time.sleep(2)’’’前進(jìn),后退’’’elem_news = driver.find_element_by_link_text('新聞').click() # 點(diǎn)擊進(jìn)入新聞time.sleep(2)driver.back() # 后退到百度首頁time.sleep(2)driver.forward() # 從百度前進(jìn)到新聞頁time.sleep(2)
6、獲取瀏覽器版本號
.capabilities[‘version’]
# 獲取瀏覽器版本號''' Creates a new session with the desired capabilities. :Args: - browser_name - The name of the browser to request. - version - Which browser version to request. - platform - Which platform to request the browser on. - javascript_enabled - Whether the new session should support JavaScript. - browser_profile - A selenium.webdriver.firefox.firefox_profile.FirefoxProfile object. Only used if Firefox is requested.'''print(driver.capabilities[’version’])
以上這篇python+Selenium自動(dòng)化測試——輸入,點(diǎn)擊操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
