python selenium expected_conditions使用實例

場景

Expected Conditions的使用場景有2種

  • 直接在斷言中使用
  • 與WebDriverWait配合使用,動態等待頁面上元素出現或者消失

方法註釋

先翻譯一下這些方法的用法

  • title_is: 判斷當前頁面的title是否精確等於預期

  • title_contains: 判斷當前頁面的title是否包含預期字符串

  • presence_of_element_located: 判斷某個元素是否被加到了dom樹裏,並不代表該元素一定可見

  • visibility_of_element_located: 判斷某個元素是否可見.可見代表元素非隱藏,並且元素的寬和高都不等於0

  • visibility_of: 跟上面的方法做一樣的事情,只是上面的方法要傳入locator,這個方法直接傳定位到的element就好了

  • presence_of_all_elements_located: 判斷是否至少有1個元素存在於dom樹中。舉個例子,如果頁面上有n個元素的class都是'column-md-3',那麼只要有1個元素存在,這個方法就返回True

  • text_to_be_present_in_element: 判斷某個元素中的text是否包含了預期的字符串

  • text_to_be_present_in_element_value: 判斷某個元素中的value屬性是否包含了預期的字符串

  • frame_to_be_available_and_switch_to_it: 判斷該frame是否可以switch進去,如果可以的話,返回True並且switch進去,否則返回False

  • invisibility_of_element_located: 判斷某個元素中是否不存在於dom樹或不可見

  • element_to_be_clickable: 判斷某個元素中是否可見並且是enable的,這樣的話才叫clickable

  • staleness_of: 等某個元素從dom樹中移除,注意,這個方法也是返回True或False

  • element_to_be_selected: 判斷某個元素是否被選中了,一般用在下拉列表

  • element_selection_state_to_be: 判斷某個元素的選中狀態是否符合預期

  • element_located_selection_state_to_be: 跟上面的方法作用一樣,只是上面的方法傳入定位到的element,而這個方法傳入locator

  • alert_is_present: 判斷頁面上是否存在alert,這是個老問題,很多同學會問到

具體的例子

例子的代碼在這裏,並且是可以運行通過的。

下面的代碼演示了一些常見疑問

  • 如何等待頁面上的某個元素出現,然後再對這個元素進行操作
  • 如何在unittest框架中所有的用例都共用1個瀏覽器實例,然後在全部用例結束後關閉瀏覽器

expected_conditions.py

#encoding:utf-8
# example of how to use https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/support/expected_conditions.py

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By

import unittest

# dr = webdriver.PhantomJS('phantomjs')
dr = webdriver.Firefox()
# dr = webdriver.Chrome()
url = 'http://www.baidu.com'
search_text_field_id = 'kw'
dr.get(url)

class ECExample(unittest.TestCase):

  def test_title_is(self):
    ''' 判斷title是否符合預期 '''
    title_is_baidu = EC.title_is(u'百度一下,你就知道')
    self.assertTrue(title_is_baidu(dr))

  def test_titile_contains(self):
    ''' 判斷title是否包含預期字符 '''
    title_should_contains_baidu = EC.title_contains(u'百度')
    self.assertTrue(title_should_contains_baidu(dr))

  def test_presence_of_element_located(self):
    ''' 判斷element是否出現在dom樹 '''
    locator = (By.ID, search_text_field_id)
    search_text_field_should_present = EC.visibility_of_element_located(locator)

    ''' 動態等待10s,如果10s內element加載完成則繼續執行下面的代碼,否則拋出異常 '''
    WebDriverWait(dr, 10).until(EC.presence_of_element_located(locator))
    WebDriverWait(dr, 10).until(EC.visibility_of_element_located(locator))

    self.assertTrue(search_text_field_should_present(dr))

  def test_visibility_of(self):
    search_text_field = dr.find_element_by_id(search_text_field_id)
    search_text_field_should_visible = EC.visibility_of(search_text_field)
    self.assertTrue(search_text_field_should_visible('yes'))

  def test_text_to_be_present_in_element(self):
    text_should_present = EC.text_to_be_present_in_element((By.NAME, 'tj_trhao123'), 'hao123')
    self.assertTrue(text_should_present(dr))


  @classmethod
  def tearDownClass(kls):
    print 'after all test'
    dr.quit()
    print 'quit dr'

if __name__ == '__main__':
  unittest.main()

分析

以title_is爲例

class title_is(object):
    """An expectation for checking the title of a page.
    title is the expected title, which must be an exact match
    returns True if the title matches, false otherwise."""
    def __init__(self, title):
        self.title = title

    def __call__(self, driver):
        return self.title == driver.title

可以看到title_is實際上是1個class,其__call__方法被定義成是返回1個bool值。因此,一般的用法就是

# 實例化
the_instance = title_is('expected')
# 直接在實例上調用__call__
the_instance(dr) #return True or False
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章