Skip to content

Commit

Permalink
Initial WebDriver tests (work in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamtriggs committed Dec 16, 2015
1 parent 6574e47 commit d582317
Show file tree
Hide file tree
Showing 9 changed files with 2,060 additions and 0 deletions.
41 changes: 41 additions & 0 deletions selenium/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.vivoweb</groupId>
<artifactId>vivo-selenium</artifactId>
<version>1.9.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>VIVO Selenium Tests</name>

<build>
<plugins>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.48.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.vivoweb.vivo.selenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public final class DriverFactory {
private static WebDriver driver = null;
private static Object closeToken = null;

public static WebDriver getDriver() {
if (driver == null) {
driver = new FirefoxDriver();
}

return driver;
}

public static void close() {
if (closeToken == null && driver != null) {
driver.quit();
driver = null;
}
}

public static void close(Object token) {
if (closeToken == token || (closeToken != null && closeToken.equals(token))) {
if (driver != null) {
driver.quit();
driver = null;
}
}
}

public static void setCloseToken(Object token) {
closeToken = token;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.vivoweb.vivo.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public final class SeleniumUtils {
private static String baseUrl = "http://localhost:8080/vivo";

public static void setBaseUrl(String baseUrl) {
SeleniumUtils.baseUrl = baseUrl;
}

public static String makeUrl(String urlPart) {
if (urlPart.startsWith("/")) {
return baseUrl + urlPart;
} else {
return baseUrl + "/" + urlPart;
}
}

public static void navigate(WebDriver driver, String urlPart) {
driver.navigate().to(makeUrl(urlPart));
}
}
40 changes: 40 additions & 0 deletions selenium/src/test/java/org/vivoweb/vivo/selenium/VIVOSuite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.vivoweb.vivo.selenium;

import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.List;

public class VIVOSuite extends Suite {
public VIVOSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(klass, builder);
}

public VIVOSuite(RunnerBuilder builder, Class<?>[] classes) throws InitializationError {
super(builder, classes);
}

protected VIVOSuite(Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
super(klass, suiteClasses);
}

protected VIVOSuite(RunnerBuilder builder, Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
super(builder, klass, suiteClasses);
}

protected VIVOSuite(Class<?> klass, List<Runner> runners) throws InitializationError {
super(klass, runners);
}

@Override
protected void runChild(Runner runner, RunNotifier notifier) {
// Set Driver factory, can run multiple times
super.runChild(runner, notifier);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.vivoweb.vivo.selenium.suites;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;
import org.vivoweb.vivo.selenium.DriverFactory;
import org.vivoweb.vivo.selenium.VIVOSuite;
import org.vivoweb.vivo.selenium.tests.CreateOrganization;
import org.vivoweb.vivo.selenium.tests.RebuildSearchIndex;

@RunWith(VIVOSuite.class)
@SuiteClasses(
{
RebuildSearchIndex.class,
CreateOrganization.class
}
)
public class AddNonPersonThings {
@BeforeClass
public static void setup() {
DriverFactory.setCloseToken(AddNonPersonThings.class);
}

@AfterClass
public static void shutdown() {
DriverFactory.close(AddNonPersonThings.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.vivoweb.vivo.selenium.tests;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
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;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.vivoweb.vivo.selenium.DriverFactory;
import org.vivoweb.vivo.selenium.SeleniumUtils;

public class AbstractSeleniumTest {
protected WebDriver driver;

protected void assertTitle(String title) {
Assert.assertEquals(title, driver.getTitle());
}

protected void clickAndWait(By by) {
driver.findElement(by).click();
}

protected void deleteAllVisibleCookies() {
driver.manage().deleteAllCookies();
}

protected void logIn(String email, String password) {
clickAndWait(By.linkText("Log in")); // clickAndWait,link=Log in
assertTitle("Log in to VIVO"); // aseertTitle,Log in to VIVO

type(By.id("loginName"), email); // type,id=loginName,[email protected]
type(By.id("loginPassword"), password); // type,id=loginPassword,Password

clickAndWait(By.name("loginForm")); // clickAndWait,name=loginForm
assertTitle("VIVO"); // assertTitle,VIVO
}

protected void logOut() {
Actions actions = new Actions(driver);
actions.moveToElement( driver.findElement(By.id("user-menu")) ).perform();
driver.findElement(By.linkText("Log out")).click();
}

protected void open(String urlPart) {
SeleniumUtils.navigate(driver, urlPart);
}

protected void selectByLabel(By by, String label) {
Select select = new Select(driver.findElement(by));
select.selectByVisibleText(label);

}

protected void type(By by, String text) {
driver.findElement(by).sendKeys(text);
}

protected void typeTinyMCE(String text) {
// <td> tinyMCE.activeEditor.setContent('The Primate College of America is a privately-funded college for the study of primates.')</td>

driver.switchTo().frame("literal_ifr");
WebElement element = driver.findElement(By.cssSelector("body"));
element.click();
element.sendKeys(text);
driver.switchTo().defaultContent();
}

protected void verifyTextPresent(String text) {
Assert.assertNotNull(driver.findElement(xpathForTextPresent(text)));
}

protected boolean waitForElementPresent(By by) {
return waitForElementPresent(by, 30);
}

protected boolean waitForElementPresent(By by, int timeout) {
WebDriverWait wait = new WebDriverWait(driver, timeout);
return wait.until(ExpectedConditions.presenceOfElementLocated(by)) != null;
}

protected boolean waitForTextPresent(String text) {
return waitForTextPresent(text, 30);
}

protected boolean waitForTextPresent(String text, int timeout) {
WebDriverWait wait = new WebDriverWait(driver, timeout);
return wait.until(ExpectedConditions.presenceOfElementLocated(xpathForTextPresent(text))) != null;
}

protected By xpathForTextPresent(String text) {
return By.xpath("//*[text()[contains(.,'" + text + "')]]");
}

@Before
public void setup() {
driver = DriverFactory.getDriver();
}

@After
public void cleanup() {
DriverFactory.close();
}
}
Loading

0 comments on commit d582317

Please sign in to comment.