全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Wait机制及实现Explicit and Implicit Waits(显示或隐式等待)Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step. You should choose to use Explicit Waits or Implicit Waits.WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10s and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.Explicit WaitsAn explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.WebDriver driver = new FirefoxDriver();driver.get(http:/somedomain/url_that_delays_loading);WebElement myDynamicElement = (new WebDriverWait(driver, 10) .until(ExpectedConditions.presenceOfElementLocated(By.id(myDynamicElement);This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.Expected ConditionsThere are some common conditions that are frequently come across when automating web browsers. Listed below are Implementations of each. Java happens to have convienence methods so you dont have to code an ExpectedCondition class yourself or create your own utility package for them. Element is Clickable - it is Displayed and Enabled. WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(someid);Implicit WaitsAn implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance. 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);1显示等待:原理:显式等待,就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception.(简而言之:就是直到元素出现才去操作,如果超时则报异常)1.1 Java 线程等待函数 =硬等待Thread.sleep(int sleeptime);此方法会把当前的driver进程暂停一段时间,然后在执行接下来的操作。这方法有个缺点就是,你不能确定元素到底多久加载出来,如果你的sleepTimes是10秒,但是元素2秒就加载出来了,那么此进程还会继续等待8秒,造成时间浪费。所以非必要情况下不要使用此方法。1.2 显示智能等待写一个方法判断元素是不是已经加载出来public static void intelligentWait(WebDriver driver,int timeOut, final By by)try (new WebDriverWait(driver, timeOut).until(new ExpectedCondition() public Boolean apply(WebDriver driver) WebElement element = driver.findElement(by);return element.isDisplayed();); catch (TimeoutException e) Assert.fail(超时! + timeOut + 秒之后还没找到元素 + by + ,e);此方法有两个参数,timeOut是等待元素的超时时间,就是说过了这个时间如果元素还没加载出来就报错。By对象,这个是你元素的定位方式比如By.id(“login”);这个方法会在给定timeOut去查找元素,如果在小于timeOut的时间内找到了元素,剩下的时间不在等待,直接执行接下来的操作。Main方法中验证public static void main(String args) throws InterruptedException WebDriver driver = new FirefoxDriver();/设置等待页面完全加载的时间是10秒,如果在10秒内加载完毕,剩余时间不在等待driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);driver.get(/);By inputBox = By.id(kw);By searchButton = By.id(su);/智能等待元素加载出来intelligentWait(driver, 10, inputBox);/智能等待元素加载出来intelligentWait(driver, 10, searchButton);/输入内容driver.findElement(inputBox).sendKeys(JAVA);/点击查询driver.findElement(searchButton).click();driver.quit();具体见Java文件:2隐式等待/driver.manage().timeouts().pageLoadTimeout(pageLoadTime, TimeUnit.SECONDS);driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS);这段代码,加载driver.get(url)方法之前,他们等待你给定的时间,如果在给定时间内网页还是没有加载出来就会报错
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年11月广东深圳市龙华区招聘社区网格员72人备考题库及一套完整答案详解
- 2025年洛阳市中心医院招聘合同制工作人员93人参考模拟试题及答案解析
- 2025年富阳区总工会公开招聘工会社会工作者3人备考题库及答案详解(历年真题)
- 2025广东梅州市梅县区雁洋镇村级党群服务中心政务服务专职工作人员招聘备考题库含答案详解(满分必刷)
- 南充市消防救援支队2025年关于面向社会招聘消防文员的备考题库(二)(6人)含答案详解(轻巧夺冠)
- 2025年马鞍山宁马城际招聘招聘车站协理员70人参考笔试题库及答案解析
- 2025青海西宁市湟中区正丰现代农业科技服务有限公司招聘1人参考模拟试题及答案解析
- 2025万祥社区卫生服务中心卫生室招聘笔试考试备考试题及答案解析
- 2025年鄂州招聘警务辅助人员66人备考题库及答案详解(真题汇编)
- 2025宁波市鄞州区五乡镇人民政府代招村工作人员2人备考题库及完整答案详解一套
- 2025年秋季湖南省港航水利集团有限公司社会招聘备考题库附答案详解
- 河南省青桐鸣2026届高三上学期第二次联考语文试卷及参考答案
- 维护文化安全课件
- 汽车制造行业年终述职
- 交通运输公司安全管理工作计划及措施
- 工程监理居间协议书
- GB/T 46621-2025机械式停车设备报废条件
- 西安科技大学高新学院《电气工程专业英语》2024-2025学年第一学期期末试卷
- 22北京音乐合格考题
- 2025年工会考试练习题及答案
- 水库文明施工方案
评论
0/150
提交评论