There are two scenarios for using Expected Conditions
- Use directly in an assertion
- Use with WebDriverWait to dynamically wait for elements to appear or disappear on the page
Method Notes
Translate the usage of these methods first
-
title_is: Determines if the title of the current page is exactly equal to what is expected
-
title_contains: Determines if the title of the current page contains the expected string
-
presence_of_element_located: Determining whether an element has been added to the dom tree does not mean that it must be visible
-
visibility_of_element_located: Determines whether an element is visible. Visibility represents an element that is not hidden, and neither its width nor height is equal to 0
-
visibility_of: Does the same thing as the above method, except that the above method passes in the locator, which passes directly to the element being located
-
presence_of_all_elements_located: Determines if at least one element exists in the dom tree.For example, if the class es of n elements on a page are'column-md-3', this method returns True if only one element exists
-
text_to_be_present_in_element: Determines if the text in an element contains the expected string
-
text_to_be_present_in_element_value: Determines if the value attribute in an element contains the expected string
-
frame_to_be_available_and_switch_to_it: Determines if the frame can be switched in, returns True if it can, and switches in otherwise returns False
-
invisibility_of_element_located: Determines whether an element does not exist in the dom tree or is not visible
-
element_to_be_clickable: Determines whether an element is visible and enable d, which is called clickable
-
An element such as staleness_of: is removed from the dom tree, note that this method also returns True or False
-
element_to_be_selected: Determines whether an element is selected, commonly used in drop-down lists
-
element_selection_state_to_be: Determines whether an element's selected state is as expected
-
element_located_selection_state_to_be: Just like the above method does, it just passes in the positioned element, and this method passes in the locator
-
alert_is_present: Determining if alert exists on a page is an old question that many students will ask
Specific examples
The code for the example is Here And can be run and passed.
The following code demonstrates some common questions
- How to wait for an element to appear on the page before you manipulate it
- How to share a browser instance for all use cases in the unittest framework and then close the browser after all use cases have ended
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): ''' judge title Is it as expected ''' title_is_baidu = EC.title_is(u'Baidu once, you know') self.assertTrue(title_is_baidu(dr)) def test_titile_contains(self): ''' judge title Does it contain the expected characters ''' title_should_contains_baidu = EC.title_contains(u'Baidu') self.assertTrue(title_should_contains_baidu(dr)) def test_presence_of_element_located(self): ''' judge element Is it present dom tree ''' locator = (By.ID, search_text_field_id) search_text_field_should_present = EC.visibility_of_element_located(locator) ''' Dynamic Wait 10 s,If 10 s within element Continue executing the following code when loading is complete, otherwise throw an exception ''' 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()Analysis
with title_is take as an example
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.titleYou can see
title_is
It's actually one class,his__call__
Method is defined as returning 1 bool Value.Therefore, the general usage is
# instantiation the_instance = title_is('expected') # Call u call_u directly on the instance the_instance(dr) #return True or False</div> </div>