package com.jlkj_atlantis_ht_test.utils;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.security.MessageDigest;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
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.StaleElementReferenceException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.ITestResult;
import com.jlkj_atlantis_ht_test.utils.SeleniumUtil;
import com.jlkj_atlantis_ht_test.utils.HttpClientUtil;
import net.minidev.json.JSONObject;
/**
* @Description 包装所有selenium的操作以及通用方法,简化用例中代码量
* */
public class SeleniumUtil {
/** 使用Log4j,第一步就是获取日志记录器,这个记录器将负责控制日志信息 */
public static Logger logger = Logger.getLogger(SeleniumUtil.class.getName());
public ITestResult it = null;
public WebDriver driver = null;
public WebDriver window = null;
public String current_handles="";
/***
* 启动浏览器并打开页面
* */
public void launchBrowser(String browserName, ITestContext context,String webUrl,int timeOut) {
SelectBrowser select = new SelectBrowser();
driver = select.selectExplorerByName(browserName, context);
try {
maxWindow(browserName);
waitForPageLoading(timeOut);
get(webUrl);
} catch (TimeoutException e) {
logger.warn("注意:页面没有完全加载出来,刷新重试!!");
refresh();
JavascriptExecutor js = (JavascriptExecutor)driver;
String status= (String)js.executeScript("return document.readyState");
logger.info("打印状态:"+status);
}
}
/**
* 最大化浏览器操作
* */
public void maxWindow(String browserName) {
logger.info("最大化浏览器:" + browserName);
driver.manage().window().maximize();
}
/**
* 设定浏览器窗口大小: 设置浏览器窗口的大小有下面两个比较常见的用途:<br>
* 1、在统一的浏览器大小下运行用例,可以比较容易的跟一些基于图像比对的工具进行结合
* ,提升测试的灵活性及普遍适用性。比如可以跟sikuli结合,使用sikuli操作flash;<br>
* 2、在不同的浏览器大小下访问测试站点,对测试页面截图并保存,然后观察或使用图像比对工具对被测页面的前端样式进行评测。
* 比如可以将浏览器设置成移动端大小(320x480),然后访问移动站点,对其样式进行评估;<br>
* */
public void setBrowserSize(int width, int height) {
driver.manage().window().setSize(new Dimension(width, height));
}
/**
* 包装查找元素的方法 element
* */
public WebElement findElementBy(By by) {
return driver.findElement(by);
}
/**
* 包装查找元素的方法 elements
* */
public List<WebElement> findElementsBy(By by) {
return driver.findElements(by);
}
/**
* 这是一堆相同的elements中 选择 其中 一个
* */
public WebElement findElementBy(By bys, int index) {
return findElementsBy(bys).get(index);
}
/**导航链接到url*/
public void navigateTargetUrl(String url){
driver.navigate().to(url);
logger.info("导航到:"+url);
}
/**
* 包装点击操作- By
* */
public void click(By byElement) {
try {
clickTheClickable(byElement, System.currentTimeMillis(), 2500);
} catch (StaleElementReferenceException e) {
logger.error("The element you clicked:[" + byElement + "] is no longer exist!");
Assert.fail("The element you clicked:[" + byElement + "] is no longer exist!");
} catch (Exception e) {
logger.error("Failed to click element [" + byElement + "]");
Assert.fail("Failed to click element [" + byElement + "]",e);
}
logger.info("点击元素 [" + byElement + "]");
}
public void click(int timeOut,By byElement) {
logger.info("开始查找元素[" + byElement + "]");
try {
(new WebDriverWait(driver, timeOut)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
WebElement element = driver.findElement(byElement);
return element.isDisplayed();
}
});
} catch (TimeoutException e) {
logger.error("超时!! " + timeOut + " 秒之后还没找到元素 [" + byElement + "]");
Assert.fail("超时!! " + timeOut + " 秒之后还没找到元素 [" + byElement + "]");
}
logger.info("找到了元素 [" + byElement + "]");
try {
clickTheClickable(byElement, System.currentTimeMillis(), 2500);
} catch (StaleElementReferenceException e) {
logger.error("The element you clicked:[" + byElement + "] is no longer exist!");
Assert.fail("The element you clicked:[" + byElement + "] is no longer exist!");
} catch (Exception e) {
logger.error("Failed to click element [" + byElement + "]");
Assert.fail("Failed to click element [" + byElement + "]",e);
}
logger.info("点击元素 [" + byElement + "]");
}
public boolean isEnabled(By by){
return driver.findElement(by).isEnabled();
}
/**提交*/
public void submit(WebElement w){
try{
w.submit();
}catch(Exception e){
logger.error("在元素:"+w+"做的提交操作失败");
Assert.fail("在元素:"+w+"做的提交操作失败");
}
logger.info("在元素:"+w+"做了提交操作");
}
/**
* 包装点击操作 -webelment
* */
public void click(WebElement element) {
try {
element.click();
} catch (StaleElementReferenceException e) {
logger.error("The element you clicked:[" + element.toString() + "] is no longer exist!");
Assert.fail("The element you clicked:[" + element.toString() + "] is no longer exist!");
} catch (Exception e) {
logger.error("Failed to click element [" + element.toString() + "]");
Assert.fail("Failed to click element [" + element.toString() + "]",e);
}
logger.info("点击元素 [" + element.toString() + "]");
}
/** 不能点击时候重试点击操作 */
public void clickTheClickable(By byElement, long startTime, int timeOut) throws Exception {
try {
findElementBy(byElement).click();
} catch (Exception e) {
if (System.currentTimeMillis() - startTime > timeOut) {
logger.warn(byElement+ " is unclickable");
throw new Exception(e);
} else {
Thread.sleep(500);
logger.warn(byElement + " is unclickable, try again");
clickTheClickable(byElement, startTime, timeOut);
}
}
}
/**
* 获得页面的标题
* */
public String getTitle() {
return driver.getTitle();
}
/**
* 获得元素的文本
* */
public String getText(By elementLocator) {
return driver.findElement(elementLocator).getText().trim();
}
/**
* 获得元素 属性的文本
* */
public String getAttributeText(By elementLocator, String attribute) {
return driver.findElement(elementLocator).getAttribute(attribute).trim();
}
/**
* 包装清除操作
* */
public void clear(By byElement) {
try {
findElementBy(byElement).clear();
} catch (Exception e) {
logger.error("清除