之前在寫一個測試用例的時候,就有發現點擊提交按鈕不起作用,今天又遇到點擊鏈接也不起作用的情況。經過一些嘗試,暫時已通過其他方式解決。
1. 軟件版本
1) 操作系統:Win7 旗艦版(64位)
2) JDK:1.7
3) Eclipse:Mars Release (4.5.0) Eclipse Java EE IDE for Web Developers
4) Eclipse TestNG插件:org.testng.eclipse_6.9.5.201506120235
5) Webdriver:selenium-java-2.46.0
6) IEDriverServer.exe
2. 被測對象說明
爲了說明問題,我以一個簡單的登陸流程作爲測試對象:登陸頁面,如果用戶密碼正確則提交後跳轉到登陸成功頁面,否則跳轉到登陸失敗頁面,登陸失敗頁面有一個鏈接,點擊後跳轉到登陸界面。下面是各頁面的代碼。
2.1 登陸頁面
點擊(此處)摺疊或打開
- <%@ page language="java"%>
- <%@ page language="java" contentType="text/html; charset=UTF-8"%>
- <% String path = request.getContextPath(); %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>系統登錄</title>
- </head>
- <body>
- <center>
- <h1>系統登錄</h1>
- <hr>
- <form name="loginForm" action="<%=path%>/login.action" method="post">
- 用戶名:<input type="text" name="username" /><br>
- 密碼:<input type="password" name="password" /><br>
- <input type="submit" value="登錄" id="btnLogin" name="btnLogin" /><br>
- </form>
- </center>
- </body>
- </html>
2.2 登陸成功頁面
點擊(此處)摺疊或打開
- <%@ page language="java"%>
- <%@ page language="java" contentType="text/html; charset=UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>登錄成功</title>
- </head>
- <body>
- <center>
- <h1>登錄成功</h1>
- <hr>
- </center>
- </body>
- </html>
2.3 登陸失敗頁面
點擊(此處)摺疊或打開
- <%@ page language="java" import="java.util.*"%>
- <%@ page language="java" contentType="text/html; charset=UTF-8"%>
- <% String path = request.getContextPath(); %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>登錄失敗</title>
- </head>
- <body>
- <center>
- <h1>登錄失敗</h1>
- <hr>
- <a class="return" href="<%=path%>/login.jsp">返回</a>
- </center>
- </body>
- </html>
3. 點擊提交按鈕不起作用
一開始我針對登陸成功的測試用例是這樣寫的:
點擊(此處)摺疊或打開
- @Test
- public void loginSuccessTest() {
- System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\IEDriverServer.exe");
- WebDriver driver = new InternetExplorerDriver();
- driver.get("http://127.0.0.1:8080/ Login/");
- driver.manage().window().maximize();
-
- CharSequence[] csUser = new CharSequence[1];
- csUser[0] = "admin";
- driver.findElement(By.name("username")).sendKeys(csUser);
-
- CharSequence[] csPW = new CharSequence[1];
- csPW[0] = "123456";
- driver.findElement(By.name("password")).sendKeys(csPW);
-
- driver.findElement(By.name("btnLogin ")).click();
-
- String excepted = "登錄成功";
- String actual = driver.getTitle();
- assertEquals(actual, excepted);
-
- driver.close();
-
- try{
- Runtime.getRuntime().exec("tskill IEDriverServer");
- }catch(IOException ie){
- System.out.println("failed to close IEDriverServer......");
- ie.printStackTrace();
- }
- }
但執行這個用例,並沒有點擊【登錄】按鈕,執行到填寫完兩個文本輸入框後就停止了,然後就關閉了,用例報錯,說獲取到的是【系統登錄】而非期望的【登陸成功】。但沒有報空指針異常,也就是說,submit按鈕是獲取到的,然後我又把click()改爲submit(),即:
點擊(此處)摺疊或打開
- driver.findElement(By.name("btnLogin ")).submit ();
執行後還是一樣一樣的結果。
後來查到一篇關於click()和submit()區別的帖子[1],說click()只能用於submit按鈕,而submit()可以用於form中的所有element,包括form本身。於是修改代碼爲:
點擊(此處)摺疊或打開
- driver.findElement(By.name("loginForm ")).submit ();
執行成功。
4. 點擊鏈接不起作用
在測試失敗頁面點擊【返回】連接跳轉回登陸頁面的用例中,又遇到點擊後不起作用的問題。
點擊(此處)摺疊或打開
- @Test
- public void returnTest() {
- System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\IEDriverServer.exe");
- driver = new InternetExplorerDriver();
- driver.get("http://127.0.0.1:8080/Login/login_failure.jsp");
- driver.manage().window().maximize();
-
- driver.findElement(By.xpath("//a[@class='return']")).click();
- String excepted = "系統登錄";
- String actual = driver.getTitle();
- assertEquals(actual, excepted);
-
- driver.close();
-
- try{
- Runtime.getRuntime().exec("tskill IEDriverServer");
- }catch(IOException ie){
- System.out.println("failed to close IEDriverServer......");
- ie.printStackTrace();
- }
- }
這個就沒法用上面submit按鈕的方法了,於是我想,如果能將焦點設置到這個連接上,然後按【Enter】鍵,和點擊的效果是一樣的。於是我參考了資料[2],將代碼修改爲:
點擊(此處)摺疊或打開
- WebElement element = driver.findElement(By.xpath("//a[@class='return']"));
- Actions action = new Actions(driver);
- action.contextClick(element).perform();
- element.sendKeys(Keys.ESCAPE);
- element.sendKeys(Keys.ENTER);
這是用在連接上點擊右鍵,然後按【ESC】鍵來設置焦點的,沒有直接設置焦點的方法。有一點很奇怪,Actions的click()和doubleClick(),及單擊和雙擊都不起作用,只有右鍵contextClick()起作用。另:Actions類在org.openqa.selenium.interactions下面。
還有記得在斷言之前,間隔一兩秒,不然會沒操作完成,就去獲取title了,然後斷言失敗。
5. 用處理連接的方法處理提交按鈕
處理鏈接的方法,是否能用到submit按鈕上呢?試試就知道了
點擊(此處)摺疊或打開
- WebElement element = driver.findElement(By.name("btnLogin"));
- Actions action = new Actions(driver);
- action.contextClick(element).perform();
- element.sendKeys(Keys.ESCAPE);
- element.sendKeys(Keys.ENTER);
參考資料
[1] Selenium Webdriver submit() vs click() http://stackoverflow.com/questions/17530104/selenium-webdriver-submit-vs-click
[2] webDriver中如何給元素設置焦點 http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2013/0115/205906.html