一、等待處理
1.全局等待
/*全局設(shè)置,當(dāng)元素識(shí)別不到的時(shí)候,可以接受的長(zhǎng)等待時(shí)間。*/
driver.manage()timeouts().implicitlyWait(30, TimeUnit.SECONDS);
/*全局設(shè)置,頁(yè)面加載的長(zhǎng)等待時(shí)間。*/
driver.manage()timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
/*全局設(shè)置,關(guān)于JavaScript代碼的異步處理的超時(shí)時(shí)間。AJAX請(qǐng)求。*/
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"));//調(diào)用方法
系統(tǒng)自帶的方法:
WebElement btnLogin = new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(By.id("login")));
btnLogin.click();
二、斷言
1.判斷頁(yè)面元素是否存在
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.判斷頁(yè)面上的元素的值/內(nèi)容
String result = driver.findElement(By.id("msg")).getText();
//對(duì)于內(nèi)容的判斷:
//1.嚴(yán)格匹配:result.equals("")
//2.模糊匹配:result.startsWith(""),result.endsWith, result.contains
//3.正則表達(dá)式:result.matches("正則表達(dá)式 No=.*")
if(result.contains("aaa")){
}else{
}
3.直接讀取數(shù)據(jù)庫(kù)的內(nèi)容
String sql = "SELECT ID FROM USERNAME ORDER BY ID "
int maxId = thisgetMaxId(sql);
int postEqual = result.indexOf("=");//取=號(hào)在字符串中的位置
String newId= result.subString(postEqual + 1 );//從=號(hào)開(kāi)始截取到后,+1后移一位
if(maxId == Integer.parseInt(newId)){
}else{
}
三、新窗口處理
1.對(duì)話(huà)框確認(rèn)框的操作
Alert alert = driver.switchTo().alert();
alert.accept(); //點(diǎn)擊確定
alert.dismiss(); //點(diǎn)擊取消
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("部分標(biāo)題")){
break;
}
}
3.彈出窗口和Iframe
driver.switchTo().frame("frameId");//切換到frame頁(yè)面
driver.switchTo().window("windowhandle");//切換回到主頁(yè)面
四、鼠標(biāo)操作事件
private Actions action;
//單擊-click
public void click(){
action.moveToElement(drvier.findElement(By.linkText("login"))).perform();
action.click().perform(); //action的調(diào)用后面一定要加上perform,才能保證能真正的運(yùn)行。
}
//雙擊-
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);
}
五、鍵盤(pán)事件處理
1.webDriver鍵盤(pán)操作-Action
//webDriver鍵盤(pán)操作
action.sendKeys("A").perform(); //按下鍵盤(pán)A
action.sendKeys(Keys.Delete).perform();
2.Java鍵盤(pán)操作-Robot
//Java鍵盤(pán)操作
//Java內(nèi)含robot操作對(duì)象。throws Exception拋出異常給調(diào)用者。try{}catch(Excetion e){}//Excetion所有異常的父類(lèi),捕捉所有異常。
public void robotUsage(){
Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_MASK); //鼠標(biāo)左鍵點(diǎn)擊
robot.mouseRelease(InputEvent.BUTTON1_MASK); //鼠標(biāo)左鍵釋放
robot.mousePress(keyEvent.VK_ENTER); //回車(chē)鍵
robot.mouseRelease(keyEvent.VK_ENTER);
}