Note that there are some explanatory texts on larger screens.

plurals
  1. POHtmlUnit's HtmlPage.getElementById seems to reinitialize JavaScript after many calls
    primarykey
    data
    text
    <p>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.</p> <p>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 <code>ratingCount</code> gets reset back to 0 for some reason.</p> <p>See my comments below (between the <code>// ******</code> sections) to see where the problem areas are.</p> <p>Has anyone else experience this behavior or have any advice on how to deal with it? Thanks!</p> <p><strong>ratings.html (HTML Page to add "ratings"):</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form name="new-rating" method="post" action="/add-rating"&gt; &lt;table&gt; &lt;tr&gt; &lt;td valign="top"&gt;Ratings:&lt;/td&gt; &lt;td&gt; Should be ordered from best to worst.&lt;br&gt; &lt;a id="add-rating" href="#"&gt;add rating&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt; &lt;button name="submit-button" type="submit"&gt;Add Rating&lt;/button&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;h2&gt;All Ratings&lt;/h2&gt; &lt;ul id="ratings"&gt; &lt;/ul&gt; &lt;script&gt; $(window).load(function(){ // display ratings $.getJSON("/ratings", function(data) { var items = $.map(data, function(el) { var ratingsTable = ""; if (el.ratings.length &gt; 0) { $.tmpl("&lt;li&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Rating&lt;/th&gt;&lt;/tr&gt;{{each ratings }}&lt;tr&gt;&lt;td&gt;${$value.label}&lt;/td&gt;&lt;/tr&gt;{{/each}}&lt;/table&gt;&lt;/li&gt;", 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("&lt;span id='rating${ratingId}'&gt;&lt;input name='rating${ratingId}'&gt;&lt;br&gt;&lt;/span&gt;", {ratingId: ratingCount}).insertBefore('#add-rating'); if(ratingCount &gt;= 5) { $('#add-rating').hide(); } }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>RatingsPage.scala (Scala interface to HTML page):</strong></p> <pre><code>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 } </code></pre> <p><strong>AddRating.scala (Scala HtmlUnit test that fails):</strong></p> <pre><code>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", ()=&gt;page.asXml.contains("Sell")) page.asText should include ("Buy") client.closeAllWindows() } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload