Commits

Vineet Reynolds committed 6cb16a7

Added Selenium tests for uploading of photos. Modified the facelet tag for displaying INFO/ERROR messages in the UploadPhoto page.

  • Participants
  • Parent commits f2d822f

Comments (0)

Files changed (6)

galleria-jsf/src/main/webapp/private/photo/UploadPhoto.xhtml

 					</li>
 				</ul>
 			</h:form>
-			<h:messages id="messages" globalOnly="true" infoClass="infoStyle"
-						warnClass="warnStyle" errorClass="errorStyle" />
+			<h:messages id="messages" showSummary="true" showDetail="false" infoClass="infoStyle"
+				warnClass="warnStyle" errorClass="errorStyle" />
 		</div>
 	</ui:define>
 </ui:composition>

galleria-jsf/src/test/java/info/galleria/view/AllPagesIntegrationTest.java

 import static org.junit.Assert.*;
 
 import info.galleria.view.album.*;
+import info.galleria.view.photo.UploadPhotoPage;
 import info.galleria.view.user.*;
 
 import java.io.File;
 import org.openqa.selenium.support.ui.*;
 import org.slf4j.*;
 
+
 @RunWith(Arquillian.class)
 @RunAsClient
 public class AllPagesIntegrationTest
 		assertArrayEquals(deletionMessages, homePage.fetchSuccessMessages());
 	}
 
