selenium expected_conditions 源码学习记录

#expected_conditions模块收集了一系列的场景判断方法

#源码地址
# https://seleniumhq.github.io/selenium/docs/api/py/_modules/selenium/webdriver/support/expected_conditions.html

 #源码通过__call__实现函数调用

#如果类定义了__call__,那么对应的实例,可以直接作为函数使用
#个类实例要变成一个可调用对象,只需要实现一个特殊方法__call__()

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait 


'''

# title_is:
    判断当前页面的title是否等于预期字符串,返回true/false
# title_contains 
    判断当前页面的title是否包含预期字符串,true/false
    return self.title in driver.title
# presence_of_element_located:
    判断某个元素是否被加到了dom树里,并不代表该元素一定可见
    return _find_element(driver, self.locator)
# presence_of_all_elements_located : 判断是否至少有1个元素存在于dom树中。

# visibility_of_element_located : 
    判断某个元素是否可见
    return _element_if_visible(_find_element(driver, self.locator))/false
# element_to_be_selected:
    判断某个元素是否被选中了,一般用在下拉列表
# element_located_selection_state_to_be:
    跟上面的方法作用一样,只是上面的方法传入定位到的element,而这个方法传入locator

# text_to_be_present_in_element : 
    判断某个元素中的text是否包含了预期的字符串 true/false
    return self.text in element_text
# text_to_be_present_in_element_value:
     返回true/false
     判断某个元素中的value属性是否包含了预期的字符串get_attribute("value")

# frame_to_be_available_and_switch_to_it : 
     判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False
# alert_is_present : 
    判断页面上是否出现alert,如果出现driver.switch_to.alert,否则返回false

# element_to_be_clickable : 
    判断某个元素中是否可见并且是enable的
    if element and element.is_enabled():
            return element

# staleness_of :
     等某个元素从dom树中移除,
     self.element.is_enabled()
            return False


'''


#常用的几种示例
# 1.presence_of_element_located(locator)
# 检查页面的DOM中是否该存在元素。这并不一定意味着元素是可见的
# 源码
class presence_of_element_located(object):
    """ An expectation for checking that an element is present on the DOM
    of a page. This does not necessarily mean that the element is visible.
    locator - used to find the element
    returns the WebElement once it is located
    """
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        return _find_element(driver, self.locator)

#测试例子
driver = webdriver.PhantomJS()
driver.get("https://www.baidu.com")
WebDriverWait(driver,10,0.5).until(
    EC.presence_of_element_located((By.ID,"kw")))
print ('ok')
driver.quit()


#元素是否呗selected
class element_located_to_be_selected(object):
    """An expectation for the element to be located is selected.
    locator is a tuple of (by, path)"""
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        return _find_element(driver, self.locator).is_selected()

#相似的场景和用法
WebDriverWait(driver,10,0.5).until(
    EC.element_located_to_be_selected(locator))


#2 title_is(title)

# 验证传入的usertitle和实际网页的title是否一致
# 源码
class title_is(object):

    def __init__(self, title):
        self.title = title

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

# __call__
# 实例要变成一个可调用对象,只需要实现一个特殊方法__call__()
# 实例初始化t=title_is(title)
# 实例作为函数使用t(driver)就可以直接执行self.title == driver.title       

#测试例子
#源码调用1
driver = webdriver.PhantomJS()
driver.get("https://www.baidu.com")
print (driver.title)
t=title_is(u'百度一下,你就知道')
print (t(driver))
# True

#直接模块导入调用2,一般集合unitest assert
t=EC.title_is(u'百度一下,你就知道')
print (t(driver))
# True

# #3去判断弹框是否出现了,先判断alert是否弹出,如果弹出就点确定按钮accept()

class alert_is_present(object):
    """ Expect an alert to be present."""
    def __init__(self):
        pass

    def __call__(self, driver):
        try:
            alert = driver.switch_to.alert
            return alert
        except NoAlertPresentException:
            return False
#一般在程序中调用类似如下
alert=alert_is_present()
if alert(driver):
    alert.accept()
else:
    print (u'没有弹出')

 

posted @ 2019-08-26 15:24  米 立  阅读(259)  评论(0编辑  收藏  举报