Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6026059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:23:24+00:00 2026-05-23T04:23:24+00:00

I have a simple HTML page (ratings.html) that I’m trying to test using HtmlUnit.

  • 0

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()
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:23:24+00:00Added an answer on May 23, 2026 at 4:23 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple html page with a div. I am using jQuery to
I have a simple html block like: <span id=replies>8</span> Using jquery I'm trying to
I have a simple HTML page that looks like this: ...<div id="main"> <a href="#">Click
I have a simple html page that only uses PHP in two places <?php
When Connected: I have a simple HTML page with some image elements that reference
I have a simple HTML. I am using the JQuery for AJAX purpose. Now,
I have a simple HTML (as HTA) application that shows strange behavior on Windows
I have a site that has a simple HTML button in a form. All
I have a simple form with some plain html input like bellow using ASP.NET
There are some HTML based games (ie bootleggers.us) that have a simple login form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.