一、等待處理
1.全局等待
/*全局設置,當元素識別不到的時候,可以接受的長等待時間。*/
driver.manage()timeouts().implicitlyWait(30, TimeUnit.SECONDS);
/*全局設置,頁面加載的長等待時間。*/
driver.manage()timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
/*全局設置,關于JavaScript代碼的異步處理的超時時間。AJAX請求。*/
driver.manage()timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
2.元素等待
public void waitForElement(By by) throws Exception{
for(int second = 1; second <= 30; second++){
try{
driver.findElement(by);
break;
}catch(Exception e){
System.out.println(e.getLocalizedMessage());
}
Thread.sleep(1000);
}
}
this.waitForElement(By.id("username"));//調用方法
系統(tǒng)自帶的方法:
WebElement btnLogin = new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(By.id("login")));
btnLogin.click();
二、斷言
1.判斷頁面元素是否存在
public boolean waitForElement(By by) throws Exception{
boolean isExsit = false;
for(int second = 1; second <= 30; second++){
try{
driver.findElement(by);
isExist = true;
break;
}catch(Exception e){
System.out.println(e.getLocalizedMessage());
}
Thread.sleep(1000);
}
return isExist;
}
if(this.waitForElement(By.id("username"))){
}else{
}
2.判斷頁面上的元素的值/內容
String result = driver.findElement(By.id("msg")).getText();
//對于內容的判斷:
//1.嚴格匹配:result.equals("")
//2.模糊匹配:result.startsWith(""),result.endsWith, result.contains
//3.正則表達式:result.matches("正則表達式 No=.*")
if(result.contains("aaa")){
}else{
}
3.直接讀取數據庫的內容
String sql = "SELECT ID FROM USERNAME ORDER BY ID "
int maxId = thisgetMaxId(sql);
int postEqual = result.indexOf("=");//取=號在字符串中的位置
String newId= result.subString(postEqual + 1 );//從=號開始截取到后,+1后移一位
if(maxId == Integer.parseInt(newId)){
}else{
}
三、新窗口處理
1.對話框確認框的操作
Alert alert = driver.switchTo().alert();
alert.accept(); //點擊確定
alert.dismiss(); //點擊取消
2.新窗口的操作
//windowID切換
String loginID = driver.getWindowHandle();
for(String windowID : driver.getWindowHandles()){
if (!windowID.equals(loginID))
driver.switchTo.().window(windowID);
}
//windowTitle切換
for(String windowID : driver.getWindowHandles()){
driver.switchTo.().window(windowID);
Sring windowTitle = driver.getTitle();
if(windowTitle.contains("部分標題")){
break;
}
}
3.彈出窗口和Iframe
driver.switchTo().frame("frameId");//切換到frame頁面
driver.switchTo().window("windowhandle");//切換回到主頁面
四、鼠標操作事件
private Actions action;
//單擊-click
public void click(){
action.moveToElement(drvier.findElement(By.linkText("login"))).perform();
action.click().perform(); //action的調用后面一定要加上perform,才能保證能真正的運行。
}
//雙擊-
public void doubleClick(){
action.doubleClick(drvier.findElement(By.linkText("login"))).perform();
}
//右鍵-
public void rightClick(){
action.contextClick(drvier.findElement(By.linkText("login"))).perform();
}
//懸停-
public void mouseHold(){
action.clickAndHold(drvier.findElement(By.linkText("login"))).perform();
}
//拖拽-
public void dragDrop(){
WebElement source = drvier.findElement(By.linkText("login"))
WebElement target = drvier.findElement(By.linkText("login"))
action.dragAndDrop(source, target);
action.dragAndDropBy(source, 200, 300);
}
五、鍵盤事件處理
1.webDriver鍵盤操作-Action
//webDriver鍵盤操作
action.sendKeys("A").perform(); //按下鍵盤A
action.sendKeys(Keys.Delete).perform();
2.Java鍵盤操作-Robot
//Java鍵盤操作
//Java內含robot操作對象。throws Exception拋出異常給調用者。try{}catch(Excetion e){}//Excetion所有異常的父類,捕捉所有異常。
public void robotUsage(){
Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_MASK); //鼠標左鍵點擊
robot.mouseRelease(InputEvent.BUTTON1_MASK); //鼠標左鍵釋放
robot.mousePress(keyEvent.VK_ENTER); //回車鍵
robot.mouseRelease(keyEvent.VK_ENTER);
}