public static String getTime(){
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return "<b>"+format.format(date)+"</b>";
}
public static boolean waitPageRefresh(WebElement trigger) {
int refreshTime = 0;
boolean isRefresh = false;
try {
for (int i = 1; i < 10; i++) {
refreshTime = i;
trigger.getTagName();
Thread.sleep(1000);
}
} catch (StaleElementReferenceException e) {
isRefresh = true;
System.out.println("Page refresh time is:" + refreshTime + " seconds!");
Reporter.log("<pre><font color='#000000'>【系统日志】 Page refresh time is " + refreshTime + " seconds!</font></pre>");
return isRefresh;
} catch (WebDriverException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Page didnt refresh in 10 seconds!");
Reporter.log("<pre><font color='#000000'>【系统日志】 Page didnt refresh in 10 seconds!</font></pre>");
return isRefresh;
}
public static void pauseDriver(int time){
if(time<0 || time>10){
time = 10;
}
for (int i = 1; i < time; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static boolean isAlert(WebDriver driver){
try{
driver.switchTo().alert();
sysLog("当前页面出现弹窗");
System.out.println("有弹窗");
return true;
} catch(NoAlertPresentException e){
System.out.println("没有弹窗");
return false;
}
}
public static boolean isWebElementExist(WebDriver driver, By selector){
try {
driver.findElement(selector);
System.out.println("元素存在");
return true;
} catch (NoSuchElementException e) {
System.out.println("元素不存在");
return false;
}
}
private static void getPageEnd(WebDriver driver){
String js = "window.scrollTo(0,document.body.scrollHeight);";
JavascriptExecutor jsDriver = (JavascriptExecutor) driver;
jsDriver.executeScript(js);
System.out.println("执行JS语句,控制页面滚动条滚动到页面底端");
}
private static void moveToElement(WebDriver driver, By locator) {
Actions builder = new Actions(driver);
builder.moveToElement(driver.findElement(locator)).perform();
}
public static Boolean isSelectEmpty(WebDriver driver, String selectXpath){
Select select = new Select(driver.findElement(By.xpath(selectXpath)));
return select.getOptions().isEmpty();
}