r3619 - in trunk/pollen-ui-struts2: . src/test/java/org/chorem/pollen/ui src/test/java/org/chorem/pollen/ui/it
Author: tchemit Date: 2012-08-19 13:20:12 +0200 (Sun, 19 Aug 2012) New Revision: 3619 Url: http://chorem.org/repositories/revision/pollen/3619 Log: try selenium test with same driver for a complete class test Added: trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/ trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/AbstractPollenWebDriverIT.java trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/LoginT.java trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/WebDriverResources.java Modified: trunk/pollen-ui-struts2/pom.xml trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/PollenFixtures.java Modified: trunk/pollen-ui-struts2/pom.xml =================================================================== --- trunk/pollen-ui-struts2/pom.xml 2012-08-19 11:19:48 UTC (rev 3618) +++ trunk/pollen-ui-struts2/pom.xml 2012-08-19 11:20:12 UTC (rev 3619) @@ -277,7 +277,8 @@ <deployFiles>target/${project.build.finalName}.war</deployFiles> <defaultWebContextPath>pollen</defaultWebContextPath> - <defaultSiteUrl>http://localhost:8080/${defaultWebContextPath} + <defaultSiteUrl> + http://localhost:8080/${defaultWebContextPath} </defaultSiteUrl> <defaultLogDir>${basedir}/target</defaultLogDir> </properties> @@ -605,7 +606,8 @@ <configuration> <daemon>true</daemon> <webAppConfig> - <descriptor>${basedir}/src/test/resources/web-test.xml + <descriptor> + ${basedir}/src/test/resources/web-test.xml </descriptor> <contextPath>/${defaultWebContextPath}</contextPath> @@ -648,7 +650,7 @@ <siteUrl>${pollenTestUrl}</siteUrl> </environmentVariables> <includes> - <include>**/*SIT.java</include> + <include>**/LoginSIT.java</include> </includes> </configuration> </execution> @@ -660,6 +662,96 @@ </profile> <profile> + <id>selenium2</id> + <!--activation> + <property> + <name>maven.test.skip</name> + <value>!true</value> + </property> + </activation--> + <properties> + <pollenTestServerPort>8765</pollenTestServerPort> + <pollenTestUrl> + http://localhost:${pollenTestServerPort}/${defaultWebContextPath} + </pollenTestUrl> + </properties> + + <build> + <defaultGoal>post-integration-test</defaultGoal> + <plugins> + <plugin> + <groupId>org.mortbay.jetty</groupId> + <artifactId>jetty-maven-plugin</artifactId> + <executions> + + <execution> + <id>start-pollen</id> + <phase>pre-integration-test</phase> + <goals> + <goal>start</goal> + </goals> + <configuration> + <daemon>true</daemon> + <webAppConfig> + <descriptor> + ${basedir}/src/test/resources/web-test.xml + </descriptor> + <contextPath>/${defaultWebContextPath}</contextPath> + + </webAppConfig> + <useTestClasspath>true</useTestClasspath> + <systemProperties> + <systemProperty> + <name>siteUrl</name> + <value>${pollenTestUrl}</value> + </systemProperty> + <systemProperty> + <name>jetty.port</name> + <value>${pollenTestServerPort}</value> + </systemProperty> + </systemProperties> + </configuration> + </execution> + + <execution> + <id>stop-pollen</id> + <phase>post-integration-test</phase> + <goals> + <goal>stop</goal> + </goals> + </execution> + </executions> + </plugin> + + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <executions> + + <execution> + <phase>integration-test</phase> + <goals> + <goal>test</goal> + </goals> + <configuration> + <environmentVariables> + <siteUrl>${pollenTestUrl}</siteUrl> + </environmentVariables> + <includes> + <include>**/*T.java</include> + </includes> + <excludes> + <exclude>**/*SIT.java</exclude> + </excludes> + </configuration> + </execution> + </executions> + </plugin> + + </plugins> + </build> + </profile> + + <profile> <id>selenium-server</id> <properties> <pollenTestServerPort>8765</pollenTestServerPort> @@ -686,7 +778,8 @@ <configuration> <daemon>false</daemon> <webAppConfig> - <descriptor>${basedir}/src/test/resources/web-test.xml + <descriptor> + ${basedir}/src/test/resources/web-test.xml </descriptor> <contextPath>/${defaultWebContextPath}</contextPath> Modified: trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/PollenFixtures.java =================================================================== --- trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/PollenFixtures.java 2012-08-19 11:19:48 UTC (rev 3618) +++ trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/PollenFixtures.java 2012-08-19 11:20:12 UTC (rev 3619) @@ -163,4 +163,7 @@ return date(year, month, day, 0, 0); } + public String logoutURL() { + return baseUrl() + "user/logout"; + } } Added: trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/AbstractPollenWebDriverIT.java =================================================================== --- trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/AbstractPollenWebDriverIT.java (rev 0) +++ trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/AbstractPollenWebDriverIT.java 2012-08-19 11:20:12 UTC (rev 3619) @@ -0,0 +1,205 @@ +package org.chorem.pollen.ui.it; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.ui.PollenFixtures; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; + +import java.util.Collection; +import java.util.List; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since TODO + */ +@RunWith(value = Parameterized.class) +public abstract class AbstractPollenWebDriverIT { + + /** Logger. */ + protected final Log log = LogFactory.getLog(getClass()); + + @Parameterized.Parameters + public static Collection<Object[]> driverTypes() { + return WebDriverResources.driverTypes(); + } + + @ClassRule + public static final WebDriverResources seleniumServer = new WebDriverResources(); + + protected final Class<? extends WebDriver> driverType; + + protected WebDriver driver; + + protected PollenFixtures fixtures; + + @Before + public void setUp() throws Exception { + driver = seleniumServer.getDriver(driverType); + fixtures = new PollenFixtures(); + } + + @After + public void after() { + + // make sure driver came back in original state: + // go back to home page and logout if was loggued in + + gotoUrl(fixtures.logoutURL(), fixtures.homeURL(), true); + + } + + protected AbstractPollenWebDriverIT(Class<? extends WebDriver> driverType) { + this.driverType = driverType; + } + + protected void gotoUrl(String url, boolean strict) { + gotoUrl(url, null, strict); + } + + protected void gotoUrl(String url) { + gotoUrl(url, null, true); + } + + protected void gotoUrl(String url, String fallBackUrl) { + gotoUrl(url, fallBackUrl, true); + } + + protected void gotoUrl(String url, String fallBackUrl, boolean strict) { + driver.get(url); + String expectedUrl; + if (fallBackUrl == null) { + expectedUrl = url; + } else { + expectedUrl = fallBackUrl; + } + checkCurrentUrl(expectedUrl, strict); + } + + /** + * Check that the current URL is the expected one. + * If it is a strict comparison, the URL should be the same that the wanted. + * If not, it only checks that the current URL start with the wanted url. + */ + protected void checkCurrentUrl(String expectedUrl, boolean strict) { + if (strict) { + Assert.assertEquals("Current url [" + driver.getCurrentUrl() + "] should be " + expectedUrl, expectedUrl, driver.getCurrentUrl()); + } else { + Assert.assertTrue("Current url [" + driver.getCurrentUrl() + "] should starts with " + expectedUrl, driver.getCurrentUrl().startsWith(expectedUrl)); + } + } + + protected WebElement findElement(By by) { + WebElement element = driver.findElement(by); + return element; + } + + protected List<WebElement> findElements(By by) { + List<WebElement> result = driver.findElements(by); + return result; + } + + /** Try to log in using the given {@code username} and {@code password}. */ + protected void connect(String username, String password) { + + // click on tologin element (display the login form) + WebElement loginClick = driver.findElement(By.id("tologin")); + loginClick.click(); + + // Find the text input element by its name + WebElement loginElement = driver.findElement(By.name("login")); + Assert.assertEquals("input", loginElement.getTagName()); + Assert.assertTrue(loginElement.isDisplayed()); + loginElement.sendKeys(username); + + WebElement passwordElement = driver.findElement(By.name("password")); + Assert.assertTrue(passwordElement.isDisplayed()); + Assert.assertEquals("input", passwordElement.getTagName()); + passwordElement.sendKeys(password); + + WebElement submit = driver.findElement(By.name("action:login")); + submit.click(); + } + + /** Prepare the poll with general data : one title and two choices */ + void preparePoll() { + // Go on home page + gotoUrl(fixtures.createPollURL()); + + // Set title + sendKeysByName("poll.title", "My Poll"); + + // Set first choice + sendKeysByName("textChoice_0.name", "Choix numero un"); + + // Set second choice + sendKeysByName("textChoice_1.name", "Choix numero deux"); + } + + /** + * Set a value for a WebElement. + * This operations does 3 steps : + * <ul> + * <li>Get the WebElement thanks to its id (given in parameter)</li> + * <li>check the element is an input</li> + * <li>check the element is displayed</li> + * <li>clear the element</li> + * <li>send the wanted keys</li> + * </ul> + */ + protected void sendKeysById(String webElementId, String value) { + sendKeysBy(By.id(webElementId), value); + } + + /** + * Set a value for a WebElement. + * This operations does 3 steps : + * <ul> + * <li>Get the WebElement thanks to its name (given in parameter)</li> + * <li>check the element is an input</li> + * <li>check the element is displayed</li> + * <li>clear the element</li> + * <li>send the wanted keys</li> + * </ul> + */ + protected void sendKeysByName(String webElementName, String value) { + By byName = By.name(webElementName); + sendKeysBy(byName, value); + } + + /** + * Set a value for a WebElement. + * This operations does 3 steps : + * <ul> + * <li>Get the WebElement thanks to a {@link By} locator</li> + * <li>check the element is an input</li> + * <li>check the element is displayed</li> + * <li>clear the element</li> + * <li>send the wanted keys</li> + * </ul> + */ + protected void sendKeysBy(By by, String value) { + WebElement webElement = findElement(by); + Assert.assertEquals("input", webElement.getTagName()); + Assert.assertTrue(webElement.isDisplayed()); + webElement.sendKeys(value); + Assert.assertEquals(value, webElement.getAttribute("value")); + } + + /** Get a WebElement with a {@link By} locator, and check it is an input and that is value is the one expected. */ + protected void checkWebElementValue(By by, String expectedValue) { + WebElement webElement = findElement(by); + Assert.assertEquals("input", webElement.getTagName()); + Assert.assertTrue(webElement.isDisplayed()); + Assert.assertEquals(expectedValue, webElement.getAttribute("value")); + } +} Property changes on: trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/AbstractPollenWebDriverIT.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/LoginT.java =================================================================== --- trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/LoginT.java (rev 0) +++ trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/LoginT.java 2012-08-19 11:20:12 UTC (rev 3619) @@ -0,0 +1,62 @@ +package org.chorem.pollen.ui.it; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; + +import java.util.List; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since TODO + */ +public class LoginT extends AbstractPollenWebDriverIT { + + public LoginT(Class<? extends WebDriver> driverType) { + super(driverType); + } + + @Test + public void loginAdmin() throws Exception { + + gotoUrl(fixtures.homeURL()); + + connect("admin", "admin"); + + // check info_success is on + findElement(By.className("info_success")); + + // check there is a admin menu (so 4 menus) + List<WebElement> menus = findElements(By.className("menu_elt")); + Assert.assertEquals(4, menus.size()); + } + + @Test + public void loginUser() throws Exception { + + gotoUrl(fixtures.homeURL()); + + connect("user", "user"); + + // check info_success is on + findElement(By.className("info_success")); + + // check there is not a admin menu (so 3 menus) + List<WebElement> menus = findElements(By.className("menu_elt")); + Assert.assertEquals(3, menus.size()); + } + + @Test + public void badLogin() throws Exception { + + gotoUrl(fixtures.homeURL()); + + connect("admin", "admin" + System.currentTimeMillis()); + + findElement(By.className("info_error")); + } +} Property changes on: trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/LoginT.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/WebDriverResources.java =================================================================== --- trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/WebDriverResources.java (rev 0) +++ trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/WebDriverResources.java 2012-08-19 11:20:12 UTC (rev 3619) @@ -0,0 +1,134 @@ +package org.chorem.pollen.ui.it; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.htmlunit.HtmlUnitDriver; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * Web driver resource. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.4 + */ +public class WebDriverResources extends TestWatcher { + + /** Logger. */ + private static final Log log = LogFactory.getLog(WebDriverResources.class); + + protected static Collection<Object[]> safeDrivers; + + protected Map<Class<? extends WebDriver>, WebDriver> servers = + Maps.newHashMap(); + + public static Collection<Object[]> driverTypes() { + if (safeDrivers == null) { + + List<Class<? extends WebDriver>> allDrivers = Lists.newArrayList(); + allDrivers.add(HtmlUnitDriver.class); + allDrivers.add(FirefoxDriver.class); +// allDrivers.add(ChromeDriver.class); +// allDrivers.add(InternetExplorerDriver.class); +// allDrivers.add(SafariDriver.class); + + List<Class<?>> checkedDrivers = Lists.newArrayList(); + for (Class<? extends WebDriver> driver : allDrivers) { + boolean ok = checkDriver(driver); + if (ok) { + checkedDrivers.add(driver); + } + } + Object[][] data = new Object[checkedDrivers.size()][1]; + int i = 0; + for (Class<?> checkedDriver : checkedDrivers) { + data[i++][0] = checkedDriver; + } + safeDrivers = Arrays.asList(data); + } + + return safeDrivers; + } + + + private static boolean checkDriver(Class<? extends WebDriver> driverType) { + + boolean result = false; + try { + createDriver(driverType, true); + result = true; + } catch (Exception e) { + + // can not use this driver + if (log.isInfoEnabled()) { + log.info("Can not use driver " + driverType.getName(), e); + } + } + + return result; + } + + public <W extends WebDriver> W getDriver(Class<W> driverType) throws Exception { + W result = (W) servers.get(driverType); + if (result == null) { + result = createDriver(driverType, false); + + if (HtmlUnitDriver.class == driverType) { + ((HtmlUnitDriver) result).setJavascriptEnabled(true); + } + servers.put(driverType, result); + } + return result; + } + + @Override + protected void starting(Description description) { + String testName = + description.getClassName() + "#" + description.getMethodName(); + + if (log.isInfoEnabled()) { + log.info("Start test " + testName); + } + } + + @Override + protected void finished(Description description) { + + // close all servers + + for (WebDriver webDriver : servers.values()) { + try { + webDriver.quit(); + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("Could not close nicely driver " + webDriver, e); + } + } + } + } + + protected static <W extends WebDriver> W createDriver(Class<W> driverType, + boolean clear) + throws Exception { + W result = driverType.getConstructor().newInstance(); + + if (clear) { + result.close(); + } + + if (HtmlUnitDriver.class == driverType) { + ((HtmlUnitDriver) result).setJavascriptEnabled(true); + } + return result; + } + +} Property changes on: trunk/pollen-ui-struts2/src/test/java/org/chorem/pollen/ui/it/WebDriverResources.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native
participants (1)
-
tchemit@users.chorem.org