selenium自動化測試入門 設置等待時間

time.sleep(3) 固定等待3秒

driver.implicitly_wait(10) 隱性的等待,對應全局

WebDriverWait( driver, timeout).until(『有返回值的__call__()方法或函數』) 顯性的等待,對應到元素

一、time.sleep(seconds) 固定等待

import timentime.sleep(3) #等待3秒n

time.sleep(seconds) seconds參數為整數,單位(秒)。

它是Python的time提供的休眠方法。

常用於短時間的等待,為了自動測試用例的執行效率固定等待的時間需要控制在3秒內。在用例中盡量少用固定等待。

二、智能隱性的等待implicitly_wait(回應超時等待)

driver.implicitly_wait(time_to_wait)

回應超時等待,隱性的,設置後對應的是全局,如查找元素。

driver.implicitly_wait(10) # 設置全局隱性等待時間,單位秒n

每次driver執行 找不到元素都會等待設置的時間,它的值設置的過長對用例執行效率有很大的影響,必須在執行完成之後還原回來。driver.implicitly_wait() 要慎之又慎的使用。

driver對它的默認值為0,driver.implicitly_wait(0)能還原隱性等待的設置時間。

三、智能顯性等待WebDriverWait

WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)

參數說明:

driver 為webdriver驅動

timeout 最長超時時間,單位(秒)

poll_frequency 循環查找元素每次間隔的時間,默認0.5秒

ignored_exceptions 超時後需要輸出的異常信息

WebDriverWait()下面有兩個方法可用until()和until_not()

WebDriverWait(driver, timeout).until(method, message=)

method 函數或者實例__call__()方法返回True時停止,否則超時後拋出異常。

參數說明:

method 在等待時間內調用的方法或者函數,該方法或函數需要有返回值,並且只接收一個參數driver。

message 超時時拋出TimeoutException,將message傳入異常顯示出來

WebDriverWait(driver, timeout).until_not(method, message=)

於上面的until() 相反,until_not 中的method函數或者實例__call__()方法返回False結束,否則拋出異常。

until方法使用的method 的函數或者類__call()__方法詳解:

函數我們一般採用匿名函數lambda 。

lambda driver:driver.find_element(<定位元素>) # 當定位的元素時為True,無元素時為False。如示例1、2:

WebDriverWait示例1:

WebDriverWait(driver,5).until(lambda driver:driver.find_element_by_id(query))n

5秒內等待元素(id=query)出現,lambda driver:driver.find_element_by_id(query) 為一個匿名函數,只有一個driver參數,返回的是查找的元素對象。

WebDriverWait示例2:

WebDriverWait(driver, 5).until_not(lambda driver:driver.find_element_by_name(query))n

5秒內等待元素消失,同示例1 until_not 要求無元素返回即元素不存在於該頁面。

定義類中的__call()__方法。

class wait_element(object):n def __init__(self, locator):n self.locator = locatornn def __call__(self, driver):n return driver.find_element(self.locator)nnWebDriverWait(driver, 5).until(wait_element((By.ID, query))) # 等待元素出現nWebDriverWait(driver, 5).until_not(wait_element((By.ID, query))) # 等待元素消失n

wait_element類中__init__()方法接收需要定位的元素,__call__()方法中只能有唯一變數driver,並且返回元素對象。

這樣做是是不是很麻煩,其實selenium提供的一個庫進行操作,expected_conditions庫。引入位置

from selenium.webdriver.support import expected_conditions as ec,它囊括了我們需要使用等待的所有情況。

四、expected_conditions 類庫

from selenium.webdriver.support import expected_conditions as ec # 引入包n

下面示例都是以搜狗搜索首頁為例。

方法中參數說明 locator=(By.ID, id) 表示使用By方法定位元素的元組,element表示獲取的webElement元素對象。

ec.title_is(『title』) 判斷頁面標題等於title

ec.title_contains(『title』) 判斷頁面標題包含title

WebDriverWait(driver, 10).until(ec.title_is(搜狗搜索引擎 - 上網從搜狗開始)) # 等待title 於參數相等nWebDriverWait(driver, 10).until(ec.title_contains(搜狗搜索引擎)) # 等待title 包含 參數的內容n

ec.presence_of_element_located(locator) 等待locator元素是否出現

