<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Grails Geek &#187; image presence</title>
	<atom:link href="http://grailsgeek.com/tag/image-presence/feed/" rel="self" type="application/rss+xml" />
	<link>http://grailsgeek.com</link>
	<description>Grails tips I wanna share with you ;)</description>
	<lastBuildDate>Sun, 01 Nov 2009 13:29:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Assert image presence on page with Selenium</title>
		<link>http://grailsgeek.com/2009/07/assert-image-presence-on-page-with-selenium/</link>
		<comments>http://grailsgeek.com/2009/07/assert-image-presence-on-page-with-selenium/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 17:03:31 +0000</pubDate>
		<dc:creator>Alexander Naumenko</dc:creator>
				<category><![CDATA[integration testing]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[image presence]]></category>
		<category><![CDATA[img]]></category>
		<category><![CDATA[integration-test]]></category>
		<category><![CDATA[selenium]]></category>

		<guid isPermaLink="false">http://grailsgeek.com/?p=61</guid>
		<description><![CDATA[Assume you have some images on your page under test, which has some images. You should be sure they are really displayed. Simple validation on image control presence doesn&#8217;t solves the problem, because &#60;img&#62; could have invalid source (src attribute). We will enhance our basic test with method which could be used like:
assertImagePresent("imgUnderTestId")
assertImagePresent() method could [...]]]></description>
			<content:encoded><![CDATA[<p>Assume you have some images on your page under test, which has some images. You should be sure they are really displayed. Simple validation on image control presence doesn&#8217;t solves the problem, because <span style="color: #333399;">&lt;img&gt;</span> could have invalid source (<span style="color: #333399;">src</span> attribute). We will enhance our basic test with method which could be used like:</p>
<pre class="brush:groovy">assertImagePresent("imgUnderTestId")</pre>
<p><span style="color: #333399;">assertImagePresent()</span> method could be used with <span style="color: #333399;">img</span> component <span style="color: #333399;">id</span> or with <span style="color: #333399;">XPath</span> locator to assert that image has nonzero width and height (such technique used because width and height under assertion are <em>real</em> dimensions of target image in already rendered page).</p>
<p>We will enhance our basic SeleniumTest (used as superclass for all integration test cases) again:</p>
<pre class="brush:groovy">public abstract class SeleniumTest extends SeleneseTestCase {
    static String context = "http://localhost:8080/app/"

    public void setUp() {
        setUp context, "*firefox"
    }
    ...
    def assertImagePresent(String imageIdOrXPathLocator) {
        assertTrue Integer.valueOf(getDomAttribute(imageIdOrXPathLocator, "naturalWidth")) &gt; 0
        assertTrue Integer.valueOf(getDomAttribute(imageIdOrXPathLocator, "naturalHeight")) &gt; 0
    }

    def getDomAttribute(String elementIdOrXPathLocator, String attribute) {
        if (elementIdOrXPathLocator.startsWith("//")) {
            def expression = "window.document.evaluate('${StringEscapeUtils.escapeJavaScript(elementIdOrXPathLocator)}', window.document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue['$attribute'];"
            return selenium.getEval(expression)
        } else {
            return selenium.getEval("window.document.getElementById('$elementIdOrXPathLocator')['$attribute'];")
        }
    }
    ...
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://grailsgeek.com/2009/07/assert-image-presence-on-page-with-selenium/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
