WebDriver中點擊按鈕、連接無效問題

WebDriver中點擊按鈕、連接無效問題

 

之前在寫一個測試用例的時候,就有發現點擊提交按鈕不起作用,今天又遇到點擊鏈接也不起作用的情況。經過一些嘗試,暫時已通過其他方式解決。

 

 

1.       軟件版本

1)      操作系統Win7 旗艦版(64)

2)      JDK1.7

3)      EclipseMars Release (4.5.0) Eclipse Java EE IDE for Web Developers

4)      Eclipse TestNG插件org.testng.eclipse_6.9.5.201506120235

5)      Webdriverselenium-java-2.46.0

6)      IEDriverServer.exe

 

 

2.       被測對象說明

爲了說明問題,我以一個簡單的登陸流程作爲測試對象:登陸頁面,如果用戶密碼正確則提交後跳轉到登陸成功頁面,否則跳轉到登陸失敗頁面,登陸失敗頁面有一個鏈接,點擊後跳轉到登陸界面。下面是各頁面的代碼。

 

2.1   登陸頁面

點擊(此處)摺疊或打開

  1. <%@ page language="java"%>
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"%>

  3. <% String path = request.getContextPath(); %>

  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>系統登錄</title>
  9. </head>
  10. <body>
  11.     <center>
  12.         <h1>系統登錄</h1>
  13.         <hr>
  14.         <form name="loginForm" action="<%=path%>/login.action" method="post">
  15.             用戶名:<input type="text" name="username" /><br>
  16.             密碼:<input type="password" name="password" /><br>
  17.             <input type="submit" value="登錄" id="btnLogin" name="btnLogin" /><br>
  18.         </form>
  19.     </center>
  20. </body>
  21. </html>


 

2.2   登陸成功頁面

點擊(此處)摺疊或打開

  1. <%@ page language="java"%>
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>登錄成功</title>
  8. </head>
  9. <body>
  10.     <center>
  11.         <h1>登錄成功</h1>
  12.         <hr>        
  13.     </center>
  14. </body>
  15. </html>
 


2.3   登陸失敗頁面

點擊(此處)摺疊或打開

  1. <%@ page language="java" import="java.util.*"%>
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"%>

  3. <% String path = request.getContextPath(); %>

  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>登錄失敗</title>
  9. </head>
  10. <body>
  11.     <center>
  12.         <h1>登錄失敗</h1>
  13.         <hr>    
  14.         <a class="return" href="<%=path%>/login.jsp">返回</a>    
  15.     </center>
  16. </body>
  17. </html>


 

3.       點擊提交按鈕不起作用

一開始我針對登陸成功的測試用例是這樣寫的:

點擊(此處)摺疊或打開

  1. @Test
  2. public void loginSuccessTest() {
  3.         System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\IEDriverServer.exe");
  4.         WebDriver driver = new InternetExplorerDriver();
  5.         driver.get("http://127.0.0.1:8080/ Login/");
  6.         driver.manage().window().maximize();
  7.         
  8.         CharSequence[] csUser = new CharSequence[1];
  9.         csUser[0] = "admin";
  10.         driver.findElement(By.name("username")).sendKeys(csUser);
  11.         
  12.         CharSequence[] csPW = new CharSequence[1];
  13.         csPW[0] = "123456";
  14.         driver.findElement(By.name("password")).sendKeys(csPW);
  15.         
  16.         driver.findElement(By.name("btnLogin ")).click();
  17.                 
  18.         String excepted = "登錄成功";
  19.         String actual = driver.getTitle();
  20.         assertEquals(actual, excepted);
  21.         
  22.         driver.close();
  23.         
  24.         try{
  25.             Runtime.getRuntime().exec("tskill IEDriverServer");
  26.         }catch(IOException ie){
  27.             System.out.println("failed to close IEDriverServer......");
  28.             ie.printStackTrace();
  29.         }
  30. }


但執行這個用例,並沒有點擊【登錄】按鈕,執行到填寫完兩個文本輸入框後就停止了,然後就關閉了,用例報錯,說獲取到的是【系統登錄】而非期望的【登陸成功】。但沒有報空指針異常,也就是說,submit按鈕是獲取到的,然後我又把click()改爲submit(),即:

點擊(此處)摺疊或打開

  1. driver.findElement(By.name("btnLogin ")).submit ();


執行後還是一樣一樣的結果。

 

後來查到一篇關於click()submit()區別的帖子[1],說click()只能用於submit按鈕,而submit()可以用於form中的所有element,包括form本身。於是修改代碼爲:

點擊(此處)摺疊或打開

  1. driver.findElement(By.name("loginForm ")).submit ();


執行成功。

 

 

4.       點擊鏈接不起作用

在測試失敗頁面點擊【返回】連接跳轉回登陸頁面的用例中,又遇到點擊後不起作用的問題。

點擊(此處)摺疊或打開

  1. @Test
  2. public void returnTest() {
  3.         System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\IEDriverServer.exe");
  4.         driver = new InternetExplorerDriver();
  5.         driver.get("http://127.0.0.1:8080/Login/login_failure.jsp");
  6.         driver.manage().window().maximize();
  7.         
  8.         driver.findElement(By.xpath("//a[@class='return']")).click();

  9.         String excepted = "系統登錄";
  10.         String actual = driver.getTitle();
  11.         assertEquals(actual, excepted);
  12.         
  13.         driver.close();
  14.         
  15.         try{
  16.             Runtime.getRuntime().exec("tskill IEDriverServer");
  17.         }catch(IOException ie){
  18.             System.out.println("failed to close IEDriverServer......");
  19.             ie.printStackTrace();
  20.         }
  21. }



這個就沒法用上面submit按鈕的方法了,於是我想,如果能將焦點設置到這個連接上,然後按【Enter】鍵,和點擊的效果是一樣的。於是我參考了資料[2],將代碼修改爲:

點擊(此處)摺疊或打開

  1. WebElement element = driver.findElement(By.xpath("//a[@class='return']"));
  2. Actions action = new Actions(driver);
  3. action.contextClick(element).perform();
  4. element.sendKeys(Keys.ESCAPE);
  5. element.sendKeys(Keys.ENTER);
測試通過!


 

這是用在連接上點擊右鍵,然後按【ESC】鍵來設置焦點的,沒有直接設置焦點的方法。有一點很奇怪,Actionsclick()doubleClick(),及單擊和雙擊都不起作用,只有右鍵contextClick()起作用。另:Actions類在org.openqa.selenium.interactions下面。

 

還有記得在斷言之前,間隔一兩秒,不然會沒操作完成,就去獲取title了,然後斷言失敗。

 

 

5.       用處理連接的方法處理提交按鈕

處理鏈接的方法,是否能用到submit按鈕上呢?試試就知道了

點擊(此處)摺疊或打開

  1. WebElement element = driver.findElement(By.name("btnLogin"));
  2. Actions action = new Actions(driver);
  3. action.contextClick(element).perform();
  4. element.sendKeys(Keys.ESCAPE);
  5. 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


轉載自http://blog.chinaunix.net/uid-21340438-id-5158425.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章