package com.dn.UI;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.io.Files;
public class KeyWordsOfWeb {
public WebDriver driver = null;
public void openBrowser(String driverpath) {
try {
switch (driverpath) {
case ("chrome"):
GoogleDriver google = new GoogleDriver("tools/chromedriver.exe");
driver = google.getdriver();
break;
case ("ff"):
FFDriver ff = new FFDriver("", "tools/geckodriver.exe");
driver = ff.getdriver();
break;
case ("ie"):
IEDriver ie = new IEDriver("tools/iedriver.exe");
driver = ie.getdriver();
break;
default:
GoogleDriver google1 = new GoogleDriver("tools/chromedriver.exe");
driver = google1.getdriver();
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("浏览器关键字出错");
}
}
public void getUrl(String url) {
try {
driver.get(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("url打开失败");
}
}
// 显示等待 ,较常用 1>自定义显示等待的条件,直到条件成立
public void explicityWait(String xpath) {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver d) {
return d.findElement(By.xpath(xpath));
}
});
// 显示等待2>显示等待,用selenium本事自带的功能
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath(xpath))));
}
public void explicityWaitOfTitle() {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("Cheese");
}
});
}
// 隐式等待
public void implicityWait() {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
// 强制等待 Thread.sleep(1000);
public void halt(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void inputByXpathAndSubmit(String xpath, String key) {
try {
driver.findElement(By.xpath(xpath)).sendKeys(key);
driver.findElement(By.xpath(xpath)).submit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("定位元素出错");
}
}
public void clickByXpath(String xpath) {
try {
driver.findElement(By.xpath(xpath)).click();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("点击元素失败");
}
}
public void setWindow() {
try {
driver.manage().window().maximize();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("maximize 出错。");
}
}
// 选择句柄
public void switchWindow(String targtext) {
String s = null;
Set<String> handls = driver.getWindowHandles();
for (String t : handls) {
if (driver.switchTo().window(t).getTitle().equals(targtext)) {
s = t;
}
}
try {
driver.switchTo().window(s);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("switchtWindow 出错。");
}
}
// 悬浮
public void mouseOn(String xpath) {
Actions action = new Actions(driver);
try {
action.moveToElement(driver.findElement(By.xpath(xpath))).perform();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("mouseOn 出错。");
}
}
// 切换iframe
public void selectIframe(String xpath) {
try {
driver.switchTo().frame(driver.findElement(By.xpath(xpath)));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("selectIframe 出错。");
}
}
public void outIframe() {
try {
driver.switchTo().defaultContent();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("outIframe 出错。");
}
}
// 文本校验断言
public void assertByText(String xpath, String judge) {
String result;
try {
explicityWait(xpath);
result = driver.findElement(By.xpath(xpath)).getText();
System.out.println(driver.findElement(By.xpath(xpath)));
System.out.println(result);
if (result.equals(judge))
System.out.println("pass");
else
System.out.println("fail");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// public void assertByText(String xpath , String text) {
// try {
// String s = driver.findElement(By.xpath(xpath)).getText();
// System.out.println(driver.findElement(By.xpath(xpath)));
// System.out.print(s);
// if(s.equals(text))
// System.out.print("pass");
// else
// System.out.print("fail");
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }srcshot/
// 截图做断言
public void screenShot(String srcshotname) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
String creatdate = sdf.format(date);
String imgname = "SCRshot/" + creatdate + srcshotname + ".png";
File img = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
try {
Files.copy(img, new File(imgname));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("screenShot 出错。");
}
}
public void saveScrShot() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
String creatdate = sdf.format(date);
String imgname = "SCRshot/" + creatdate + ".png";
File srcfile = new File(imgname);
File tmp = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(tmp, srcfile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("saveScrShot 出错。");
}
}
public void sendFfile(String xpath, String filepath) {
try {
driver.findElement(By.xpath(xpath)).sendKeys(filepath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("sendFfile 出错。");
}
}
public void selectByText(String xpath, String text) {
Select sele = new Select(driver.findElement(By.xpath(xpath)));
try {
sele.selectByVisibleText(text);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("selectByText 出错。");
}
}
//加turn 是有返回值的
public void getJS(String text) {
String t = "";
JavascriptExecutor js = (JavascriptExecutor)driver;
try {
t = js.executeScript(text).toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("getJS 出错。");
}
}
public void runJs(String text) {
JavascriptExecutor js = (JavascriptExecutor)driver;
try {
//executeScript 执行js
js.executeScript(text);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("runJs 出错。");
}
}
public void srcoll() {
JavascriptExecutor js = (JavascriptExecutor)driver;
try {
js.executeScript("window.scrollTo(100,document.body.scrollHeight)" );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getTitle() {
try {
System.out.print(driver.getTitle());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("gettitle 出错。");
}
}
public void closeBrowser() {
try {
driver.quit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("关闭浏览器出错");
}
}
}