<?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; javascript</title>
	<atom:link href="http://grailsgeek.com/tag/javascript/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>Attaching file to file upload control with Selenium (via JavaScript)</title>
		<link>http://grailsgeek.com/2009/07/attaching-file-to-file-upload-control-with-selenium-via-javascript/</link>
		<comments>http://grailsgeek.com/2009/07/attaching-file-to-file-upload-control-with-selenium-via-javascript/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 16:25:34 +0000</pubDate>
		<dc:creator>Alexander Naumenko</dc:creator>
				<category><![CDATA[integration testing]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://grailsgeek.com/?p=48</guid>
		<description><![CDATA[I didn&#8217;t found useful way to set file inputs controls in our integration tests written with Selenium. I propose JavaScript way to execute with Selenium RC to achieve this. Finally this should be doable with following code in any Test Case:
setFile "fileInputId", getResourcePath("some photo.jpg")
getResourcePath() &#8211; returns path to resource (java/groovy resources meant). You could use [...]]]></description>
			<content:encoded><![CDATA[<p>I didn&#8217;t found useful way to set file inputs controls in our integration tests written with Selenium. I propose JavaScript way to execute with Selenium RC to achieve this. Finally this should be doable with following code in any Test Case:</p>
<pre class="brush:groovy">setFile "fileInputId", getResourcePath("some photo.jpg")</pre>
<p><span style="color: #333399;">getResourcePath()</span> &#8211; returns path to resource (java/groovy resources meant). You could use any path string instead, but inclusion of resources (test resources) is more convenient, because they become part of your test environment. If you have some problems with java/groovy resources, feel free to ask.</p>
<p>* We will extend our SeleniumTest (basic class for all integration tests  with this method:</p>
<pre class="brush:groovy">public abstract class SeleniumTest extends SeleneseTestCase {
    static String context = "http://localhost:8080/app/"

    public void setUp() {
        setUp context, "*firefox"
    }
    ...
    void setFile(String fileInputId, String fileName) {
        selenium.getEval(
            """var imageControl = window.document.getElementById('${fileInputId}');
               imageControl.value='$fileName';
               var event = window.document.createEvent('HTMLEvents');
               event.initEvent('change',true,true);
               imageControl.dispatchEvent(event);
            """)
    }

    String getResourcePath(String resourcePath) {
        getClass().getResource(resourcePath).getPath()
    }
    ...
}</pre>
<p>This method uses <span style="color: #333399;">document</span> model to inject file path into HTML <span style="color: #333399;">file input</span> control and also generates <span style="color: #333399;">onChange</span> event, which could be useful if onChange event handler defined for this component.</p>
]]></content:encoded>
			<wfw:commentRss>http://grailsgeek.com/2009/07/attaching-file-to-file-upload-control-with-selenium-via-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
