public
Last active

Selenium test for GWT FileUpload, e.g., how to specify a file path using Selenium for <input type="file"...>

  • Download Gist
App.gwt.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.0//EN"
"http://google-web-toolkit.googlecode.com/svn/releases/2.0/distro-source/core/src/gwt-module.dtd">
<module rename-to="App">
 
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<inherits name="com.google.gwt.place.Place"/>
<inherits name="com.google.gwt.activity.Activity"/>
<inherits name="com.google.gwt.logging.Logging"/>
<inherits name="com.google.gwt.http.HTTP" />
<inherits name="com.google.gwt.user.theme.chrome.Chrome"/>
 
<set-property name="gwt.logging.enabled" value="TRUE"/>
<set-property name="gwt.logging.logLevel" value="FINE"/>
<set-property name="gwt.logging.consoleHandler" value="DISABLED"/>
<set-property name="gwt.logging.developmentModeHandler" value="ENABLED"/>
<set-property name="gwt.logging.firebugHandler" value="ENABLED"/>
<set-property name="gwt.logging.hasWidgetsHandler" value="DISABLED"/>
<set-property name="gwt.logging.popupHandler" value="DISABLED"/>
<set-property name="gwt.logging.systemHandler" value="ENABLED"/>
<set-property name="gwt.logging.simpleRemoteHandler" value="DISABLED"/>
 
<!-- Specify the app entry point class. -->
<entry-point
class='com.moesol.examples.selenium.gwt.client.App'/>
 
<source path="client"/>
<source path="shared"/>
</module>
App.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package com.moesol.examples.selenium.gwt.client;
 
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.RootPanel;
 
public class App
implements EntryPoint
{
@Override
public void onModuleLoad()
{
FileUpload uploader = new FileUpload();
uploader.getElement().setId("FOO");
RootPanel.get().add(uploader);
}
}
AppTest1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
package com.moesol.examples.selenium.gwt.client;
 
import com.thoughtworks.selenium.SeleneseTestBase;
import org.junit.Before;
import org.junit.Test;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
 
/**
* Selenium 1 RC Implementation
*/
public class AppTest1
extends SeleneseTestBase
{
@Before
public void setUp() throws Exception
{
setUp("http://localhost:8080", "*firefox");
}
 
@Test
public void testSetFileInputField()
throws InterruptedException
{
selenium.open("/index.html");
Thread.sleep(TimeUnit.SECONDS.toMillis(2));
final String locator = "id=FOO";
openFileSelector(locator);
selenium.keyDownNative(String.valueOf(KeyEvent.VK_SHIFT));
selenium.keyPressNative(String.valueOf(KeyEvent.VK_C));
selenium.keyPressNative(String.valueOf(KeyEvent.VK_SEMICOLON));
selenium.keyUpNative(String.valueOf(KeyEvent.VK_SHIFT));
selenium.keyPressNative(String.valueOf(KeyEvent.VK_SLASH));
selenium.keyPressNative(String.valueOf(KeyEvent.VK_T));
selenium.keyPressNative(String.valueOf(KeyEvent.VK_M));
selenium.keyPressNative(String.valueOf(KeyEvent.VK_P));
closeFileSelector();
}
 
private void closeFileSelector()
{
selenium.keyPressNative(String.valueOf(KeyEvent.VK_ENTER));
}
 
private void openFileSelector(final String locator)
throws InterruptedException
{
selenium.focus(locator);
Thread.sleep(TimeUnit.SECONDS.toMillis(2));
selenium.keyPressNative(String.valueOf(KeyEvent.VK_SPACE));
Thread.sleep(TimeUnit.SECONDS.toMillis(2));
selenium.selectPopUp("null");
}
}
AppTest2.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
package com.moesol.examples.selenium.gwt.client;
 
import junit.framework.TestCase;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
/**
* Selenium 2 WebDriver Implementation
* Note: For some reason this doesn't work with Firefox 7.0.1
*/
public class AppTest2
extends TestCase
{
@Test
public void testSetFileInputField()
throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.get("http://localhost:8080/index.html");
WebElement inputField = driver.findElement(By.id("FOO"));
inputField.sendKeys("C:/tmp");
}
}
index.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<html>
<head>
<title>App</title>
<script type="text/javascript"
language="javascript"
src="App/App.nocache.js"></script>
</head>
<body>
<div id="hook"></div>
<iframe src="javascript:''"
id="__gwt_historyFrame"
tabIndex='-1'
style="position:absolute;width:0;height:0;border:0">
</iframe>
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled
in order for this application to display correctly.
</div>
</noscript>
 
</body>
</html>
pom.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
 
<groupId>com.moesol.examples.selenium</groupId>
<artifactId>smartgwt-selenium-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
 
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
 
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
<version>2.4.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-servlet</artifactId>
<version>2.4.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
 
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.4.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<runTarget>index.html</runTarget>
<module>com.moesol.examples.selenium.gwt.App</module>
<logLevel>INFO</logLevel>
<style>OBF</style>
<copyWebapp>true</copyWebapp>
</configuration>
</plugin>
</plugins>
</build>
 
</project>
web.xml
XML
1 2 3 4 5 6 7
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>App</display-name>
</web-app>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.