Wait commands in WebDriver

Listing out the different WebDriver Wait statements that can be useful for an effective scripting and can avoid using the Thread.sleep() comamnds

After few searches and digging into the WebDriver Java doc, I managed to design a mindmap of the different WebDriver commands available

WebDriver.manage().timeouts()

implicitlyWait

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));


The ImplicitWait will tell the webDriver to poll the DOM for a certain duration when trying to find the element, this will be useful when certain elements on the webpage will not be available immediately and needs some time to load. 
By default it ill take the value to 0, for the life of the WebDriver object instance through out the test script.

pageLoadTimeout

driver.manage().timeouts().pageLoadTimeout(100, SECONDS);

Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite. 

setScriptTimeout

driver.manage().timeouts().setScriptTimeout(100,SECONDS);

Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely. 


Support.ui

FluentWait

// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

ExpectedConditions

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

Models a condition that might reasonably be expected to eventually evaluate to something that is neither null nor false. 
Examples : Would include determining if a web page has loaded or that an element is visible. 
Note that it is expected that ExpectedConditions are idempotent. They will be called in a loop by the WebDriverWait and any modification of the state of the application under test may have unexpected side-effects. 

WebDriverWait will be used as we used in the Expected conditions code snippet as above.

sleeper is something same as the Thread.sleep() method, but this with an Abstraction around the thread.sleep() for better testability

selenium-webdriver的等待方法的更多相关文章

  1. Python selenium 三种等待方法

    1. 强制等待 sleep(xx) 是最简单粗暴的一种办法,不管你浏览器是否加载完了,程序都得等待3秒,3秒一到,继续执行下面的代码,作为调试很有用,不建议总用这种等待方式,严重影响程序执行速度. 代 ...

  2. selenium三个等待方法

    为什么需要等待时间:页面加载需要时间,如果页面没有加载完成,直接去定位,可能定位不到元素 1.强制等待: import time time.sleep(2) 不管有没有完成加载,必须等待2秒 2.隐式 ...

  3. selenium中的等待方法及区别

    等待是为了使脚本执行更加稳定 常用的休眠方式: 1.time模块的sleep方法 :引入from time import sleep 2.implicitly_wait():设置webdriver等待 ...

  4. python之selenium三种等待方法

    前提: 我们在做Web自动化时,有的时候要等待元素加载出来,才能操作,不然会报错 1.强制等待 2.隐式等待 3.显示等待 内容: 一,强制等待 这个比较简单,就是利用time模块的sleep的方法来 ...

  5. selenium webdriver python 等待

    AJAX,即“Asynchronous Javascript And  XML”.可以实现网页的异步更新.也就是在不重新加载整个网页的情况下,对网页的某部分进行更新. 现在大多数网站都使用AJAX技术 ...

  6. Selenium_用selenium webdriver实现selenium RC中的类似的方法

    最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...

  7. selenium webdriver缺陷

    关闭  selenium webdriver缺陷 除了http://573301735.com/?p=5126讲 的,昨天又发现一个让我1个小时生不如死的问题,就是使用两个不同的配置文件来初始化dri ...

  8. [selenium webdriver Java]元素定位——findElement/findElements

    策略 语法 语法 描述 By id driver.findElement(By.id()) driver.findElements(By.id()) 通过id属性定位元素 By name driver ...

  9. java selenium webdriver实战 seleniumIDE

    Selenium是ThoughtWorks公司,一个名为Jason Huggins的测试为了减少手工测试的工作量,自己实现的一套基于Javascript语言的代码库 使用这套库可以进行页面的交互操作, ...

  10. java selenium webdriver第一讲 seleniumIDE

    Selenium是ThoughtWorks公司,一个名为Jason Huggins的测试为了减少手工测试的工作量,自己实现的一套基于Javascript语言的代码库 使用这套库可以进行页面的交互操作, ...

随机推荐

  1. String类与Date类的转换

    public class DateTest { public static void main(String[] args) throws ParseException { Date date = n ...

  2. 基于NodeJs的网页爬虫的构建(二)

    好久没写博客了,这段时间已经忙成狗,半年时间就这么没了,必须得做一下总结否则白忙.接下去可能会有一系列的总结,都是关于定向爬虫(干了好几个月后才知道这个名词)的构建方法,实现平台是Node.JS. 背 ...

  3. top工具

    top 显示进程所占系统资源 能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top命令打印出了很多信息,包括系统负载(loadaverage).进程数(Tasks).c ...

  4. Dataset

    1,if(ds == null) 这是判断内存中的数据集是否为空,说明DATASET为空,行和列都不存在!! 2,if(ds.Tables[0].Count == 0) 这应该是在内存中存在一个DAT ...

  5. 四句话总结JavaScript作用域

    上一篇文章中简单介绍了一下JS作用域,本篇将作进一步探究和总结. 前言:JavaScript的作用域一直以来都是前端开发中比较难以理解的知识点,JavaScript6中新引入了 let 关键字,用于指 ...

  6. Web模板引擎本质前奏

    执行字符串表示的函数,并为该函数提供全局变量: #! /usr/bin/env python3 namespace = {'name': 'zingp', 'data': [16, 19, 25]} ...

  7. 使用reinterpret_cast的危险

    关键字: c++ cast // Cast.cpp : Defines the entry point for the console application. // #include "s ...

  8. 关于DPC和workitem的简单用法

    这个随笔是记录我半个月左右的时间,从想法到查资料请教,以及到实践的成果. 我想实现的是,隔定时时间写文件,本以为调用写的函数就可以实现了,结果各种BSOD,IRQL_NOT_LESS_OR_EQUAL ...

  9. Oracle死锁只会回滚跟死锁有关的那条SQL,而不会回滚整个事务

    数据库检测到死锁后,只会回滚跟死锁有关的某条语句,而不会回滚整个事务. 创建测试环境:SQL> create table test1(id int,name char(1)); 表已创建. SQ ...

  10. css常用伪类记录

    1.超链接使用css伪类设置颜色 a:link {color: #000000} /* 未访问的链接 */a:visited {color: #d90a81} /* 已访问的链接 */a:hover ...