HtmlUnit 的 HtmlPage.getElementById 似乎在多次调用后重新初始化 JavaScript

发布于 2024-11-15 18:37:36 字数 4639 浏览 3 评论 0 原文

我有一个简单的 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(HTML 页面的 Scala 接口):

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()
    }
}

I have a simple HTML page (ratings.html) that I'm trying to test using HtmlUnit. The action that I'm testing works when I load it up in a browser and do it by hand. However, when I try to test it with HtmlUnit, it seems like too many calls to getElementById (or getInputByName) cause the JavaScript on the page to be reinitialized.

In the AddRating.scala test, the first two calls to page.addRating work, but the third fails because it can't find the 'rating3' element on the page. After lots of debugging, I've discovered that the ratingCount gets reset back to 0 for some reason.

See my comments below (between the // ****** sections) to see where the problem areas are.

Has anyone else experience this behavior or have any advice on how to deal with it? Thanks!

ratings.html (HTML Page to add "ratings"):

<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 interface to HTML page):

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 test that fails):

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()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别靠近我心 2024-11-22 18:37:36

我的猜测是,您需要让锚点单击处理程序“返回 false”。

这应该可以防止浏览器重新加载页面的行为。

My guess is that you need to have your anchor click handler to "return false".

This should prevent the browser behavior of reloading the page.

清音悠歌 2024-11-22 18:37:36

Turn your <a href... link into button and it should work. Use <button type="button" or else it will be considered a submit button.

Button (provided it is not submit) will not cause the page to reload. <a href='#' causes reload in HtmlUnit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文