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 voidgetUrl (String URL) { the try{ Driver.get (URL); } the catch (Exception E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); of System.out.print ( "URL open failure" ); } } // display wait, more common 1> customize the display condition to wait until the condition is satisfied public void explicityWait (String XPath) { WebDriverWait the wait = new new WebDriverWait (Driver, 10 ); wait.until ( new new ExpectedCondition <WebElement> () { public WebElement Apply (the 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(); Of System.out.print ( "clickable elements failed" ); } } public void setWindow () { the try { driver.manage () window () Maximize ();.. } The catch (Exception E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); of System.out.print ( "Maximize error." ); } } // select handle public void switchWindow (String targtext) { String S = null ; the 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() { the try { driver.quit (); } the catch (Exception E) { // TODO Auto-Generated the catch Block e.printStackTrace (); System.out.print ( "close the browser error" ); } } }