package com.yanfuchang.selenium.utils;

 import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.HasInputDevices;
import org.openqa.selenium.interactions.Keyboard;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait; /**
* 基于selenium的二次封装
*/
public class WebDriverUtil {
private static WebDriver driver = null;
private static Select select = null;
private static Alert alert = null;
private static WebElement element = null;
private static List<WebElement> elementList = null;
private static long timeOutInSeconds = 10;
//--------------------自定义常量------------------------
public final String LINE = "\r\n";
public final String smile = "^_^";
public final String sad = "*o*"; public WebDriverUtil(long timeOutInSeconds) {
WebDriverUtil.timeOutInSeconds = timeOutInSeconds;
} public WebDriverUtil() {} /**
* 指定浏览器打开URL
*/
public static void openBrowser(String url, String browser) {
driver = initBrowser(browser);
driver.manage().timeouts().implicitlyWait(timeOutInSeconds, TimeUnit.SECONDS);
driver.get(url);
}
/**
* 指定浏览器打开URL
*/
public static void openBrowser(String url) {
driver = initBrowser();
driver.get(url);
} /**
* 初始化浏览器,方式1
* Firefox
*/
public static WebDriver initBrowser() {
/*
* 谷歌浏览器 System.setProperty("webdriver.chrome.driver",
* "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
* WebDriver driver = new ChromeDriver(); driver.get("http://www.baidu.com/");
*/
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.manager.showWhenStarting", false);// 是否显示下载进度框
profile.setPreference("browser.offline-apps.notify", false);// 网站保存离线数据时不通知我
profile.setPreference("browser.helperApps.alwaysAsk.force", false);// 应用程序设置不询问
profile.setPreference("browser.download.folderList", 0);// 设置下载地址0是桌面;1是“我的下载”;2是自定义
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream, application/vnd.ms-excel, text/csv, application/zip, application/msword");
profile.setPreference("dom.webnotifications.enabled", false);// 允许通知
WebDriver driver = new FirefoxDriver(profile);// 启动火狐浏览器
driver.manage().window().maximize();// 设置窗口大小
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);// 设置页面加载超时
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);// 设置查询组件等待时间
return driver;
} /**
* 初始化浏览器,方式2
* (ie, ff, chrome)
*/
private static WebDriver initBrowser(String browser) {
switch (browser) {
case "ie":
System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
break;
case "ff":
case "firefox":
case "Firefox":
case "FireFox":
/**
* FireFox安装方式为默认安装:
* FireFox版本小于48
* System.setProperty("webdriver.firefox.marionette", ".\\Tools\\geckodriver.exe");
* FireFox版本大于48,默认安装时可以试试,应该可以
* System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe");
*/
// FireFox安装方式为自定义安装
System.setProperty("webdriver.firefox.bin", "D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
break;
case "chrome":
case "Chrome":
System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");
driver = new ChromeDriver();
break;
default:
try {
throw new Exception("浏览器错误!");
} catch (Exception e) {
e.printStackTrace();
}
}
return driver;
} //----------------------------------------------------元素相关-----------------------------------------------------------------------------
/**
* 查找元素
* @param by 传入一个类型 例如:name
* @param byValue 传入一个类型值 例如:username
*/
public WebElement findElement(String by, String byValue) {
try {
switch (by) {
case "id":
element = driver.findElement(By.id(byValue));
break;
case "name":
element = driver.findElement(By.name(byValue));
break;
case "class":
element = driver.findElement(By.className(byValue));
break;
case "tag":
element = driver.findElement(By.tagName(byValue));
case "link":
element = driver.findElement(By.linkText(byValue));
break;
case "partiallinktext":
element = driver.findElement(By.partialLinkText(byValue));
case "css":
element = driver.findElement(By.cssSelector(byValue));
break;
case "xpath":
element = driver.findElement(By.xpath(byValue));
break;
default:
throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
}
} catch (Exception e) {
System.out.println("没有找到元素:" + byValue);
}
return element;
}
/**
* 查找一组元素
* @param by 传入一个类型 例如:name
* @param byValue 传入一个类型值 例如:username
*/
public List<WebElement> findElements(String by, String byValue) {
try {
switch (by) {
case "id":
elementList = driver.findElements(By.id(byValue));
break;
case "name":
elementList = driver.findElements(By.name(byValue));
break;
case "class":
elementList = driver.findElements(By.className(byValue));
break;
case "tag":
elementList = driver.findElements(By.tagName(byValue));
case "link":
elementList = driver.findElements(By.linkText(byValue));
break;
case "partiallinktext":
elementList = driver.findElements(By.partialLinkText(byValue));
case "css":
elementList = driver.findElements(By.cssSelector(byValue));
break;
case "xpath":
elementList = driver.findElements(By.xpath(byValue));
break;
default:
throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
}
} catch (Exception e) {
System.out.println("没有找到元素:" + byValue);
}
return elementList;
} /**
* 获取单个元素
*/
public WebElement findElementByXpath(String xpath) {
return driver.findElement(By.xpath(xpath));
}
public WebElement findElementByTag(String tag) {
return driver.findElement(By.tagName(tag));
}
public WebElement findElementById(String id) {
return driver.findElement(By.id(id));
}
public WebElement findElementByClassName(String name) {
return driver.findElement(By.className(name));
}
public WebElement findElementByText(String text) {
return driver.findElement(By.linkText(text));
}
public WebElement findElementByPartialText(String text) {
return driver.findElement(By.partialLinkText(text));
}
public WebElement findElementByName(String name) {
return driver.findElement(By.name(name));
} /**
* 获取多个元素
*/
public List<WebElement> findElementsByClassName(String className) {
return driver.findElements(By.className(className));
}
public List<WebElement> findElementsByText(String text) {
return driver.findElements(By.linkText(text));
}
public List<WebElement> findElementsByPartialText(String text) {
return driver.findElements(By.partialLinkText(text));
}
public List<WebElement> findElementsById(String id) {
return driver.findElements(By.id(id));
}
public List<WebElement> findElementsByTag(String tag) {
return driver.findElements(By.tagName(tag));
} /**
* 获取一组元素中的指定元素
*/
public WebElement FindByElements(By by, int index) {
WebElement element = null;
if (this.elementsExists(by)) {
element = driver.findElements(by).get(index);
}
return element;
} /**
* 查找元素并点击
* @param by 传入一个类型 例如:name
* @param byValue 传入一个类型值 例如:username
*/
public boolean findElementClick(String by, String byValue) {
try {
switch (by) {
case "id":
driver.findElement(By.id(byValue)).click();
return true;
case "name":
driver.findElement(By.name(byValue)).click();
return true;
case "class":
driver.findElement(By.className(byValue)).click();
return true;
case "tag":
driver.findElement(By.tagName(byValue)).click();
case "link":
driver.findElement(By.linkText(byValue)).click();
return true;
case "partiallinktext":
driver.findElement(By.partialLinkText(byValue)).click();
case "css":
driver.findElement(By.cssSelector(byValue)).click();
return true;
case "xpath":
driver.findElement(By.xpath(byValue)).click();
return true;
default:
throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
}
} catch (Exception e) {
System.out.println("*****没有找到元素,类型为::" + by + "属性值为:" + byValue + " 的元素或者该元素无法点击****");
return false;
}
} /**
* 定位元素并点击
*/
public void findElementByIdAndClick(String id) {
driver.findElement(By.id(id)).click();
}
public void findElementByNameAndClick(String name) {
driver.findElement(By.name(name)).click();
}
public void findElementByTextAndClick(String text) {
driver.findElement(By.linkText(text)).click();
}
public void findElementByPartiaTextAndClick(String text) {
driver.findElement(By.partialLinkText(text)).click();
}
public void findElementByXpathAndClick(String xpath) {
driver.findElement(By.xpath(xpath)).click();
}
public void findElementByClassNameAndClick(String name) {
driver.findElement(By.className(name)).click();
} /**
* 查找元素并清除文本内容
* @param by 传入一个类型 例如:name
* @param byValue 传入一个类型值 例如:username
*/
public boolean findElementClear(String by, String byValue) {
try {
switch (by) {
case "id":
driver.findElement(By.id(byValue)).clear();
return true;
case "name":
driver.findElement(By.name(byValue)).clear();
return true;
case "class":
driver.findElement(By.className(byValue)).clear();
return true;
case "tag":
driver.findElement(By.tagName(byValue)).clear();
return true;
case "link":
driver.findElement(By.linkText(byValue)).clear();
return true;
case "partiallinktext":
driver.findElement(By.partialLinkText(byValue)).clear();
return true;
case "css":
driver.findElement(By.cssSelector(byValue)).clear();
return true;
case "xpath":
driver.findElement(By.xpath(byValue)).clear();
return true;
default:
throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
}
} catch (Exception e) {
System.out.println("*****没有找到元素,类型为::" + by + "属性值为:" + byValue + " 的元素或者该元素没有输入值****");
return false;
}
} /**
* 查找元素并输入值
* @param by 传入一个类型 例如:name
* @param byValue 传入一个类型值 例如:username
* @param key 填写要输入的值 例如:zhangsan
*/
public boolean findElementSendKeys(String by, String byValue, String key) {
try {
switch (by) {
case "id":
driver.findElement(By.id(byValue)).sendKeys(key);
return true;
case "name":
driver.findElement(By.name(byValue)).sendKeys(key);
return true;
case "class":
driver.findElement(By.className(byValue)).sendKeys(key);
return true;
case "tag":
driver.findElement(By.tagName(byValue)).sendKeys(key);
return true;
case "link":
driver.findElement(By.linkText(byValue)).sendKeys(key);
return true;
case "partiallinktext":
driver.findElement(By.partialLinkText(byValue)).sendKeys(key);
case "css":
driver.findElement(By.cssSelector(byValue)).sendKeys(key);
return true;
case "xpath":
driver.findElement(By.xpath(byValue)).sendKeys(key);
return true;
default:
throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
}
} catch (Exception e) {
System.out.println("*****没有找到元素,类型为::" + by + "属性值为:" + byValue + " 的元素或者该元素无法输入****");
return false;
}
}
/**
* 查找元素并输入值
* @param by 传入一个类型 例如:name
* @param byValue 传入一个类型值 例如:username
* @param key 填写要输入的值 例如:zhangsan
*/
public boolean findElementClearAndSendKeys(String by, String byValue, String key) {
try {
switch (by) {
case "id":
findElementClear(by,byValue);
driver.findElement(By.id(byValue)).sendKeys(key);
return true;
case "name":
findElementClear(by,byValue);
driver.findElement(By.name(byValue)).sendKeys(key);
return true;
case "class":
findElementClear(by,byValue);
driver.findElement(By.className(byValue)).sendKeys(key);
return true;
case "tag":
findElementClear(by,byValue);
driver.findElement(By.tagName(byValue)).sendKeys(key);
return true;
case "link":
findElementClear(by,byValue);
driver.findElement(By.linkText(byValue)).sendKeys(key);
return true;
case "partiallinktext":
findElementClear(by,byValue);
driver.findElement(By.partialLinkText(byValue)).sendKeys(key);
case "css":
findElementClear(by,byValue);
driver.findElement(By.cssSelector(byValue)).sendKeys(key);
return true;
case "xpath":
findElementClear(by,byValue);
driver.findElement(By.xpath(byValue)).sendKeys(key);
return true;
default:
throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
}
} catch (Exception e) {
System.out.println("*****没有找到元素,类型为::" + by + "属性值为:" + byValue + " 的元素或者该元素无法输入****");
return false;
}
} /**
* 定位元素并清空文本内容,输入相应的值
*/
public void findElementByIdAndClearSendkeys(String id, String text) {
driver.findElement(By.id(id)).clear();
driver.findElement(By.id(id)).sendKeys(text);
}
public void findElementByIdAndClearSendkeys(String id, int num) {
driver.findElement(By.id(id)).clear();
driver.findElement(By.id(id)).sendKeys(num + "");
}
public void findElementByNameAndClearSendkeys(String name, String text) {
driver.findElement(By.name(name)).clear();
driver.findElement(By.name(name)).sendKeys(text);
}
public void findElementByNameAndClearSendkeys(String name, int num) {
driver.findElement(By.name(name)).clear();
driver.findElement(By.name(name)).sendKeys(num + "");
}
public void findElementByXpathAndClearSendkeys(String xpath, String text) {
findElementByXpath(xpath).clear();
findElementByXpath(xpath).sendKeys(text);
}
public void findElementByXpathAndClearSendkeys(String xpath, int num) {
findElementByXpath(xpath).clear();
findElementByXpath(xpath).sendKeys(num + "");
}
public void findElementByClassnameAndClearSendkeys(String classname, String text) {
driver.findElement(By.className(classname)).clear();
driver.findElement(By.className(classname)).sendKeys(text);
}
public void findElementByClassnameAndClearSendkeys(String classname, int num) {
driver.findElement(By.className(classname)).clear();
driver.findElement(By.className(classname)).sendKeys(num + "");
} /**
* 定位元素,并获取其文本内容
*/
public String getTextByXpath(String xpath) {
return findElementByXpath(xpath).getText();
}
public String getTextByClassName(String name) {
return findElementByClassName(name).getText();
}
public String getTextById(String id) {
return findElementById(id).getText();
}
public String getTextByName(String name) {
return findElementByName(name).getText();
} /**
* 定位元素,并指定点击次数(连续点击)
*/
public boolean clickById(String id, int clickCount) {
try {
for (int i = 0; i < clickCount; i++) {
driver.findElement(By.id(id)).click();
}
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
public boolean clickByXpath(String xpath, int clickCount) {
try {
for (int i = 0; i < clickCount; i++) {
driver.findElement(By.xpath(xpath)).click();
}
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
public boolean clickByCss(String css, int clickCount) {
try {
for (int i = 0; i < clickCount; i++) {
driver.findElement(By.cssSelector(css)).click();
}
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} // 判断元素是否存在
public boolean exists(By selector) {
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 设置查询组件等待时间
try {
driver.findElement(selector);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);// 设置查询组件等待时间
return true;
} catch (Exception e) {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);// 设置查询组件等待时间
return false;
}
}
/**
* 判断一个元素是否存在
*/
public boolean isElementExist(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
} /**
* 判断一组元素是否存在
*/
public boolean elementsExists(By by) {
return (driver.findElements(by).size() > 0) ? true : false;
} //---------------------------------------判断页面是否包含指定文本---------------------------------------------------------
/**
* 1、指定时间内等待直到页面包含文本字符串
* @param text 期望出现的文本
* @param seconds 超时时间
* @return Boolean 检查给定文本是否存在于指定元素中, 超时则捕获抛出异常TimeoutException并返回false
* @see org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElement(WebElement
* element, String text)
*/
public static Boolean waitUntilPageContainText(String text, long seconds) {
try {
return new WebDriverWait(driver, seconds)
.until(ExpectedConditions.textToBePresentInElement(driver.findElement(By.tagName("body")), text));
} catch (Exception e) {
e.printStackTrace();
return false;
}
} /**
* 2、默认时间等待直到页面包含文本字符串
* @param text 期望出现的文本
* @return Boolean 检查给定文本是否存在于指定元素中, 超时则捕获抛出异常TimeoutException并返回false
* @see org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElement(WebElement
* element, String text)
*/
public static Boolean waitUntilPageContainText(String text) {
try {
return new WebDriverWait(driver, timeOutInSeconds)
.until(ExpectedConditions.textToBePresentInElement(driver.findElement(By.tagName("body")), text));
} catch (Exception e) {
e.printStackTrace();
return false;
}
} //---------------------------------------元素判断---------------------------------------------------------
/**
* 1、指定时间内等待直到元素存在于页面的DOM上并可见, 可见性意味着该元素不仅被显示, 而且具有大于0的高度和宽度
* @param locator 元素定位器
* @param seconds 超时时间
* @return Boolean 检查给定元素的定位器是否出现, 超时则捕获抛出异常TimeoutException并返回false
* @see org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated(By
* locator)
*/
public static Boolean waitUntilElementVisible(By locator, int seconds) {
try {
new WebDriverWait(driver, seconds).until(ExpectedConditions.visibilityOfElementLocated(locator));
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} /**
* 2、默认时间内等待直到元素存在于页面的DOM上并可见, 可见性意味着该元素不仅被显示, 而且具有大于0的高度和宽度
* @param locator 元素定位器
* @return Boolean 检查给定元素的定位器是否出现, 超时则捕获抛出异常TimeoutException并返回false
* @see org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated(By
* locator)
*/
public static Boolean waitUntilElementVisible(By locator) {
try {
new WebDriverWait(driver, timeOutInSeconds).until(ExpectedConditions.visibilityOfElementLocated(locator));
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} /**
* 判断元素是否显示
*/
public boolean getDisplayStatById(String id) {
return driver.findElement(By.id(id)).isDisplayed();
}
public boolean getDisplayStatByXpath(String xpath) {
return driver.findElement(By.xpath(xpath)).isDisplayed();
}
public boolean getDisplayStatByCss(String css) {
return driver.findElement(By.cssSelector(css)).isDisplayed();
}
/**
* 判断元素是否可写
*/
public boolean getEnableStatById(String id) {
return driver.findElement(By.id(id)).isEnabled();
}
public boolean getEnableStatByXpath(String xpath) {
return driver.findElement(By.xpath(xpath)).isEnabled();
}
public boolean getEnableStatByCss(String css) {
return driver.findElement(By.cssSelector(css)).isEnabled();
}
/**
* 判断元素是否选中
*/
public boolean getSelectStatById(String id) {
return driver.findElement(By.id(id)).isSelected();
}
public boolean getSelectStatByXpath(String xpath) {
return driver.findElement(By.xpath(xpath)).isSelected();
}
public boolean getSelectStatByCss(String css) {
return driver.findElement(By.cssSelector(css)).isSelected();
} /**
* 获取当前焦点所在页面元素的属性值(name,value,id,src等等)
*/
public String getFocusAttributeValue(String attribute) {
String value = "";
try {
Thread.sleep(333);
} catch (Exception e) {
e.printStackTrace();
}
value = driver.switchTo().activeElement().getAttribute(attribute);
System.out.println("The focus Element's " + attribute + "attribute value is>>" + value);
return value;
} // 等待元素可用再点击
public void waitForEnabledByXpathAndClick(String xpath) throws InterruptedException {
boolean key = true;
while (key) {
if (findElementByXpath(xpath).isEnabled() && findElementByXpath(xpath).isDisplayed()) {
clickByJsByXpath(xpath);
key = false;
} else {
sleep(0);
}
}
}
// 自定义等待时间
public static void sleep(int key) throws InterruptedException {
switch (key) {
case 0:
Thread.sleep(500);
break;
case 1:
Thread.sleep(2000);
break;
case 2:
Thread.sleep(5000);
break;
default:
System.out.println("错误");
break;
}
} //---------------------------------------下拉列表操作---------------------------------------------------------
// 根据id获取下拉框,根据index选择选项
public void findSelectByIdAndSelectByIndex(String id, int index) {
Select select = new Select(findElementById(id));
select.selectByIndex(index);
}
// 根据id获取下拉框,根据value选择选项
public void findSelectByIdAndSelectByValue(String id, String value) {
Select select = new Select(findElementById(id));
select.selectByValue(value);
}
// 根据id获取下拉框,根据text选择选项
public void findSelectByIdAndSelectByText(String id, String text) {
Select select = new Select(findElementById(id));
select.selectByVisibleText(text);
} // 根据classname获取下拉框,根据text选择选项
public void findSelectByClassNameAndSelectByText(String name, String text) {
Select select = new Select(findElementByClassName(name));
select.selectByVisibleText(text);
}
// 根据classname获取下拉框,根据Value选择选项
public void findSelectByClassNameAndSelectByValue(String name, String value) {
Select select = new Select(findElementByClassName(name));
select.selectByValue(value);
}
// 根据classname获取下拉框,根据index选择选项
public void findSelectByClassNameAndSelectByIndex(String name, int index) {
Select select = new Select(findElementByClassName(name));
select.selectByIndex(index);
} // 根据name获取下拉框,根据text选择选项
public void findSelectByNameAndSelectByText(String name, String text) {
Select select = new Select(findElementByName(name));
select.selectByVisibleText(text);
}
// 根据name获取下拉框,根据Value选择选项
public void findSelectByNameAndSelectByValue(String name, String value) {
Select select = new Select(findElementByName(name));
select.selectByValue(value);
}
// 根据name获取下拉框,根据index选择选项
public void findSelectByNameAndSelectByIndex(String name, int index) {
Select select = new Select(findElementByName(name));
select.selectByIndex(index);
} /**
* 定位select并选中对应text的option
* @param locator
* @param text
* @see org.openqa.selenium.support.ui.Select.selectByVisibleText(String text)
*/
public static void selectByText(By locator, String text) {
select = new Select(driver.findElement(locator));
select.selectByVisibleText(text);
} /**
* 定位select并选中对应index的option
* @param locator
* @param index
* @see org.openqa.selenium.support.ui.Select.selectByIndex(int index)
*/
public static void selectByIndex(By locator, int index) {
select = new Select(driver.findElement(locator));
select.selectByIndex(index);
} /**
* 定位select并选中对应value值的option
* @param locator 定位select的选择器
* @param value option 中的value值
* @see org.openqa.selenium.support.ui.Select.selectByValue(String value)
*/
public static void selectByValue(By locator, String value) {
select = new Select(driver.findElement(locator));
select.selectByValue(value);
} //---------------------------------------弹框操作---------------------------------------------------------
// 判断是否有弹框
public boolean isAlertPresent() {
try {
alert = driver.switchTo().alert();
return true;
} catch (NoAlertPresentException Ex) {
return false;
}
}
// 接受弹出框
public void acceptAlert() {
if (this.isAlertPresent()) {
alert.accept();
}
}
// 取消弹出框
public void dimissAlert() {
if (this.isAlertPresent()) {
alert.dismiss();
}
}
// 获取弹出内容
public String getAlertText() {
String text = null;
if (this.isAlertPresent()) {
text = alert.getText();
} else {
// todo:log;
}
return text;
}
// 弹出对话框输入文本字符串
public void inputTextToAlert(String text) {
if (this.isAlertPresent()) {
alert.sendKeys(text);
} else {
// todo:log;
}
} //---------------------------------------窗口和iframe---------------------------------------------------------
/**
* 切换到当前页面
*/
public static void switchToCurrentPage() {
String handle = driver.getWindowHandle();
for (String tempHandle : driver.getWindowHandles()) {
if (tempHandle.equals(handle)) {
driver.close();
} else {
driver.switchTo().window(tempHandle);
}
}
}
/**
* 切换到指定title的窗口
*/
public void switchToWindow(String windowTtitle) {
Set<String> windowHandles = driver.getWindowHandles();
for (String handler : windowHandles) {
driver.switchTo().window(handler);
String title = driver.getTitle();
if (windowTtitle.equals(title)) {
break;
}
}
} /**
* 切换至父级frame
* @see org.openqa.selenium.WebDriver.TargetLocator.parentFrame()
*/
public static void switchToParentFrame() {
driver.switchTo().parentFrame();
} /**
* 切换默认最外层frame或者窗口
* @return 这个驱动程序聚焦在顶部窗口/第一个frame上
* @see org.openqa.selenium.WebDriver.TargetLocator.defaultContent()
*/
public static void switchToDefault() {
driver.switchTo().defaultContent();
} /**
* 切换到指定iframe
*/
public void switchToFrameById(String frameId) {
driver.switchTo().frame(frameId);
}
public void switchToFrameByIndex(int index) {
driver.switchTo().frame(index);
}
public void switchToframeByElement(By locator) {
driver.switchTo().frame(driver.findElement(locator));
} /**
* 提交表单
* @see org.openqa.selenium.WebElement.submit()
*/
public static void submitForm(By locator) {
driver.findElement(locator).submit();
} /**
* 上传文件
*/
public static void uploadFile(By locator, String filePath) {
driver.findElement(locator).sendKeys(filePath);
} //---------------------------------------JS操作---------------------------------------------------------
// JS点击指定元素
public void clickByJs(WebElement element) {
((JavascriptExecutor) driver).executeScript("arguments[0].click()", element);
} // 定位元素触发JS点击事件
public void clickByJsByXpath(String xpath) {
clickByJs(driver.findElement(By.xpath(xpath)));
}
public void clickByJsByText(String text) {
clickByJs(findElementByText(text));
}
public void clickByJsById(String id) {
clickByJs(findElementById(id));
}
public void clickByJsByClassName(String name) {
clickByJs(findElementByClassName(name));
}
public void clickByJsByName(String name) {
clickByJs(findElementByName(name));
} // 滚动到窗口最上方
public void scrollToTop() {
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,0);");
}
// 滚动到页面底部
public void scrollToBottom(String id) {
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,10000);");
}
// 滚动到某个元素
public void scrollToElement(WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", element);
}
// js给指定元素value赋值
public void inputTextByJs(String text, WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value=" + text + "\"", element);
}
// js使元素隐藏元素显示
public void makeElementDisplay(WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style=arguments[1]", element, "display: block;");
} //---------------------------------------浏览器操作---------------------------------------------------------
/**
* 关闭当前浏览器
*/
public static void closeCurrentBrowser() {
driver.close();
}
/**
* 关闭所有selenium驱动打开的浏览器
*/
public static void closeAllBrowser() {
driver.quit();
}
/**
* 最大化浏览器
*/
public static void maxBrowser() {
driver.manage().window().maximize();
} /**
* 自定义设置浏览器尺寸
*/
public static void setBrowserSize(int width, int heigth) {
driver.manage().window().setSize(new Dimension(width, heigth));
} /**
* 获取网页的title值
*/
public String getTitle() {
return driver.getTitle();
} /**
* 获取当前url字符串
*/
public static String getURL() {
return driver.getCurrentUrl();
} /**
* 上一个页面(点击浏览器返回)
*/
public static void returnToPreviousPage() {
driver.navigate().back();
} /**
* 下一个页面(如果没有下一个页面则什么都不做)
* 浏览器上的前进
*/
public static void forwardToNextPage() {
driver.navigate().forward();
} /**
* 刷新页面
*/
public static void refreshPage() {
driver.navigate().refresh();
} // 强制刷新页面
public void refresh() {
Actions ctrl = new Actions(driver);
ctrl.keyDown(Keys.CONTROL).perform();
try {
pressKeyEvent(KeyEvent.VK_F5);
} catch (AWTException e) {
e.printStackTrace();
}
ctrl.keyUp(Keys.CONTROL).perform();
} /**
* 判断是否加载有JQuery
*/
public Boolean JQueryLoaded() {
Boolean loaded;
JavascriptExecutor js = (JavascriptExecutor) driver;
try {
loaded = (Boolean) js.executeScript("return" + "JQuery()!=null");
} catch (WebDriverException e) {
loaded = false;
}
return loaded;
}
//---------------------------------------屏幕截图---------------------------------------------------------
public void screenShot(WebDriver driver) {
String dir_name = "screenshot";
if (!(new File(dir_name).isDirectory())) {
new File(dir_name).mkdir();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
String time = sdf.format(new Date());
try {
File source_file = (((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE));// 执行截屏
FileUtils.copyFile(source_file, new File(dir_name + File.separator + time + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
// 截图命名为当前时间保存桌面
public void takeScreenshotByNow() throws IOException {
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
String time = sdf.format(new Date());
String file = "C:\\Users\\zhangsan\\Desktop\\picture\\" + time + ".png";
FileUtils.copyFile(srcFile, new File(file));
}
// 截图重命名保存至桌面
public void takeScreenshotByName(String name) throws IOException {
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String file = "C:\\Users\\zhangsan\\Desktop\\picture\\" + name + ".png";
FileUtils.copyFile(srcFile, new File(file));
} //---------------------------------------键盘操作---------------------------------------------------------
// 获取键盘
public Keyboard getKeyboard() {
return ((HasInputDevices) driver).getKeyboard();
}
// 模拟crtrl+F5
public void refreshWithCtrlF5() {
getKeyboard().sendKeys(Keys.CONTROL, Keys.F5);
}
/**
* 按物理按键(KeyEvent类中查找相关的常量)
* 例子:
* Robot robot = new Robot();
* robot.keyPress(KeyEvent.VK_ENTER);//按下enter键
*/
public void pressKeyEvent(int keycode) throws AWTException {
Robot robot = new Robot();
robot.keyPress(keycode);
} //---------------------------------------鼠标操作---------------------------------------------------------
// 鼠标悬浮指定元素并点击
public void moveToElementById(String id) {
Actions actions = new Actions(driver);
actions.moveToElement(findElementById(id)).perform();
}
public void moveToElementByClassName(String name) {
Actions actions = new Actions(driver);
actions.moveToElement(findElementByClassName(name)).perform();
} // 鼠标右键点击
public void RightClickWebElement(String id) {
Actions actions = new Actions(driver);
actions.contextClick(findElementById(id)).perform();
}
// 鼠标双击
public void DoubleClickWebElement(String id) {
Actions actions = new Actions(driver);
actions.doubleClick(findElementById(id)).perform();
} /**
* 模拟点击键盘上的键:
* keyDown()按下
* keyUp()抬起,松开
*
* 常见的键:
* Keys.SHIFT Keys.ALT Keys.Tab
*/
public void ClickCtrl(String id) {
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL);//按下control键
actions.keyUp(Keys.CONTROL);//松开control键
} /**
* 模拟键盘输入关键字到输入框
*/
public void sendText(By by,String text) {
Actions actions = new Actions(driver);
actions.sendKeys(driver.findElement(by),text).perform();
} /**
* 模拟鼠标移动到指定元素,并点击
*/
public void moveToElementAndClick(By by,String text) {
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(by)).click().perform();
} /**
* 模拟鼠标点击和释放
*/
public void clickHoldAndRelease(By by) {
Actions actions = new Actions(driver);
actions.clickAndHold(driver.findElement(by)).perform();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
actions.release(driver.findElement(by)).perform();
} //---------------------------------------Cookie操作---------------------------------------------------------
/**
* 获取当前域所有的cookies
* @return Set&lt;Cookie> 当前的cookies集合
* @see org.openqa.selenium.WebDriver.Options.getCookies()
*/
public static Set<Cookie> getAllCookies() {
return driver.manage().getCookies();
}
// 输出cookies信息
public void outputCookie() {
Set<Cookie> cookie = driver.manage().getCookies();
System.out.println(cookie);
}
//添加cookie信息
public void addCookie(Map<String, String> args) {
Set<String> keys = args.keySet();
for (String key : keys) {
driver.manage().addCookie(new Cookie(key, args.get(key)));
}
}
/**
* 用给定的name和value创建默认路径的Cookie并添加, 永久有效
* @param name
* @param value
* @see org.openqa.selenium.WebDriver.Options.addCookie(Cookie cookie)
* @see org.openqa.selenium.Cookie.Cookie(String name, String value)
*/
public static void addCookie(String name, String value) {
driver.manage().addCookie(new Cookie(name, value));
} /**
* 用给定的name和value创建指定路径的Cookie并添加, 永久有效
* @param name cookie名称
* @param value cookie值
* @param path cookie路径
*/
public static void addCookie(String name, String value, String path) {
driver.manage().addCookie(new Cookie(name, value, path));
}
/**
* 根据cookie名称删除cookie
* @param name cookie的name值
* @see org.openqa.selenium.WebDriver.Options.deleteCookieNamed(String name)
*/
public static void deleteCookie(String name) {
driver.manage().deleteCookieNamed(name);
}
/**
* 删除当前域的所有Cookie
* @see org.openqa.selenium.WebDriver.Options.deleteAllCookies()
*/
public static void deleteAllCookies() {
driver.manage().deleteAllCookies();
} /**
* 根据名称获取指定cookie
* @param name cookie名称
* @return Map&lt;String, String>, 如果没有cookie则返回空, 返回的Map的key值如下:
* <ul>
* <li><tt>name</tt> <tt>cookie名称</tt>
* <li><tt>value</tt> <tt>cookie值</tt>
* <li><tt>path</tt> <tt>cookie路径</tt>
* <li><tt>domain</tt> <tt>cookie域</tt>
* <li><tt>expiry</tt> <tt>cookie有效期</tt>
* </ul>
* @see org.openqa.selenium.WebDriver.Options.getCookieNamed(String name)
*/
public static Map<String, String> getCookieByName(String name) {
Cookie cookie = driver.manage().getCookieNamed(name);
if (cookie != null) {
Map<String, String> map = new HashMap<String, String>();
map.put("name", cookie.getName());
map.put("value", cookie.getValue());
map.put("path", cookie.getPath());
map.put("domain", cookie.getDomain());
map.put("expiry", cookie.getExpiry().toString());
return map;
}
return null;
} //---------------------------------------远程---------------------------------------------------------
/**
* 进入测试,打开浏览器,输入网址,打开网页
*
* @param remoteUrl 远程服务器地址
* @param pageUrl 测试页面地址
*/
public boolean startTest(String remoteUrl, String pageUrl) {
try {
try {
driver = new RemoteWebDriver(new URL(remoteUrl), DesiredCapabilities.firefox());
} catch (MalformedURLException e) {
e.printStackTrace();
}
driver.get(pageUrl);
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
} /**
* 进入测试,打开浏览器,输入网址,打开网页
* @param explore 调用的浏览器,需要启动不同的server
* 如:firefox,需要运行selenium-server-standalone-2.33.0.jar。
* IE,则需运行IEDriverServer.exe
* @param remoteUrl 远程服务器地址
* @param pageUrl 测试页面地址
*/
public boolean startTest(String explore, String remoteUrl, String pageUrl) {
try {
try {
if ("f".equals(explore)) {
System.out.println("firefox");
driver = new RemoteWebDriver(new URL(remoteUrl), DesiredCapabilities.firefox());
} else if ("ie".equals(explore)) {
System.out.println("internet explorer");
DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
driver = new RemoteWebDriver(new URL(remoteUrl), cap);
} else {
System.out.println("firefox");
driver = new RemoteWebDriver(new URL(remoteUrl), DesiredCapabilities.firefox());
}
} catch (Exception e) {
e.printStackTrace();
}
driver.get(pageUrl);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
}

Selenium二次封装-Java版本的更多相关文章

  1. Selenium二次封装-Python版本

    from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...

  2. selenium2.0的初步封装(java版本)

    我们都知道, 在本地创建java项目后,引入selenium-java-2.35.0.jar   selenium-support-2.35.0.jar junit-4.8.1.jar等等jar包之后 ...

  3. Selenium使用总结(Java版本)

    硒在最近的发展中被广泛应用,因为它以前没有被使用过,并且已经走了太多的坑.这是一张唱片. 1.环境配置 配置要点: 1.Web驱动程序应该与浏览器版本相对应,chrome使用chrome驱动程序和ch ...

  4. 《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)

    1.简介 上一篇宏哥介绍了如何设计支持不同浏览器测试,宏哥的方法就是通过来切换配置文件设置的浏览器名称的值,来确定启动什么浏览器进行脚本测试.宏哥将这个叫做浏览器引擎类.这个类负责获取浏览器类型和启动 ...

  5. selenium 使用教程详解-java版本

    第一章 Selenium 概述 1.1.Selenium 发展史 ​ Selenium是一系列基于Web的自动化工具,提供一套测试函数,用于支持Web自动化测试.函数非常灵活,能够完成界面元素定位.窗 ...

  6. python+selenium十:基于原生selenium的二次封装

    from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium ...

  7. Python_selenium二次封装selenium的几个方法

    Python_selenium二次封装selenium的几个方法 将常用的几个webdriver方法封装到自己写的一个类中去,此实例中是将"浏览器后退.浏览器前进.打开站点和关闭浏览器&qu ...

  8. selenium + python自动化测试unittest框架学习(五)webdriver的二次封装

    因为webdriver的api方法很长,再加上大多数的定位方式是以xpath方式定位,更加让代码看起来超级长,为了使整体的代码看起来整洁,对webdriver进行封装,学习资料来源于虫师的<se ...

  9. Python+Selenium中级篇之-二次封装Selenium中几个方法

    本文来介绍,如何把常用的几个webdriver的方法封装到自己写的一个类中去,这个封装过程叫二次封装Selenium方法.我们把打开站点,浏览器前进和后退,关闭和退出浏览器这这个方法封装到一个新写的类 ...

随机推荐

  1. eclipse部署和启动guns

    eclipse部署guns: 1.import -> 搜索maven -> Existing Maven Projects -> 选择guns根目录 2.修改配置文件: spring ...

  2. 30 python 并发编程之多线程

    一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍 官网链接:https://docs.python ...

  3. New Concept English three (51)

    22 76 Predicting the future is notoriously difficult. Who could have imagined, in the mid 1970s, for ...

  4. WC2018 即时战略

    交互题 一棵树,一开始只有 1 号点是已知的,其他的都是未知的,你可以调用函数 explore(x,y) ,其中 x 必须是已知的,函数会找到 x 到 y 路径上第二个点,并把它标成已知,求最小步数使 ...

  5. UVA - 11768 Lattice Point or Not (扩展欧几里得)

    求一条线段上有多少个整点. 是道扩欧基础题,列出两点式方程,然后分四种情况讨论即可.但细节处理较多很容易写挫(某zzWA了十几发才过掉的). 由于数据精度较小,浮点数比较没有用eps,直接==比较了. ...

  6. CH5E02 [IOI1999]花店橱窗[暴力dp]

    众所周知,这个人太菜了,所以她又来切水题了. 显然设计状态表示第$i$朵花放第$j$瓶中的最大价值.然后瞎转移一波是n三方的,加个前缀max变成n方就水过去了. 当然这题可以搜索剪枝的. 虐lyd书上 ...

  7. Visualforce Page CSS样式

    Salesforce Page开发者文档:https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_stylin ...

  8. 服务监控Zabbix和Nagios的继任者

    本文转载自:https://blog.csdn.net/moonpure/article/details/78633668 为了调研市场,从而做出更好的监控工具,David Gildeh 曾采访了超过 ...

  9. shell脚本中常用命令

    1           Shell中的特殊符号 1.1           $  美元符号.用来表示变量的值.如变量NAME的值为Mike,则使用$NAME就可以得到“Mike”这个值. 1.2    ...

  10. AngularJS:模块

    ylbtech-AngularJS:模块 1.返回顶部 1. AngularJS 模块 模块定义了一个应用程序. 模块是应用程序中不同部分的容器. 模块是应用控制器的容器. 控制器通常属于一个模块. ...