Web自动化常用方法封装(不定时更新)

 
   
/**
* JScript实现鼠标悬停
*/
public void mouseHoverJScript(By by,WebElement HoverElement) {
// TODO Auto-generated method stub
try {
if (isElementPresent(by)) { //isElementPresent() :自己封装的判断元素是否存在的方法
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript, HoverElement);
} else {
System.out.println("Element was not visible to hover " + "\n");
}
} catch (StaleElementReferenceException e) {
// TODO: handle exception
System.out.println("Element with " + HoverElement + "元素未附加到页面文档" + Arrays.toString(e.getStackTrace()));
} catch (NoSuchElementException e) {
// TODO: handle exception
System.out.println("Element " + HoverElement + " 元素未在DOM中没有找到" + Arrays.toString(e.getStackTrace()));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("悬停时发生错误" + Arrays.toString(e.getStackTrace()));
}
}

/**
* 等待弹窗出现并点掉
*/
public void waitAlertClick(){
WebDriverWait wait = new WebDriverWait(driver, 15);
try {
Alert alert = wait.until(new ExpectedCondition() {
@Override
public Alert apply(WebDriver driver1) {
try {
return driver.switchTo().alert();
} catch (NoAlertPresentException | UnhandledAlertException ignored) {
return null;
}
}
});
alert.accept();
} catch (NullPointerException e) {
System.out.println("-----------------------No Alert-----------------------");
}
}

/**
*
*获取弹窗,便于进行对其进行操作
* @return
*/
public Alert getAlert(){
WebDriverWait wait = new WebDriverWait(driver, 15);
Alert alert;
try{
alert= wait.until(new ExpectedCondition() {
@Override
public Alert apply(WebDriver driver1) {
try {
return driver.switchTo().alert();
} catch (NoAlertPresentException | UnhandledAlertException ignored) {
return null;
}
}
});
}catch (TimeoutException ignored){
System.out.println("-----------------------Time Out:No Alert-----------------------");
return null;
}
return alert;
}

/**
* 等待元素存在并可见
* @param by
* isEnable用于存储input、select等元素的可编辑状态,可以编辑返回true,否则返回false
*/
public void waitEleIsDisplay(By by){
WebDriverWait wait = new WebDriverWait(driver, 20);
try {
wait.until(new ExpectedCondition() {
@Override
public Boolean apply(WebDriver driver1) {
try {
WebElement ele=driver.findElement(by);
return ele.isDisplayed()&&ele.isEnabled();
}catch (NoSuchElementException e){
return null;
}
}
});
}catch (TimeoutException e ){
System.out.println("------------Timed out after 20 seconds waiting for:"+by+"------------");
}

}


/**
* 发送字符串到文本框或者其他

* 如果普通的element.sendKeys无法使用,请尝试使用此方法
* @param element
* @param keys
*/
public void mouseSendKeys(WebElement element, String keys) {
Actions action = new Actions(driver);
action.moveToElement(element);
action.click();
action.sendKeys(keys);
action.perform();
}
 

/**
* 截图-当前页面
* @param driver
* @return booelan 截图是否成功
*/
public boolean screenshot() {
String pngName = String.valueOf(System.currentTimeMillis())+".png";
String capturePath = DcitsUtil.SCREENSHOT_PATH + "/" + pngName; //DcitsUtil.SCREENSHOT_PATH 为封装的路径
File screenShotFile = ((TakesScreenshot)this.driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShotFile, new File(capturePath));
} catch (Exception e) {
// TODO: handle exception
System.out.println(""截图出错+e.getMessage())
return false;
}
return true;
}
 

转载于:https://www.cnblogs.com/HJkoma/p/9240058.html

你可能感兴趣的:(Web自动化常用方法封装(不定时更新))