zoukankan      html  css  js  c++  java
  • WebDriver API——延时操作及元素等待

        在自动化测试过程当中,受网络、测试设备等诸多因素的影响,我们经常需要在自动化测试脚本中添加一些延时来更好的定位元素来进行一系列的操作。

        一般有这么几种方式:

    1.implicitlyWait。识别对象时的超时时间。过了这个时间如果对象还没找到的话就会抛出NoSuchElement异常

    2.setScriptTimeout。异步脚本的超时时间。webdriver 可以异步执行脚本,这个是设置异步执行脚本脚本返回结果的超时时间。

    3.pageLoadTimeout。页面加载时的超时时间。因为webdriver 会等页面加载完毕在进行后面的操作,所以如果页面在这个超时时间内没有加载完成,那么webdriver 就会抛出异常。

    4.Thread.sleep()。这是一种挂起线程然后重新唤醒的方式,不建议使用,占用系统线程资源。

    package com.testngDemo;
    
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Demo_TimeControl {
    	
    	public static void main(String args[])
    	{
    		WebDriver driver=new FirefoxDriver();
    		driver.get("http://www.baidu.com");
    		
    		//设置延时操作
    		driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);//-----页面加载时间
    		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//------元素等待时间(隐式等待)
    		driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);//----脚本执行时间
    		//显式等待
    		WebElement e = (new WebDriverWait(driver, 10)) .until(
    			        new ExpectedCondition< WebElement>(){
    						@Override
    						public WebElement apply(WebDriver d) {
    							return d.findElement(By.id("kw"));
    						}
    			        }
    			    );   
    	}
    	/**
    	 *  自定义显式等待
    	 * @param driver
    	 * @param by
    	 * @return
    	 */
    	private static WebElement webelementExplicitWait(WebDriver driver,By by)
    	{
    		return (new WebDriverWait(driver, 10)) .until(
    			        new ExpectedCondition< WebElement>(){
    						@Override
    						public WebElement apply(WebDriver d) {
    							return d.findElement(by);
    						}
    			        }
    			    );
    	}
    	/**
    	 *  判断元素是否存在
    	 * @param driver
    	 * @param by
    	 * @return
    	 */
    	private static boolean isPresentElement(WebDriver driver,By by)
    	{
    		try {
    			driver.findElement(by);
    			return true;
    		} catch (NoSuchElementException e) {	
    			return false;
    		}catch(Exception e){
    			return false;
    		}
    	}
    
    }
    

         我们可以根据自己的需要来封装一些元素等待,元素查找的方法,像上面判断元素是否存在,如果查找不到就抛出异常,有的时候可能是抛错没有这样的元素,有的时候也可能是抛错元素不可见等错误,可以用Excpetion来捕获。

         在timeout接口里就介绍了这前三种时间等待的方法,这里也不细加研究了。

         再就是显式等待ExplicitWait,可以根据我们的需要判读在一个时间范围内这个元素是否能定位到,这个作用是比较大的,像上面的方法只是来整体的延时,到底能不能来定位到元素也不知道。但是我们也可以根据自己项目需要来对这些方法进行封装,使之更好的为我们服务。

  • 相关阅读:
    web测试学习大纲
    Python语言编程基础
    python文件IO
    Python 正则表达式
    python官网导航翻译
    python常用库
    python连接数据库
    sublime与python交互
    selenium连接浏览器方式
    sublime中运行python时编码格式问题
  • 原文地址:https://www.cnblogs.com/dreamyu/p/6512575.html
Copyright © 2011-2022 走看看