我有一个简单的HTML页面(ratings.html),我正在尝试使用HtmlUnit进行测试。当我在浏览器中加载并手工完成它时,我正在测试的操作是有效的。但是,当我尝试用HtmlUnit测试它时,似乎太多对getElementById (或getInputByName)的调用会导致重新初始化页面上的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 (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 (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()
}
}发布于 2011-06-15 22:07:15
将您的<a href...链接转换为按钮,它应该可以工作。使用<button type="button",否则它将被视为提交按钮。
按钮(如果不是提交的话)将不会导致页面重新加载。<a href='#'导致在HtmlUnit中重新加载
发布于 2011-06-15 22:08:32
我的猜测是,您需要让您的锚单击处理程序“返回false”。
这将防止浏览器重新加载页面的行为。
https://stackoverflow.com/questions/6364675
复制相似问题