錯誤描述:
java.lang.Exception: Unexpected exception, expected<java.util.NoSuchElementException> but was<org.openqa.selenium.NoSuchElementException>
源碼如下:
package info.itest.www;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class DeletePost {
WebDriver dr;
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\yule\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
this.dr=new ChromeDriver();
}
@After
public void tearDown() throws Exception {
this.dr.quit();
}
@Test(expected=NoSuchElementException.class) // 方法一:這裏是junit4框架提供的去除異常並通過的書寫方式
public void testDeletePost() {
String userName="admin";
String passWord="1";
this.login(userName,passWord);
String postId=this.createPost();
this.dr.get("http://localhost/wordpress/wp-admin/edit.php");
String rowId="post-"+postId;
System.out.println(rowId);
WebElement row=this.dr.findElement(By.id(rowId));
System.out.println(row);
Actions action=new Actions(this.dr);
action.moveToElement(row).perform();
row.findElement(By.cssSelector(".trash a")).click();
this.dr.findElement(By.id(rowId));
// assertFalse(this.isPostRowExists(rowId)); 方法二: 如果不按方法一,可以去掉方法一那行代碼,將//打開
}
// private boolean isPostRowExists(String rowId) {
// try{
// this.dr.findElement(By.id(rowId));
// return true;
// }catch(NoSuchElementException e){
// e.printStackTrace();
// return false;
// }
//
// }
public void login(String userName,String passWord){
dr.get("http://localhost/wordpress/wp-login.php");
dr.findElement(By.id("user_login")).sendKeys(userName);
dr.findElement(By.id("user_pass")).sendKeys(passWord);
dr.findElement(By.id("wp-submit")).click();
}
public String createPost(String title,String content){
this.dr.get("http://localhost/wordpress/wp-admin/post-new.php");
dr.findElement(By.name("post_title")).sendKeys(title);
this.sendContent(content);
dr.findElement(By.id("publish")).click();
return this.fetch_post_Id();
}
public String fetch_post_Id(){
String postUrl=this.dr.findElement(By.id("sample-permalink")).getText();
String[] arr=postUrl.split("=");
System.out.println("我獲得的PostId是"+arr[1]);
return arr[1];
}
public String createPost(){
String title ="Test title"+System.currentTimeMillis();
String content="Test content"+System.currentTimeMillis();
return this.createPost(title, content);
}
public void sendContent(String content){
String js="document.getElementById('content_ifr').contentWindow.document.body.innerHTML='" + content + "'";
((JavascriptExecutor)dr).executeScript(js);
}
}
現象描述:
當NoSuchElementException引用的包爲import java.util.NoSuchElementException; 則無法捕獲異常,並通過。
所以要修改引用包 import org.openqa.selenium.NoSuchElementException; 修改後就可以正常通過了。