Re: [JWebUnit-users] Accessing dynamic images created in Javascript
Brought to you by:
henryju
From: Jevon W. <je...@je...> - 2010-09-30 13:17:17
|
Hi Jeff, Firstly yes, JWebUnit/HtmlUnit handles most types of Javascript, so it should be fine as long as your Javascript is compliant. Yes, using an XPath approach sounds fine. Have you tried searching through all //img, and making sure your image is first present there? Perhaps the @src attribute has whitespace or the URI is automagically made absolute, try using contains() instead. You could also first assert that the H2 exists (through XPath or an element ID) and then assert that it contains an image, and then peeking at the source of the image that way. Hope this helps, Jevon On Thu, Sep 30, 2010 at 2:57 AM, Jeff Davies <jef...@ya...> wrote: > Hi all, > Another quick question. I have some javascript/JQuery that is creating > an img dynamically. I tried using the *assertElementPresentByXPath*() method > but that is not behaving as expected. For example, I have an Ajax call that > builds the img tag as follows: > > xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && > xmlhttp.status==200) { try { tableHTML = "<h2>Administrators <img > id=\"inviteAdmin\" title=\"Invite an administrator\" class=\"clickable\" > src=\"images/icon_envelope.png\" onclick=\"showInviteAdministrator();\" > width=\"48\" height=\"48\" alt=\"Invite administrator\"></img></h2><table>"; > result = JSON.parse(xmlhttp.responseText); > > My JWebUnit test file then tries the following assert: > > assertElementPresentByXPath("//img[@src='images/icon_envelope.png']"); > > Here is the resulting (truncated) trace when the assert fails: > junit.framework.AssertionFailedError: Unable to locate element with xpath > "//img[@src='images/icon_envelope.png']" at > junit.framework.Assert.fail(Assert.java:47) at > junit.framework.Assert.assertTrue(Assert.java:20) at > net.sourceforge.jwebunit.junit.WebTester.assertElementPresentByXPath(WebTester.java:2034) > at > net.sourceforge.jwebunit.junit.WebTestCase.assertElementPresentByXPath(WebTestCase.java:1619) > > Now I am reasonably good with XPath, but I also know how finicky it can be > and how easy it is to overlook something. Here are my questions: > > > - Am I even going about this the right way? > - Can JWebUnit handle dynamic elements created by javascript? > - Is this something HtmlUnit is better suited? > > > Thanks in advance for your help. > > - Jeff > > > ------------------------------------------------------------------------------ > Start uncovering the many advantages of virtual appliances > and start using them to simplify application deployment and > accelerate your shift to cloud computing. > http://p.sf.net/sfu/novell-sfdev2dev > _______________________________________________ > JWebUnit-users mailing list > JWe...@li... > https://lists.sourceforge.net/lists/listinfo/jwebunit-users > > |