标签:manager bdr ext open 设定 put comm ack fileutil
package util;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
/*提供页面各种操作的方法,包括判断元素是否存在,页面等待,截图以及窗口转换等*/
public class PageOperation {
String currentHandle;
//通过id查看指定元素是否存在
public boolean isElementExistById(String s_id){
try{
DriverManager.getDriver().findElement(By.id(s_id));
return true;
}catch(Exception e){
return false;
}
}
//通过xpath查看指定元素是否存在
public boolean isElementExistByXpath(String s_xpath){
try{
DriverManager.getDriver().findElement(By.xpath(s_xpath));
return true;
}catch(Exception e){
return false;
}
}
/*设定查找指定元素前等待指定时间,注意该设置在整个webdriver生命周期都存在*/
public void setElementWaitSecond(int i_time){
DriverManager.getDriver().manage().timeouts().implicitlyWait(i_time, TimeUnit.SECONDS);
}
//设置页面加载时间,注意该设置在整个webdriver生命周期都存在
public void setPageLoadTime(int i_time){
DriverManager.getDriver().manage().timeouts().pageLoadTimeout(i_time, TimeUnit.SECONDS);
}
//截屏
public void screenshot(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");//设置日期格式
File screenShotFile = ((TakesScreenshot)DriverManager.getDriver()).getScreenshotAs(OutputType.FILE);//截屏代码
//将截图剪切到指定路径
try{
FileUtils.moveFile(screenShotFile, new File("test-output/snapshot/" + df.format(new Date())+ ".jpg"));
}catch(IOException e){
System.out.println("can‘t save screenshot");
}
}
//窗口切换,并返回切换前的窗口句柄
public String switchWindow(){
currentHandle=DriverManager.getDriver().getWindowHandle();//得到当前窗口句柄
for(String handles:DriverManager.getDriver().getWindowHandles()){
if(handles.equals(currentHandle)){
continue;
}
DriverManager.getDriver().switchTo().window(handles);
}
return currentHandle;
}
}
标签:manager bdr ext open 设定 put comm ack fileutil
原文地址:http://www.cnblogs.com/qianjin100/p/7684369.html