常见封装的方法:输入、点击、判断元素是否存在、根据句柄切换窗口、根据title切换窗口、滚动窗口、截图
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.util.thread.Timeout;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import Study.Day02.TestTabnumDetail;
public class CommonClass {
//封装元素输入的方法
public static int t = 1;
static String dir="F:\\Users\\Wangtest\\screenshots\\dianzhang";
public static void sendKeys(WebDriver driver,By by, String value){
driver.findElement(by).sendKeys(value);
}
//封装元素点击的方法
public static void click(WebDriver driver,By by){
driver.findElement(by).click();
}
//封装判断元素是否存在的方法
public boolean doesWebElementExist(WebDriver driver,By by){
try {
driver.findElement(by);
return false;
} catch (Exception e) {
// TODO: handle exception
return true;
}
}
//封装根据句柄切换窗口的方法
public String getWindowHandle(WebDriver driver){
String currentWindow = driver.getWindowHandle();
return currentWindow;
}
public static void SwitchNewwindow(WebDriver driver){
//得到当前句柄
String currentWindow = driver.getWindowHandle();
//得到所有窗口的句柄
Set<String> handles = driver.getWindowHandles();
//排除当前窗口的句柄,则剩下是新窗口
Iterator<String> it = handles.iterator();
while(it.hasNext()){
if(currentWindow == it.next()) continue;
driver.switchTo().window(it.next());
}
}
//封装通过title切换窗口
public boolean switchToWindow(WebDriver driver,String windowTitle){
boolean flag = false;
try {
String currentHandle = driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles();
for (String s : handles) {
if (s.equals(currentHandle))
continue;
else {
driver.switchTo().window(s);
if (driver.getTitle().contains(windowTitle)) {
flag = true;
System.out.println("Switch to window: " + windowTitle + " successfully!");
break; }
else
continue;
}
}
} catch (NoSuchWindowException e) {
System.out.printf("Window:" + windowTitle+ " cound not found!", e.fillInStackTrace());
flag = false;
}
return flag;
}
//将滚动条滚到适合的位置
public static void setScroll(WebDriver driver,int height){
try {
// String setscroll = "document.documentElement.scrollTop=" + height;
String setscroll = "document.body.scrollTop=" + height;
JavascriptExecutor jse=(JavascriptExecutor) driver;
jse.executeScript(setscroll);
} catch (Exception e) {
System.out.println("Fail to set the scroll.");
}
}
// public void TakeScreenShot(WebDriver driver,WebElement element) throws IOException {
// File screen = ((TakesScreenshot) ]this.driver).getScreenshotAs(OutputType.FILE);
//
// Point p = element.getLocation();
//
// int width = element.getSize().getWidth();
// int height = element.getSize().getHeight();
//
// Rectangle rect = new Rectangle(width, height);
//
// BufferedImage img = null;
//
// img = ImageIO.read(screen);
//
// BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width,
// rect.height);
//
// ImageIO.write(dest, "png", screen);
//
// File f = null;
//
// f = new File(dir+getDateTime()+"_"+t+".jpg");
//
// FileUtils.copyFile(screen, f);
//
// }
//获取当前时间
public static String getDateTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
return df.format(new Date());
}
//封装截图的方法
public static void ScreenShot(WebDriver driver){
File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShot, new File(dir+getDateTime()+"_"+t+".jpg"));
++t;
} catch (IOException e) {
e.printStackTrace();
}
}
}