• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HtmlUnit test to see if an image actually exists?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All

I am using HtmlUnit to test a web application.

The website uses JFreeChart to create dynamic charts. However during a demo of our application none of the images could be viewed (due to a firewall issue).

So now I need write a test case to see if an image actually exists on the page.

Does anyone know how I can do this using HtmlUnit?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could probably verify the existence of the image on the page by writing a custom assertion method on top of the methods HtmlPage inherits from "com.gargoylesoftware.htmlunit.html.DomNode".
 
Andrew Och
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Currently I have implemented the following:

// Get the pie chart image
final HtmlImage pieChart = (HtmlImage) pieChartPage.getHtmlElementById(
"maincontent:myIMG" );

// Check that the image is present
assertNotNull( pieChart );

But I am not sure that this is actually testing whether an image is present since I guess that as long as the id of the image tag is present then a new HtmlImage object is created and therefore will never be null even if the actual image does not exist and only the alt tag is viewed.

I don't understand how DomNode would help me, but I will continue to rtfm ;-)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will probably need to get the URL for the image from

pieChart.getSrcAttribute();

and assert that a get request on that URL actually gives you an image.
 
Andrew Och
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help.

The following code tries to get the image as a file. If it exists then no exception is thrown, and if it does not then an exception is thrown and thus the assertion fails.

try {
// Create an absolute URL of the image
URL urlToImage = new URL( url + img.getSrcAttribute() );

try {
urlToImage.getContent();
} catch (IOException e) {
fail( "The File: " + urlToImage.getFile() + " does not exist" ); }
} catch ( MalformedURLException mue ) {
fail( "The URL: " + url + img.getSrcAttribute() + " is malformed" ); }

This still feels a little crude. When a human browses a web page they immediately can see if an image is present and correct or not, but for a test case this seems much more complex.

Anyway I have now got a test case that I does the job so once again thank you for all your help.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A JUnit test fails when it throws an Exception, so you could simplify your test to



If you want to test whether the URL points to a valid image, you can use the URL to load it as an ImageIcon.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic