51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1993|回复: 2
打印 上一主题 下一主题

[转贴] selenium webdriver学习 11 – 怎么等待页面元素加载完成

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2017-7-18 14:08:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
web的自动化测试中,我们经常会遇到这样一种情况:当我们的程序执行时需要页面某个元素,而此时这个元素还未加载完成,这时我们的程序就会报错。怎么办?等待。等待元素出现后再进行对这个元素的操作。
在selenium-webdriver中我们用两种方式进行等待:明确的等待和隐性的等待。
明确的等待
明确的等待是指在 代码进行下一步操作之前等待某一个条件的发生。最不好的情况是使用Thread.sleep()去设置一段确认的时间去等待。但为什么说最不好呢?因为一 个元素的加载时间有长有短,你在设置sleep的时间之前要自己把握长短,太短容易超时,太长浪费时间。selenium webdriver提供了一些方法帮助我们等待正好需要等待的时间。利用WebDriverWait类和ExpectedCondition接口就能实现这一点。
下面的html代码实现了这样的一种效果:点击click按钮5秒钟后,页面上会出现一个红色的div块。我们需要写一段自动化脚本去捕获这个出现的div,然后高亮之。
Wait.html
CODE:
  1. <font size="4"><html>  
  2.     <head>  
  3.         <title>Set Timeout</title>  
  4.         <style>  
  5.             .red_box {background-color: red; width = 20%; height: 100px; border: none;}  
  6.         </style>  
  7.         <script>  
  8.             function show_div(){  
  9.                 setTimeout("create_div()", 5000);  
  10.             }  
  11.      
  12.             function create_div(){  
  13.                 d = document.createElement('div');  
  14.                 d.className = "red_box";  
  15.                 document.body.appendChild(d);  
  16.             }  
  17.         </script>  
  18.     </head>  
  19.     <body>  
  20.         <button id = "b" onclick = "show_div()">click</button>  
  21.     </body>  
  22. </html></font>
复制代码
下面的代码实现了高亮动态生成的div块的功能:
CODE:
  1. <font size="4">import org.openqa.selenium.By;  
  2. import org.openqa.selenium.JavascriptExecutor;  
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.WebElement;  
  5. import org.openqa.selenium.firefox.FirefoxDriver;  
  6. import org.openqa.selenium.support.ui.ExpectedCondition;  
  7. import org.openqa.selenium.support.ui.WebDriverWait;  
  8.    
  9. public class WaitForSomthing {  
  10.    
  11.     /**
  12.      * @author gongjf
  13.      */
  14.     public static void main(String[] args) {  
  15.         // TODO Auto-generated method stub  
  16.         System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  17.         WebDriver dr = new FirefoxDriver();  
  18.         String url = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Wait.html";// "/Your/Path/to/Wait.html"
  19.         dr.get(url);  
  20.         WebDriverWait wait = new WebDriverWait(dr,10);  
  21.         wait.until(new ExpectedCondition<WebElement>(){  
  22.             @Override
  23.             public WebElement apply(WebDriver d) {  
  24.                 return d.findElement(By.id("b"));  
  25.             }}).click();  
  26.            
  27.         WebElement element = dr.findElement(By.cssSelector(".red_box"));  
  28.         ((JavascriptExecutor)dr).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);   
  29.            
  30.     }  
  31. }</font>
复制代码
上面的代码WebDriverWait类的构造方法接受了一个WebDriver对象和一个等待最长时间(10秒)。然后调用until方法,其中重写了 ExpectedCondition接口中的apply方法,让其返回一个WebElement,即加载完成的元素,然后点击。默认情况下,WebDriverWait每500毫秒调用一次ExpectedCondition,直到有成功的返回,当然如果超过设定的值还没有成功的返回,将抛出异常。
隐性等待
隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver查询Dom一定时间。默认值是0,但是设置之后,这个时间将在WebDriver对象实例整个生命周期都起作用。上面的代码就变成了这样:
CODE:
  1. <font size="4">import java.util.concurrent.TimeUnit;  
  2.    
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.JavascriptExecutor;  
  5. import org.openqa.selenium.WebDriver;  
  6. import org.openqa.selenium.WebElement;  
  7. import org.openqa.selenium.firefox.FirefoxDriver;  
  8. import org.openqa.selenium.support.ui.ExpectedCondition;  
  9. import org.openqa.selenium.support.ui.WebDriverWait;  
  10.    
  11. public class WaitForSomthing {  
  12.    
  13.     /**
  14.      * @author gongjf
  15.      */
  16.     public static void main(String[] args) {  
  17.         // TODO Auto-generated method stub  
  18.         System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  19.         WebDriver dr = new FirefoxDriver();  
  20.            
  21.         //设置10秒  
  22.         dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
  23.            
  24.         String url = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Wait.html";// "/Your/Path/to/Wait.html"
  25.         dr.get(url);  
  26.                  //注释掉原来的  
  27.         /*WebDriverWait wait = new WebDriverWait(dr,10);
  28.         wait.until(new ExpectedCondition<WebElement>(){
  29.             @Override
  30.             public WebElement apply(WebDriver d) {
  31.                 return d.findElement(By.id("b"));
  32.             }}).click();*/
  33.         dr.findElement(By.id("b")).click();  
  34.         WebElement element = dr.findElement(By.cssSelector(".red_box"));  
  35.         ((JavascriptExecutor)dr).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);   
  36.            
  37.     }  
  38. }</font>
复制代码
两者选其一,第二种看起来一劳永逸呀。哈哈

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2
回复

使用道具 举报

该用户从未签到

2#
发表于 2017-7-18 15:20:13 | 只看该作者
都很一劳永逸
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-10 19:34 , Processed in 0.067779 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表