+	@Test
+	public void testUploadPhoto() throws Exception
+	{
+		String albumName = "Album#1";
+		String albumDescription = "My first album";
+		String photoTitle = "Photo#1";
+		String photoDescription = "My first photo";
+		String filePath = this.getClass().getClassLoader().getResource("TestPhoto.png").toExternalForm();
+
+		driver.get(contextPath.toString());
+		IndexPage indexPage = new IndexPage(driver, contextPath);
+		SignupPage signupPage = indexPage.chooseToSignup();
+		indexPage = signupPage.signupAs("User#1", "password");
+		String[] message = { "The new user account has been created. You may now login." };
+		assertArrayEquals(message, indexPage.fetchSuccessMessages());
+
+		LoginPage loginPage = indexPage.chooseToLogin();
+		HomePage homePage = loginPage.loginAs("User#1", "password");
+		CreateAlbumPage createAlbumPage = homePage.goToCreateAlbum();
+		homePage = createAlbumPage.createAlbumAs(albumName, albumDescription);
+		String[] messages = { "The album was created successfully." };
+		assertArrayEquals(messages, homePage.fetchSuccessMessages());
+
+		UploadPhotoPage uploadPhotoPage = homePage.goToUploadPhoto();
+		ViewAlbumPage viewAlbumPage = uploadPhotoPage.uploadAs(photoTitle, photoDescription, filePath);
+		String[] uploadMessages = { "The file was uploaded successfully." };
+		assertArrayEquals(uploadMessages, viewAlbumPage.fetchSuccessMessages());
+	}
+	
+	@Test
+	public void testUploadPhotoWithNoValues() throws Exception
+	{
+		String albumName = "Album#1";
+		String albumDescription = "My first album";
+
+		driver.get(contextPath.toString());
+		IndexPage indexPage = new IndexPage(driver, contextPath);
+		SignupPage signupPage = indexPage.chooseToSignup();
+		indexPage = signupPage.signupAs("User#1", "password");
+		String[] message = { "The new user account has been created. You may now login." };
+		assertArrayEquals(message, indexPage.fetchSuccessMessages());
+
+		LoginPage loginPage = indexPage.chooseToLogin();
+		HomePage homePage = loginPage.loginAs("User#1", "password");
+		CreateAlbumPage createAlbumPage = homePage.goToCreateAlbum();
+		homePage = createAlbumPage.createAlbumAs(albumName, albumDescription);
+		String[] messages = { "The album was created successfully." };
+		assertArrayEquals(messages, homePage.fetchSuccessMessages());
+
+		UploadPhotoPage uploadPhotoPage = homePage.goToUploadPhoto();
+		uploadPhotoPage = uploadPhotoPage.uploadAsExpectingError("", "", "");
+		String[] uploadMessages = { "The name of the uploaded file should be between 1 and 255 characters in length." };
+		assertArrayEquals(uploadMessages, uploadPhotoPage.fetchErrorMessages());
+	}
+	
+	@Test
+	public void testUploadPhotoWithExcessiveValues() throws Exception
+	{
+		String albumName = "Album#1";
+		String albumDescription = "My first album";
+		String photoTitle = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+		String photoDescription = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+		String filePath = this.getClass().getClassLoader().getResource("TestPhoto.png").toExternalForm();
+
+		driver.get(contextPath.toString());
+		IndexPage indexPage = new IndexPage(driver, contextPath);
+		SignupPage signupPage = indexPage.chooseToSignup();
+		indexPage = signupPage.signupAs("User#1", "password");
+		String[] message = { "The new user account has been created. You may now login." };
+		assertArrayEquals(message, indexPage.fetchSuccessMessages());
+
+		LoginPage loginPage = indexPage.chooseToLogin();
+		HomePage homePage = loginPage.loginAs("User#1", "password");
+		CreateAlbumPage createAlbumPage = homePage.goToCreateAlbum();
+		homePage = createAlbumPage.createAlbumAs(albumName, albumDescription);
+		String[] messages = { "The album was created successfully." };
+		assertArrayEquals(messages, homePage.fetchSuccessMessages());
+
+		UploadPhotoPage uploadPhotoPage = homePage.goToUploadPhoto();
+		uploadPhotoPage = uploadPhotoPage.uploadAsExpectingError(photoTitle, photoDescription, filePath);
+		String[] uploadMessages = { "The title should be between 0 and 255 characters in length.", "The description should be between 0 and 255 characters in length." };
+		assertArrayEquals(uploadMessages, uploadPhotoPage.fetchErrorMessages());
+	}
+
+	@Test
+	public void testUploadDuplicatePhoto() throws Exception
+	{
+		String albumName = "Album#1";
+		String albumDescription = "My first album";
+		String photoTitle = "Photo#1";
+		String photoDescription = "My first photo";
+		String filePath = this.getClass().getClassLoader().getResource("TestPhoto.png").toExternalForm();
+
+		driver.get(contextPath.toString());
+		IndexPage indexPage = new IndexPage(driver, contextPath);
+		SignupPage signupPage = indexPage.chooseToSignup();
+		indexPage = signupPage.signupAs("User#1", "password");
+		String[] message = { "The new user account has been created. You may now login." };
+		assertArrayEquals(message, indexPage.fetchSuccessMessages());
+
+		LoginPage loginPage = indexPage.chooseToLogin();
+		HomePage homePage = loginPage.loginAs("User#1", "password");
+		CreateAlbumPage createAlbumPage = homePage.goToCreateAlbum();
+		homePage = createAlbumPage.createAlbumAs(albumName, albumDescription);
+		String[] messages = { "The album was created successfully." };
+		assertArrayEquals(messages, homePage.fetchSuccessMessages());
+
+		UploadPhotoPage uploadPhotoPage = homePage.goToUploadPhoto();
+		ViewAlbumPage viewAlbumPage = uploadPhotoPage.uploadAs(photoTitle, photoDescription, filePath);
+		String[] uploadMessages = { "The file was uploaded successfully." };
+		assertArrayEquals(uploadMessages, viewAlbumPage.fetchSuccessMessages());
+		
+		uploadPhotoPage = viewAlbumPage.goToUploadPhoto();
+		uploadPhotoPage = uploadPhotoPage.uploadAsExpectingError(photoTitle, photoDescription, filePath);
+		String[] errorMessages = { "A duplicate photo was found in the album." };
+		assertArrayEquals(errorMessages, uploadPhotoPage.fetchErrorMessages());
+	}
 }

galleria-jsf/src/test/java/info/galleria/view/album/ViewAlbumPage.java

 package info.galleria.view.album;
 
 import info.galleria.view.PageUtilities;