ec.presence_of_all_elements_located(locator) 等待所有locator元素是否出現

WebDriverWait(driver, 10).until(ec.presence_of_element_located((By.ID, query)))nWebDriverWait(driver, 10).until(ec.presence_of_all_elements_located((By.ID, query)))n

ec.visibility_of_element_located(locator) 等待locator元素可見

ec.invisibility_of_element_located(locator) 等待locator元素隱藏

ec.visibility_of(element) 等待element元素可見

WebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.ID, stb))) # 等待元素可見nWebDriverWait(driver, 10).until(ec.invisibility_of_element_located((By.ID, stb))) # 等待元素隱藏nWebDriverWait(driver, 10).until(ec.visibility_of(driver.find_element_by_link_text(高級搜索))) # 等待元素可見n

ec.text_to_be_present_in_element(locator,text) 等待locator的元素中包含text文本

ec.text_to_be_present_in_element_value(locator,value) 等待locator元素的value屬性為value

WebDriverWait(driver, 10).until(ec.text_to_be_present_in_element((By.ID, erwx), 搜索APP)) # 等待元素中包含搜索APP文本nWebDriverWait(driver, 10).until(ec.text_to_be_present_in_element_value((By.ID, query),selenium)) # 等待元素的值為 selenium,一般用於輸入框n

ec.frame_to_be_available_and_switch_to_it(locator) 等待frame可切入

WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it((By.ID, frame)))nWebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it(frameid))nec.alert_is_present() 等待alert彈出窗口出現nWebDriverWait(driver, 10).until(ec.alert_is_present())n

ec.element_to_be_clickable(locator) 等待locator元素可點擊

WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.ID, kw)))n

等待元素被選中,一般用於複選框,單選框

ec.element_to_be_selected(element) 等待element元素是被選中

ec.element_located_to_be_selected(locator) 等待locator元素是被選中ec.element_selection_state_to_be(element, is_selected) 等待element元素的值被選中為is_selected(布爾值)

ec.element_located_selection_state_to_be(locator,is_selected) 等待locator元素的值是否被選中is_selected(布爾值)

from selenium import webdrivernimport timenfrom selenium.webdriver.support.wait import WebDriverWaitnfrom selenium.webdriver.support import expected_conditions as ec # 引入包nfrom selenium.webdriver.common.by import Bynndriver = webdriver.Chrome()ndriver.maximize_window() ndriver.get(rhttps://www.baidu.com/) ndriver.find_element_by_link_text(設置).click()nWebDriverWait(driver, 3).until(ec.element_to_be_clickable((By.LINK_TEXT, 搜索設置))) # 等待搜索可點擊,不可缺少ndriver.find_element_by_link_text(搜索設置).click()nelement = driver.find_element_by_id(s1_1)nWebDriverWait(driver, 2).until(ec.element_to_be_selected(element)) # element被選中nWebDriverWait(driver, 10).until(ec.element_located_to_be_selected((By.ID, SL_0))) # id=』SL_0』 被選中nWebDriverWait(driver, 10).until(ec.element_selection_state_to_be(element,True )) # element 被選中nWebDriverWait(driver, 10).until(ec.element_located_selection_state_to_be((By.ID, SL_1), False)) # id=』SL_1』不被選中ntime.sleep(3)ndriver.quit()n

五、什麼時候使用等待

固定等待sleep與隱性等待implicitly_wait盡量少用,它會對測試用例的執行效率有影響。

顯性的等待WebDriverWait可以靈活運用,什麼時候需要用到?

1、頁面載入的時候,確認頁面元素是否載入成功可以使用WebDriverWait

2、頁面跳轉的時候,等待跳轉頁面的元素出現,需要選一個在跳轉前的頁面不存在的元素

3、下拉菜單的時候,如上百度搜索設置的下拉菜單,需要加上個時間斷的等待元素可點擊

4、頁面刷新的時候

總之,頁面存在改變的時候;頁面上本來沒的元素,然後再出現的元素


推薦閱讀:

某測試模擬器性能優化-選對你的庫
{ 百人計劃 } 到底是個什麼鬼 ? 如何去顛覆測試職業 ?

TAG:软件测试 | 自动化测试 | Selenium |