selenium的显示期待
在进行UI自动化测试的时候,咱们为了放弃用例的稳定性,往往要设置显示期待,显示期待就是说明确的要等到某个元素的呈现或者元素的某些条件呈现,比方可点击、可见等条件,如果在规定的工夫之内都没有找到,那么就会抛出Exception
.
下面是我用selenium
写的一个测试用例,展现了selenium
中显示期待的应用形式,其中会应用到expected_conditions
模块和WebDriverWait
类,留神这里expected_conditions
是一个py文件的文件名,也就是一个模块名,这个模块上面有很多的条件类,而咱们用例中应用的title_is
就是一个条件类。
WebDriverWait
是一个类,这个类的作用就是依据肯定的条件,一直的查看这个条件是否被满足了。WebDriverWait
类只有两个办法,一个是until
直到满足某个条件,另一个是until_not
直到不满足某个条件。
class WebDriverWait(object): def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
WebDriverWait
有四个参数别离是, driver
驱动, timeout
超时工夫, poll_frequency=POLL_FREQUENCY
轮训工夫,也就是去判断条件是否满足的工夫距离,默认是0.5秒, ignored_exceptions=None
在期待的过程中须要疏忽的异样,是一个可迭代的异样类汇合,比方咱们能够设置一个list,外面是[NoSuchElementException,NoSuchAttributeException,InvalidElementStateException....]
,默认状况下,是一个元组,只蕴含一个NoSuchElementException
,因为只有元素呈现,能力去判断条件是否满足,在一直轮训的过程中,必定会产生NoSuchElementException
,这个时候必须疏忽掉这个异样,不然程序就会中断。
其中driver
和timeout
是毕传的地位参数,另外两个是抉择传递的关键字参数,如果不传都有指定的默认值。
上面就进入咱们明天的主题,selenium中的期待条件的探讨
期待条件
条件类的实现原理
在selenium.webdriver.support.expected_conditions
这个模块里,寄存着所有的期待条件,每个条件类的构造都是一样的一个__init__
构造方法和一个__call__
办法。
在python中,如果想把类型的对象当做函数来应用,那么就能够给这个类实现__call__
办法,如下:
class TestCase: def __init__(self): self.driver = webdriver.Chrome(executable_path="./driver/chromedriver") self.driver.get('http://www.baidu.com') # sleep(2) def __call__(self): print(self.driver.title)if __name__ == '__main__': case = TestCase() case()
case()
对象的调用,就会执行__call__
办法外面的逻辑打印以后页面的题目,咱们取一个selenium的实现类:
class presence_of_element_located(object): def __init__(self, locator): self.locator = locator def __call__(self, driver): return _find_element(driver, self.locator)
这个条件类的意思是判断一个元素是否曾经渲染到页面当中,在应用这个条件的时候须要先实例化,传入元素的定位,而后要进行判断的时候须要对实例对象进行调用并传入driver
,对实例对象进行调用的时候就会执行__call__
办法里的条件判断逻辑。
WebDriverWait
是如何进行条件判断的
再回到文章结尾看一下咱们应用显示期待的代码:
wait = WebDriverWait(self.driver, 2)wait.until(EC.title_is('百度一下,你就晓得'))
先是实例化一个WebDriverWait
对象,而后再调用until
办法并且传递一个条件的实例对象,until
办法里就会一直的去轮训条件是否满足。
def until(self, method, message=''): screen = None stacktrace = None end_time = time.time() + self._timeout while True: try: value = method(self._driver) if value: return value except self._ignored_exceptions as exc: screen = getattr(exc, 'screen', None) stacktrace = getattr(exc, 'stacktrace', None) time.sleep(self._poll) if time.time() > end_time: break raise TimeoutException(message, screen, stacktrace)
method
这个参数就是咱们传递进来的条件的实例对象,value = method(self._driver)
这里就是进行对象的调用,也就是执行了__call__
办法里的逻辑。
selenium里都有哪些条件
- title_is 判断title是否呈现
- title_contains 判断title页面题目是否蕴含某些字符
- presence_of_element_located 判断某个元素是否被加载到了dom树里,然而并不代表这个元素可见
- url_contains 判断以后url是否蕴含某个url
- url_matches 判断以后url是否合乎某种格局
- url_to_be 判断以后url是否呈现
- url_changes 判断以后url是否曾经产生了变动
- visibility_of_element_located 判断某个元素是否被增加到了dom树里,且宽高都大于0
- visibility_of 判断看某个元素是否可见
- presence_of_all_elements_located 判断至多有一个元素存在于dom树中,返回所有定位到的元素
- visibility_of_any_elements_located 判断至多有一个元素在页面中可见
- visibility_of_all_elements_located 判断是否所有元素都在页面中可见
- text_to_be_present_in_element 判断指定的元素中是否蕴含了预期的字符串
- text_to_be_present_in_element_value 判断指定的元素属性值中是否蕴含了预期的字符串
- frame_to_be_available_and_switch_to_it 判断iframe是否能够switch进去
- invisibility_of_element_located 判断某个元素是否在dom中不可见
- element_to_be_clickable 判断某个元素是否可见并且是enable的,也就是说是是否能够点击
- staleness_of 期待某个元素从dom中删除
- element_to_be_selected 判断某个元素是否被选中了,个别用于下拉列表中
- element_located_to_be_selected 与下面的意思一样,只不过下面实例化的时候传入的是元素对象,这个传入的是定位
- element_selection_state_to_be 判断某个元素的选中状态是否合乎预期
- element_located_selection_state_to_be 与下面一样,只不过传值不同而已
- number_of_windows_to_be 判断以后窗口数是否等于预期
- new_window_is_opened 判断是否有窗口减少
- alert_is_present 判断页面是否有弹窗
以上就是selenium反对的所有条件。
而后就是自定义了
说了那么多条件,其实咱们也能够本人实现一个条件类,
class page_is_load: def __init__(self, expected_title, expected_url): self.expected_title = expected_title self.expected_url = expected_url def __call__(self, driver): is_title_correct = driver.title == self.expected_title is_url_correct = driver.current_url == self.expected_url return is_title_correct and is_url_correct
下面是本人实现的一个条件类,依据页面的url和题目来判断页面是否被正确加载,
class TestCase: def __init__(self): self.driver = webdriver.Chrome(executable_path="./driver/chromedriver") self.driver.get('http://www.baidu.com/') # sleep(2) def __call__(self): print(self.driver.title) def test_wait(self): wait = WebDriverWait(self.driver, 2) wait.until(page_is_load("百度一下,你就晓得", "http://www.baidu.com/"))
<img src="https://zyjblog.oss-cn-beijing.aliyuncs.com/1593947944.jpg" style="zoom: 25%;" />
欢送大家去 我的博客 瞅瞅,外面有更多对于测试实战的内容哦!!