2011-06-15 97 views
0

我有一個簡單的HTML頁面(ratings.html),我試圖使用HtmlUnit進行測試。當我在瀏覽器中加載它並手動完成時,我正在測試的操作就起作用了。但是,當我嘗試使用HtmlUnit進行測試時,似乎太多調用getElementById(或getInputByName)會導致頁面上的JavaScript重新初始化。HtmlUnit的HtmlPage.getElementById似乎在多次調用後重新初始化JavaScript

在AddRating.scala測試中,前兩次調用page.addRating工作,但第三次調用失敗,因爲它無法在頁面上找到'rating3'元素。經過大量調試後,我發現ratingCount由於某種原因重置爲0。

看到我下面的評論(// ******部分之間),看看問題所在的地方。

有沒有其他人遇到過這種行爲或對如何處理它有任何建議?謝謝!

ratings.html(HTML頁面中加入 「收視率」):

<html> 
    <head> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> 
     <script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script> 
    </head> 
    <body> 
    <form name="new-rating" method="post" action="/add-rating"> 
     <table> 
     <tr> 
      <td valign="top">Ratings:</td> 
      <td> 
      Should be ordered from best to worst.<br> 
      <a id="add-rating" href="#">add rating</a></td> 
     </tr> 
     <tr> 
      <td></td> 
      <td> 
      <button name="submit-button" type="submit">Add Rating</button> 
      </td> 
     </tr> 
     </table> 
    </form> 
    <h2>All Ratings</h2> 

    <ul id="ratings"> 
    </ul> 

    <script> 
     $(window).load(function(){ 
      // display ratings 
      $.getJSON("/ratings", function(data) 
      { 
       var items = $.map(data, function(el) { 
       var ratingsTable = ""; 
       if (el.ratings.length > 0) 
       { 
        $.tmpl("<li><table><tr><th>Rating</th></tr>{{each ratings }}<tr><td>${$value.label}</td></tr>{{/each}}</table></li>", el).appendTo("#ratings"); 
       } 
       }); 
      }); 

      // add rating action 
      // *********** 
      var ratingCount = 0; // THIS GETS RE-INITIALIZED TO 0 AFTER TOO MANY getElementById or getElementByName calls. 
      // *********** 
      $('#add-rating').click(function() 
      { 
       ratingCount += 1; 
       $('#remove-rating').show(); 
       $.tmpl("<span id='rating${ratingId}'><input name='rating${ratingId}'><br></span>", 
        {ratingId: ratingCount}).insertBefore('#add-rating'); 
       if(ratingCount >= 5) { $('#add-rating').hide(); } 
      }); 
     }); 
    </script> 
    </body> 
</html> 

RatingsPage.scala(斯卡拉接口HTML頁面):

package portal 

import com.gargoylesoftware.htmlunit.WebClient 
import com.gargoylesoftware.htmlunit.html._ 

import infrastructure.SuperHtmlUnitConversions._ 

import infrastructure.WaitFor 

class RatingsPage(page: HtmlPage) 
{ 
    val newRatingForm: HtmlForm = page.getFormByName("new-rating") 

    var ratingCount = 0 

    def submit(): RatingsPage = 
    { 
     val page = newRatingForm.getButtonByName("submit-button").click[HtmlPage]() 
     ratingCount = 0 
     new RatingsPage(page) 
    } 

    def addRating(rating: String) 
    { 
     page.getElementById("add-rating").click() 
     ratingCount += 1 
     newRatingForm.getInputByName("rating" + ratingCount).asInstanceOf[HtmlInput].setValueAttribute(rating) 
    } 

    def asText(): String = page.asText 
    def asXml(): String = page.asXml 
} 

AddRating.scala (斯卡拉HtmlUnit測試失敗):

package portal 

import java.util.Date 
import org.scalatest.FunSuite 
import org.scalatest.junit.JUnitRunner 
import org.junit.runner.RunWith 
import org.scalatest.matchers.ShouldMatchers 
import com.gargoylesoftware.htmlunit.WebClient 
import com.gargoylesoftware.htmlunit.html._ 
import infrastructure.WaitFor 

@RunWith(classOf[JUnitRunner]) 
class AddRating extends FunSuite with ShouldMatchers 
{ 
    test("add ratings") 
    { 
     val client = new WebClient() 
     val index = new PortalIndexPage(client) 
     var page = index.goToRatingsPage() 

     page.addRating("Buy") // WORKS 
     page.addRating("Sell") // WORKS 
     // ********* 
     page.addRating("Sell") // FAILS! Can't find element with "rating3" name! 
     // ********* 
     page = page.submit() 
     WaitFor("rating to show up",()=>page.asXml.contains("Sell")) 

     page.asText should include ("Buy") 

     client.closeAllWindows() 
    } 
} 

回答

1

將您的<a href...鏈接變爲按鈕,它應該工作。使用<button type="button",否則它將被視爲提交按鈕。

按鈕(提供它不提交)不會導致頁面重新加載。 <a href='#'導致重新加載HtmlUnit

+1

這會如何影響'ratingCount'變量的重新初始化? – 2011-06-16 17:41:26

+1

謝謝,我已經更新了我的答案並進一步完善了 – luntain 2011-06-20 15:49:56

2

我的猜測是,你需要讓你的錨點單擊處理程序「返回false」。

這應該防止瀏覽器重新加載頁面的行爲。