基于selenium的web页面点击功能的一层封装

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.UnexpectedTagNameException;

import net.bytebuddy.agent.builder.AgentBuilder.RedefinitionStrategy.Listener.BatchReallocator;

public class WebTagEdit {
	
	private static String chromeDriverPath;
	private static ChromeOptions chromeOptions;
	private static WebDriver chromeDriver;
	private static DesiredCapabilities capabilities;
	private static JavascriptExecutor javascriptExecutor;
	private static String mainHandle;
	
	/**
	 * 初始化
	 */
	public WebTagEdit() {
		chromeDriverPath = System.getProperty("user.dir")+"\\chromedriver.exe";
		//设置驱动位置
		System.setProperty("webdriver.chrome.driver", chromeDriverPath);
		//{@link ChromeDriver}配置的管理对象
		chromeOptions = new ChromeOptions();
		capabilities = DesiredCapabilities.chrome();
		capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
		//实例化浏览器对象,通过selenium打开chrome客户端
		chromeDriver = new ChromeDriver(capabilities);
		javascriptExecutor = (JavascriptExecutor)chromeDriver;
		
	}
	
	/**
	 * 初始化
	 */
	public WebTagEdit(HashMap chromePrefs) {
		chromeDriverPath = System.getProperty("user.dir")+"\\chromedriver.exe";
		//设置驱动位置
		System.setProperty("webdriver.chrome.driver", chromeDriverPath);
		//{@link ChromeDriver}配置的管理对象
		chromeOptions = new ChromeOptions();
		chromeOptions.setExperimentalOption("prefs", chromePrefs);
		capabilities = DesiredCapabilities.chrome();
		capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
		//实例化浏览器对象,通过selenium打开chrome客户端
		chromeDriver = new ChromeDriver(capabilities);
		javascriptExecutor = (JavascriptExecutor)chromeDriver;
		
	}
	
	/**
	 * 向url指定网页发出GET请求
	 * 
	 * @param url	指定网页
	 * @return		是否访问成功
	 */
	public boolean GETRequest(String url) {
		
		boolean bRet = false;
		
		while(true) {
			
			chromeDriver.get(url);
			
			bRet = true;
			break;
		}
		
		return bRet;
	}
	
	/**
	 * 设置input标签的内容
	 * 
	 * @param tagID		标签id
	 * @param Content	需要写入的内容
	 * @return			是否写入成功
	 */
	public boolean setText(String tagID, String Content) {
		
		boolean bRet = false;
		
		try {
			
			WebElement mTag = chromeDriver.findElement(By.id(tagID));
			mTag.sendKeys(Content);
			
			bRet = true;
			
		} catch (NoSuchElementException e) {
			// TODO: handle exception
			System.out.printf("Not find tag: %s", tagID);
		}
		
		return bRet;
	}
	
	/**
	 * 设置select标签的选项
	 * @param tagID	标签id
	 * @param value	选项的value值
	 * @return		是否设置成功
	 */
	public boolean setSelectTag(String tagID, String value) {
		
		boolean bRet = false;
		
		try {
			
			WebElement mWebElement = chromeDriver.findElement(By.id(tagID));
			Select mSelect = new Select(mWebElement);
			mSelect.selectByValue(value);
			bRet = true;
			
		}catch (NoSuchElementException e) {
			// TODO: handle exception
			System.out.printf("Not find tag: %s", tagID);
			
		}catch (UnexpectedTagNameException e) {
			// TODO: handle exception
			System.out.printf("%s is not a SELECT", tagID);
		}
		
		return bRet;
	}
	
	/**
	 * 执行JS函数
	 * 
	 * @param functionName	函数名称
	 * @return				是否执行成功
	 */
	public boolean JSclik(String functionName) {
		
		boolean bRet = false;
		
		try {
			
			javascriptExecutor.executeScript(functionName);
			
			bRet = true;
		}catch (Exception e) {
			// TODO: handle exception
			System.out.printf("JavaScript function %s execute failed!\n", functionName);
			
		}
		
		return bRet;
	}
	
	/**
	 * 根据xpath字符串来匹配控件
	 * 
	 * @param xpath	xpath特征字符串
	 * @return		是否获取到控件
	 */
	public WebElement getWebElement(String xpath) {
		
		WebElement mElement = null;
		
		while(true) {
			mElement = chromeDriver.findElement(By.xpath(xpath));
			
			if(mElement == null) {
				System.out.printf("Not find tag for %s", xpath);
			}
			
			break;
		}
		
		return mElement;
	}
	
	/**
	 * 根据xpath字符串来匹配控件列表
	 * 
	 * @param xpath	xpath特征字符串
	 * @return		是否获取到控件
	 */
	public List getWebElementList(String xpath) {
		
		List mElements = null;
		
		while(true) {
			mElements = chromeDriver.findElements(By.xpath(xpath));
			
			if(mElements == null) {
				System.out.printf("Not find tag for %s", xpath);
			}
			
			break;
		}
		
		return mElements;
	}
	
	/**
	 * 获取标签为tr的所有id值
	 * 
	 * @param mElements	tr列表
	 * @return			返回获取的id字符串列表
	 */
	public List getTrIDs(List mElements){
		
		List idStrings = new ArrayList();
		
		for(int i=0; i getAllWindowHandle(){
		
		Set handles = chromeDriver.getWindowHandles();
		
		return handles;
	}
	
	/**
	 * 切换窗口句柄
	 * 
	 * @param index	句柄索引
	 */
	public void switchWindowHandle(String handle) {
		
		chromeDriver.switchTo().window(handle);
	}
	
	/**
	 * 切换到弹窗句柄
	 * 
	 * @return 返回弹窗句柄
	 */
	public Alert switchAlertHandl() {
		
		Alert mAlert = null;
		
		try {
			mAlert = chromeDriver.switchTo().alert();
		}catch (NoAlertPresentException e) {
			// TODO: handle exception
		}
		
		return mAlert;
	}
	
	/**
	 * 关闭当前窗口
	 */
	public void closeCurrentWindow() {
		
		chromeDriver.close();
	}
	
	/**
	 * 关闭所有窗口
	 */
	public void closeAllWindows() {
		
		chromeDriver.quit();
	}
}

你可能感兴趣的:(JAVA)