From 2a82238d196f93b87baafdf7b8b5152e338eab2e Mon Sep 17 00:00:00 2001
From: Christoph Thelen <christoph.thelen@mni.thm.de>
Date: Tue, 30 Oct 2012 08:47:34 +0100
Subject: [PATCH] Selenium integration tests

---
 .gitignore                                    |  1 +
 pom.xml                                       | 90 ++++++++++++++++++-
 .../java/de/thm/arsnova/dao/CouchDBDao.java   |  8 ++
 .../de/thm/arsnova/ARSnovaChromeDriver.java   | 35 ++++++++
 .../java/de/thm/arsnova/Selenium2Test.java    | 87 ++++++++++++++++++
 5 files changed, 219 insertions(+), 2 deletions(-)
 create mode 100644 src/test/java/de/thm/arsnova/ARSnovaChromeDriver.java
 create mode 100644 src/test/java/de/thm/arsnova/Selenium2Test.java

diff --git a/.gitignore b/.gitignore
index 3e1b24d5a..5c71b5f8f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
 .classpath
 .settings/*
 target/*
+chromedriver.log
diff --git a/pom.xml b/pom.xml
index b44ab394b..411ffb8e9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -222,7 +222,13 @@
 			<groupId>couchdb4j</groupId>
 			<artifactId>couchdb4j</artifactId>
 			<version>0.3.0-i386-1</version>
-
+			<exclusions>
+				<!-- Exclude httpclient: Selenium loads a more current version -->
+				<exclusion>
+					<groupId>org.apache.httpcomponents</groupId>
+					<artifactId>httpclient</artifactId>
+				</exclusion>
+			</exclusions>
 		</dependency>
 		<dependency>
 			<groupId>net.sf.ezmorph</groupId>
@@ -295,6 +301,11 @@
 			<artifactId>spring-aspects</artifactId>
 			<version>${org.springframework-version}</version>
 		</dependency>
+		<dependency>
+			<groupId>org.seleniumhq.selenium</groupId>
+			<artifactId>selenium-java</artifactId>
+			<version>2.25.0</version>
+		</dependency>
 	</dependencies>
 	<build>
 		<plugins>
@@ -305,7 +316,7 @@
 				<configuration>
 					<source>1.6</source>
 					<target>1.6</target>
-					
+
 				</configuration>
 			</plugin>
 			<plugin>
@@ -330,6 +341,9 @@
 				<artifactId>maven-surefire-plugin</artifactId>
 				<version>2.12.2</version>
 				<configuration>
+					<excludes>
+						<exclude>**/Selenium*.java</exclude>
+					</excludes>
 				</configuration>
 			</plugin>
 			<plugin>
@@ -356,6 +370,78 @@
 					<configLocation>ARSnova-checkstyle-checker.xml</configLocation>
 				</configuration>
 			</plugin>
+			<plugin>
+				<groupId>org.apache.tomcat.maven</groupId>
+				<artifactId>tomcat7-maven-plugin</artifactId>
+				<version>2.0</version>
+				<configuration>
+					<url>http://localhost:8080/manager/text</url>
+					<server>arsnova</server>
+					<path>/arsnova-war</path>
+				</configuration>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-failsafe-plugin</artifactId>
+				<version>2.12.4</version>
+				<configuration>
+					<includes>
+						<include>**/Selenium*.java</include>
+					</includes>
+				</configuration>
+				<executions>
+					<execution>
+						<goals>
+							<goal>integration-test</goal>
+							<goal>verify</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+			<plugin>
+				<groupId>org.codehaus.cargo</groupId>
+				<artifactId>cargo-maven2-plugin</artifactId>
+				<version>1.2.0</version>
+				<configuration>
+					<container>
+						<containerId>tomcat7x</containerId>
+						<zipUrlInstaller>
+							<url>http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.16/bin/apache-tomcat-7.0.16.zip
+							</url>
+							<downloadDir>${project.build.directory}/downloads</downloadDir>
+							<extractDir>${project.build.directory}/extracts</extractDir>
+						</zipUrlInstaller>
+					</container>
+					<configuration>
+						<deployables>
+							<deployable>
+								<groupId>de.thm.arsnova</groupId>
+								<artifactId>arsnova-war</artifactId>
+								<type>war</type>
+								<properties>
+									<context>arsnova-war</context>
+								</properties>
+							</deployable>
+						</deployables>
+					</configuration>
+				</configuration>
+				<executions>
+					<execution>
+						<id>start-tomcat</id>
+						<phase>pre-integration-test</phase>
+						<goals>
+							<goal>start</goal>
+						</goals>
+					</execution>
+					<execution>
+						<id>stop-tomcat</id>
+						<phase>post-integration-test</phase>
+						<goals>
+							<goal>stop</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
 		</plugins>
 	</build>
 
diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
index ba0357059..930197e79 100644
--- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
+++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
@@ -105,6 +105,14 @@ public class CouchDBDao implements IDatabaseDao {
 		logger.info(databaseName);
 		this.databaseName = databaseName;
 	}
+	
+	public void setSessionService(ISessionService sessionService) {
+		this.sessionService = sessionService;
+	}
+	
+	public void setUserService(IUserService userService) {
+		this.userService = userService;
+	}
 
 	/**
 	 * This method cleans up old feedback votes at the scheduled interval.
diff --git a/src/test/java/de/thm/arsnova/ARSnovaChromeDriver.java b/src/test/java/de/thm/arsnova/ARSnovaChromeDriver.java
new file mode 100644
index 000000000..97905e6ac
--- /dev/null
+++ b/src/test/java/de/thm/arsnova/ARSnovaChromeDriver.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2012 THM webMedia
+ * 
+ * This file is part of ARSnova.
+ *
+ * ARSnova is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova;
+
+import org.openqa.selenium.chrome.ChromeDriver;
+import org.openqa.selenium.html5.*;
+import org.openqa.selenium.remote.html5.*;
+
+public class ARSnovaChromeDriver extends ChromeDriver implements WebStorage {
+
+	public LocalStorage getLocalStorage() {
+		return new RemoteLocalStorage(this.getExecuteMethod());
+	}
+
+	@Override
+	public SessionStorage getSessionStorage() {
+		return new RemoteSessionStorage(this.getExecuteMethod());
+	}
+}
diff --git a/src/test/java/de/thm/arsnova/Selenium2Test.java b/src/test/java/de/thm/arsnova/Selenium2Test.java
new file mode 100644
index 000000000..58f12413b
--- /dev/null
+++ b/src/test/java/de/thm/arsnova/Selenium2Test.java
@@ -0,0 +1,87 @@
+package de.thm.arsnova;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.util.Properties;
+
+import org.junit.*;
+import org.openqa.selenium.*;
+import org.openqa.selenium.html5.LocalStorage;
+import org.openqa.selenium.support.ui.*;
+import org.springframework.core.io.*;
+import org.springframework.core.io.support.PropertiesLoaderUtils;
+
+import de.thm.arsnova.dao.CouchDBDao;
+import de.thm.arsnova.entities.Session;
+import de.thm.arsnova.services.*;
+
+public class Selenium2Test {
+
+	ARSnovaChromeDriver driver;
+	Properties properties;
+	
+	CouchDBDao couchdbDao;
+	SessionService sessionService;
+	StubUserService userService;
+
+	@Before
+	public void setUp() throws IOException {
+		Resource resource = new FileSystemResource("/etc/arsnova/arsnova.properties");
+		properties = PropertiesLoaderUtils.loadProperties(resource);
+		
+		userService = new StubUserService();
+		userService.setUserAuthenticated(true);
+		
+		couchdbDao = new CouchDBDao();
+		couchdbDao.setDatabaseHost(properties.getProperty("couchdb.host", "localhost"));
+		couchdbDao.setDatabasePort(properties.getProperty("couchdb.port", "5984"));
+		couchdbDao.setDatabaseName(properties.getProperty("couchdb.name", "arsnova"));
+		sessionService = new SessionService();
+		couchdbDao.setSessionService(sessionService);
+		couchdbDao.setUserService(userService);
+		sessionService.setDatabaseDao(couchdbDao);
+		
+		this.driver = new ARSnovaChromeDriver();
+		driver.get(properties.getProperty("security.arsnova-url", "http://localhost:8080/arsnova-war/"));
+		LocalStorage localStorage = this.driver.getLocalStorage();
+		localStorage.setItem("html5 info read", "");
+	}
+
+	@After
+	public void tearDown() {
+		driver.close();
+		driver.quit();
+	}
+	
+	@Test
+	public void studentGuestShouldBeAbleToJoinSession() {
+		Session session = new Session();
+		session.setName("selenium test session");
+		session.setShortName("selenium");
+		session = couchdbDao.saveSession(session);
+		
+		WebElement studentRoleButton = waitForElement(By.id("ext-gen1047"));
+		studentRoleButton.click();
+		
+		WebElement guestLoginButton = waitForElement(By.id("ext-gen1016"));
+		guestLoginButton.click();
+		
+		WebElement sessionKeywordField = waitForElement(By.name("keyword"));
+		sessionKeywordField.sendKeys(session.getKeyword());
+		WebElement joinSessionButton = waitForElement(By.id("ext-gen1138"));
+		joinSessionButton.click();
+		
+		WebElement feedbackGoodButton = waitForElement(By.className("feedbackGood"));
+		assertTrue(feedbackGoodButton.isDisplayed());
+	}
+	
+	private WebElement waitForElement(final By by) {
+		(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
+			public Boolean apply(WebDriver d) {
+				return d.findElement(by) != null;
+			}
+		});
+		return driver.findElement(by);
+	}
+}
\ No newline at end of file
-- 
GitLab