+import info.galleria.view.photo.UploadPhotoPage;
 import info.galleria.view.user.HomePage;
 
 import java.net.URI;
 		return new HomePage(driver, contextPath);
 	}
 
+	public UploadPhotoPage goToUploadPhoto()
+	{
+		driver.findElement(By.id("Upload")).click();
+		return new UploadPhotoPage(driver, contextPath);
+	}
+
 	public String[] fetchSuccessMessages()
 	{
 		List<String> messages = new ArrayList<String>();

galleria-jsf/src/test/java/info/galleria/view/photo/UploadPhotoPage.java

+package info.galleria.view.photo;
+
+import info.galleria.view.PageUtilities;
+import info.galleria.view.album.ViewAlbumPage;
+
+import java.net.URI;
+import java.util.*;
+
+import org.openqa.selenium.*;
+import org.openqa.selenium.support.ui.*;
+
+public class UploadPhotoPage
+{
+
+	private WebDriver driver;
+	private URI contextPath;
+
+	public UploadPhotoPage(WebDriver driver, URI contextPath)
+	{
+		this.driver = driver;
+		this.contextPath = contextPath;
+		Wait<WebDriver> wait = new WebDriverWait(driver, 15);
+		wait.until(PageUtilities.visibilityOfElementLocated(By.id("wrapper")));
+		if (!driver.getTitle().equals("Upload a photo"))
+		{
+			throw new IllegalStateException("This is not the Upload Photo page.");
+		}
+	}
+
+	public ViewAlbumPage uploadAs(String photoTitle, String photoDescription, String filePath)
+	{
+		driver.findElement(By.id("multipartForm:photoTitle")).clear();
+		driver.findElement(By.id("multipartForm:photoTitle")).sendKeys(photoTitle);
+		driver.findElement(By.id("multipartForm:photoDescription")).clear();
+		driver.findElement(By.id("multipartForm:photoDescription")).sendKeys(photoDescription);
+		driver.findElement(By.id("multipartForm:photoUploader")).clear();
+		driver.findElement(By.id("multipartForm:photoUploader")).sendKeys(filePath);
+		driver.findElement(By.id("multipartForm:uploadButton")).click();
+		return new ViewAlbumPage(driver, contextPath);
+	}
+
+	public UploadPhotoPage uploadAsExpectingError(String photoTitle, String photoDescription, String filePath)
+	{
+		driver.findElement(By.id("multipartForm:photoTitle")).clear();
+		driver.findElement(By.id("multipartForm:photoTitle")).sendKeys(photoTitle);
+		driver.findElement(By.id("multipartForm:photoDescription")).clear();
+		driver.findElement(By.id("multipartForm:photoDescription")).sendKeys(photoDescription);
+		driver.findElement(By.id("multipartForm:photoUploader")).clear();
+		driver.findElement(By.id("multipartForm:photoUploader")).sendKeys(filePath);
+		driver.findElement(By.id("multipartForm:uploadButton")).click();
+		return new UploadPhotoPage(driver, contextPath);
+	}
+
+	public String[] fetchErrorMessages()
+	{
+		List<String> messages = new ArrayList<String>();
+		for (WebElement element : driver.findElements(By.xpath("//ul[@id='messages']/li[@class='errorStyle']")))
+		{
+			messages.add(element.getText());
+		}
+		return messages.toArray(new String[0]);
+	}
+}

galleria-jsf/src/test/java/info/galleria/view/user/HomePage.java

 
 import info.galleria.view.PageUtilities;
 import info.galleria.view.album.*;
+import info.galleria.view.photo.UploadPhotoPage;
 
 import java.net.URI;
 import java.util.*;
 		return null;
 	}
 
+	public UploadPhotoPage goToUploadPhoto()
+	{
+		driver.findElement(By.id("Upload")).click();
+		return new UploadPhotoPage(driver, contextPath);
+	}
+
 }

galleria-jsf/src/test/resources/TestPhoto.png

Added
New image