Skip to content

Instantly share code, notes, and snippets.

@mmadson
Created December 22, 2011 18:41
Show Gist options
  • Save mmadson/1511360 to your computer and use it in GitHub Desktop.
Save mmadson/1511360 to your computer and use it in GitHub Desktop.
Selenium test for GWT FileUpload, e.g., how to specify a file path using Selenium for <input type="file"...>
<!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>
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);
}
}
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");
}
}
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");
}
}
<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>
<?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>
<?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>
@mmadson
Copy link
Author

mmadson commented Dec 22, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment