selenium webdriver 显示等待,隐示等待,元素是否可用

package util;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.huawei.hutaf.webtest.loghelper.LoggerUtil;


public class WaitUtil {
	private static Logger logger = Logger.getLogger(LoggerUtil.WORKERLOG);

	private static final int DEFAULT_TIMEOUT_IN_SECONDS =  30;
	private static final int DEFAULT_SLEEP_TIME_IN_SECONDS =  3;
	private static final int DEFAULT_ANIMATED_INTERVAL_IN_SECONDS = 3000;
	/**
	 * 显示等待
	 * Below is the syntax(语法) to check for the element presence(存在) using
	 * WebDriverWait. Here we need to pass locator(定位器) and wait time as the
	 * parameters(参数) to the below method.
	 * 
	 * @param driver
	 * @param waitTime
	 * @param locator
	 */
	public static void isElementPresent(WebDriver driver, long waitTime,By locator) {

		WebDriverWait wait = new WebDriverWait(driver, waitTime);
		wait.until(ExpectedConditions.presenceOfElementLocated(locator));
	}

	/**
	 * 显示等待
	 * Below is the syntax to check if the element is clickable or not. We need
	 * to pass wait time and the locator as parameters.
	 * 
	 * @param driver
	 * @param waitTime
	 * @param locator
	 */
	public static void isElementClickable(WebDriver driver, long waitTime,
			By locator) {

		WebDriverWait wait = new WebDriverWait(driver, waitTime);
		wait.until(ExpectedConditions.elementToBeClickable(locator));
	}

	/**
	 * 显式等待
	 * explicit Wait
	 * @param driver
	 * @param waitTime
	 * @param locator
	 */
	public static void explicitWait(WebDriver driver, long waitTime, By locator) {

		WebDriverWait wait = new WebDriverWait(driver, waitTime);
		wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
	}

	/**
	 * fluent Wait
	 * @param driver
	 * @param duration
	 * @param waitTime
	 */
	public static Wait fluentWait(WebDriver driver, long duration, long waitTime) {

	//	Wait wait =  null;
			
		return new FluentWait(driver)
		// Wait for the condition
				.withTimeout(duration, TimeUnit.SECONDS) // 30
				// which to check for the condition with interval of 5 seconds.
				.pollingEvery(waitTime, TimeUnit.SECONDS) // 5
				// Which will ignore the NoSuchElementException
				.ignoring(NoSuchElementException.class);
	}
	
	/** 
	 * Use when element is on the page or will be on the page. Can be used element is not on the 
	 * page before the ajax call and will be on the page after the ajax call
	 * @param elementId
	 * @param value
	 */
	public static void waitUntilElementGetsValue(WebDriver driver,final String elementId,final String value){
	  new FluentWait(driver)
		  .withTimeout(DEFAULT_TIMEOUT_IN_SECONDS,TimeUnit.SECONDS)
		  .pollingEvery(DEFAULT_SLEEP_TIME_IN_SECONDS,TimeUnit.SECONDS)
		  .ignoring(NoSuchElementException.class)
		  .until(new ExpectedCondition(){
		    public Boolean apply(WebDriver wd){
		      WebElement element=wd.findElement(By.id(elementId));
		      return element.getText().equals(value);
		    }
		  });
	}
	
	/** 
	 * Use when element is already precisely on the page. Throws NoSuchElementException when element is not found
	 * @param elementId
	 * @param value
	 */
	public static void waitUntilElementExistsAndGetsValue(WebDriver driver,final String elementId,final String value){
	  new FluentWait(driver).withTimeout(DEFAULT_TIMEOUT_IN_SECONDS,TimeUnit.SECONDS)
	     .pollingEvery(DEFAULT_SLEEP_TIME_IN_SECONDS,TimeUnit.SECONDS)
	     .until(new ExpectedCondition(){
	    public Boolean apply(WebDriver wd){
	      WebElement element=wd.findElement(By.id(elementId));
	      return element.getText().equals(value);
	    }
	  }
	);
	}
	
	/** 
	 * Waits until given selector elements animated with JS.
	 * @param selector : jQuery element selector
	 */
	public static void waitUntilAnimationCompletes(WebDriver driver ,final String selector){
	  new FluentWait(driver).withTimeout(DEFAULT_TIMEOUT_IN_SECONDS * 2,TimeUnit.SECONDS)
	      .pollingEvery(DEFAULT_ANIMATED_INTERVAL_IN_SECONDS,TimeUnit.MILLISECONDS)
	      .until(new ExpectedCondition(){
	    public Boolean apply(    WebDriver d){
	      return (Boolean)((JavascriptExecutor)d).executeScript("return ! $('" + selector + "').is(':animated');");
	    }
	  });
	}
	
	public static void waitUntilElementExists(WebDriver driver,final By by){
		Wait wait =new FluentWait(driver)
		// Wait for the condition
		  .withTimeout(DEFAULT_TIMEOUT_IN_SECONDS,TimeUnit.SECONDS)
		  // which to check for the condition with interval of 5 seconds.
		  .pollingEvery(DEFAULT_SLEEP_TIME_IN_SECONDS,TimeUnit.SECONDS)
		  // Which will ignore the NoSuchElementException
		  .ignoring(NoSuchElementException.class);
		
		wait.until(new ExpectedCondition(){
		    public Boolean apply(WebDriver wd){	   
		    	logger.info("loading  "+by.toString()+"  ...");
		      return wd.findElement(by).isDisplayed();
		    }
		  } );
	}
	

	
	/**
	 * 隐式等待
	 * implicit  Wait
	 * @param driver
	 * @param waitTime
	 */
	public static void implicitWait(WebDriver driver, long waitTime) {

		driver.manage().timeouts().implicitlyWait(waitTime, TimeUnit.SECONDS);
		
	}

	/**
	 * 强制等待
	 * @param seconds
	 */
	public static void sleep(long seconds) {

		long millis = 1000 * seconds;
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			logger.info("InterruptedException sleep error :" + e.getMessage());
		} catch (Exception e1) {
			logger.info("Exception sleep error e1:" + e1.getMessage());
		}

	}

	/**
	 * 判断是否选中
	 * @param driver WebDriver对象
	 * @param element 目标节点的element
	 * @return
	 */
	public static boolean isSelectedChecked(WebElement element){
    	return element.isSelected();
	}

	
	/**
	 * 判断是否存在元素
	 * @param driver WebDriver对象
	 * @param element  目标元素
	 * @return
	 */
	public static boolean isElementExist(WebDriver driver,By by){
		try{
			return driver.findElement(by).isDisplayed() ;		
		}
		catch(Exception e){
			 return false;
		}	
	}
	
	public static boolean isElementExist(WebElement element){
		try{
			return element.isDisplayed() ;
		
		}
		catch(Exception e){
			 return false;
		}	
	}
	
	/**
	 * 判断是否存在元素
	 * @param wd WebDriver对象
	 * @param xpath 目标节点的xpath
	 * @return
	 */
	public static boolean isElementDisplayed(WebDriver driver,String xpath){
	try{
		return driver.findElement(By.xpath(xpath)).isDisplayed() ;
	
	}catch (NoSuchElementException e) { 
	            return false; 
	        } 
	}
	
	/**
	 * 判断元素是否存在
	 * Below is the syntax which is used to check if the element is displayed or not
	 * @param driver
	 * @param element
	 * @return
	 */
	public static boolean isElementDisplayed(WebDriver driver, WebElement element) {

		try{
			return element.isDisplayed() ;
		
		}catch (NoSuchElementException e) { 
		            return false; 
		        } 
		catch(Exception e)
		{
			return false;
		}
	}
	
	/**
	 *  判断元素是否可用
	 * Below is the syntax which is used to check if the element is enabled or not
	 * @param driver
	 * @param element
	 * @return
	 */
	public static boolean isElementEnabled(WebDriver driver, WebElement element) {

		if(isElementDisplayed(driver,element))
		{
			return element.isEnabled();
		}
		
		return false;
	}

}

 

你可能感兴趣的:(Selenium,WebDriver,等待)