more cleanup on it js

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1296210 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-03-02 14:03:41 +00:00
parent e5f22d2423
commit b143434569
2 changed files with 181 additions and 216 deletions

View File

@ -1,8 +1,6 @@
package org.apache.archiva.web.test.parent; package org.apache.archiva.web.test.parent;
import org.apache.archiva.web.test.tools.ArchivaSeleniumRunner; import org.apache.archiva.web.test.tools.ArchivaSeleniumRunner;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -36,13 +34,6 @@ public abstract class AbstractArchivaTest
protected String fullname; protected String fullname;
@Override
@After
public void close()
throws Exception
{
super.close();
}
@Override @Override
@Before @Before
@ -53,49 +44,6 @@ public abstract class AbstractArchivaTest
assertAdminCreated(); assertAdminCreated();
} }
public void assertAdminCreated()
throws Exception
{
initializeArchiva( System.getProperty( "baseUrl" ), System.getProperty( "browser" ),
Integer.getInteger( "maxWaitTimeInMs" ), System.getProperty( "seleniumHost", "localhost" ),
Integer.getInteger( "seleniumPort", 4444 ) );
}
public void initializeArchiva( String baseUrl, String browser, int maxWaitTimeInMs, String seleniumHost,
int seleniumPort )
throws Exception
{
super.open( baseUrl, browser, seleniumHost, seleniumPort, Integer.toString( maxWaitTimeInMs ) );
getSelenium().open( baseUrl );
waitPage();
// if not admin user created create one
if ( isElementVisible( "create-admin-link" ) )
{
Assert.assertFalse( getSelenium().isVisible( "login-link-a" ) );
Assert.assertFalse( getSelenium().isVisible( "register-link-a" ) );
clickLinkWithLocator( "create-admin-link-a", false );
assertCreateAdmin();
String fullname = getProperty( "ADMIN_FULLNAME" );
String username = getAdminUsername();
String mail = getProperty( "ADMIN_EMAIL" );
String password = getProperty( "ADMIN_PASSWORD" );
submitAdminData( fullname, mail, password );
assertUserLoggedIn( username );
clickLinkWithLocator( "logout-link-a" );
}
else
{
Assert.assertTrue( getSelenium().isVisible( "login-link-a" ) );
Assert.assertTrue( getSelenium().isVisible( "register-link-a" ) );
login( getAdminUsername(), getAdminPassword() );
}
}
protected static String getErrorMessageText() protected static String getErrorMessageText()
{ {
return getSelenium().getText( "//ul[@class='errorMessage']/li/span" ); return getSelenium().getText( "//ul[@class='errorMessage']/li/span" );
@ -131,56 +79,6 @@ public abstract class AbstractArchivaTest
return basedir; return basedir;
} }
public String getAdminUsername()
{
String adminUsername = getProperty( "ADMIN_USERNAME" );
return adminUsername;
}
public String getAdminPassword()
{
String adminPassword = getProperty( "ADMIN_PASSWORD" );
return adminPassword;
}
public void assertCreateAdmin()
{
assertElementPresent( "user-create" );
assertFieldValue( "admin", "username" );
assertElementPresent( "fullname" );
assertElementPresent( "password" );
assertElementPresent( "confirmPassword" );
assertElementPresent( "email" );
}
public void submitAdminData( String fullname, String email, String password )
{
setFieldValue( "fullname", fullname );
setFieldValue( "email", email );
setFieldValue( "password", password );
setFieldValue( "confirmPassword", password );
clickButtonWithLocator( "user-create-form-register-button" );
//submit();
}
// Go to Login Page
public void goToLoginPage()
{
getSelenium().open( baseUrl );
waitPage();
// are we already logged in ?
if ( isElementVisible( "logout-link" ) ) //isElementPresent( "logoutLink" ) )
{
// so logout
clickLinkWithLocator( "logout-link-a", false );
clickLinkWithLocator( "login-link-a" );
}
else if ( isElementVisible( "login-link-a" ) )
{
clickLinkWithLocator( "login-link-a" );
}
assertLoginModal();
}
public void submitUserData( String username, String password, boolean rememberme, boolean success ) public void submitUserData( String username, String password, boolean rememberme, boolean success )
{ {
@ -203,15 +101,6 @@ public abstract class AbstractArchivaTest
} }
} }
public void assertLoginModal()
{
assertElementPresent( "user-login-form" );
Assert.assertTrue( isElementVisible( "register-link" ) );
assertElementPresent( "user-login-form-username" );
assertElementPresent( "user-login-form-password" );
assertButtonWithIdPresent( "modal-login-ok" );
}
// User Management // User Management
public void goToUserManagementPage() public void goToUserManagementPage()
{ {
@ -339,74 +228,6 @@ public abstract class AbstractArchivaTest
} }
public void login( String username, String password )
{
login( username, password, true, "Login Page" );
}
public void login( String username, String password, boolean valid, String assertReturnPage )
{
if ( isElementVisible( "login-link-a" ) )//isElementPresent( "loginLink" ) )
{
goToLoginPage();
submitLoginPage( username, password, false, valid, assertReturnPage );
}
if ( valid )
{
assertUserLoggedIn( username );
}
}
public void submitLoginPage( String username, String password )
{
submitLoginPage( username, password, false, true, "Login Page" );
}
public void submitLoginPage( String username, String password, boolean validUsernamePassword )
{
submitLoginPage( username, password, false, validUsernamePassword, "Login Page" );
}
public void submitLoginPage( String username, String password, boolean rememberMe, boolean validUsernamePassword,
String assertReturnPage )
{
clickLinkWithLocator( "login-link-a", false );
setFieldValue( "user-login-form-username", username );
setFieldValue( "user-login-form-password", password );
/*
if ( rememberMe )
{
checkField( "rememberMe" );
}*/
clickButtonWithLocator( "modal-login-ok" );
if ( validUsernamePassword )
{
assertUserLoggedIn( username );
}
/*
else
{
if ( "Login Page".equals( assertReturnPage ) )
{
assertLoginPage();
}
else
{
assertPage( assertReturnPage );
}
}*/
}
protected void assertUserLoggedIn( String username )
{
Assert.assertFalse( isElementVisible( "login-link" ) );
Assert.assertTrue( isElementVisible( "logout-link" ) );
Assert.assertFalse( isElementVisible( "register-link" ) );
Assert.assertFalse( isElementVisible( "create-admin-link" ) );
}
// User Roles // User Roles
public void assertUserRoleCheckBoxPresent( String value ) public void assertUserRoleCheckBoxPresent( String value )
{ {

View File

@ -22,14 +22,11 @@ package org.apache.archiva.web.test.parent;
import com.thoughtworks.selenium.DefaultSelenium; import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium; import com.thoughtworks.selenium.Selenium;
import org.apache.archiva.web.test.tools.AfterSeleniumFailure; import org.apache.archiva.web.test.tools.AfterSeleniumFailure;
import org.apache.commons.io.IOUtils; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
@ -52,9 +49,7 @@ public abstract class AbstractSeleniumTest
private static ThreadLocal<Selenium> selenium = new ThreadLocal<Selenium>(); private static ThreadLocal<Selenium> selenium = new ThreadLocal<Selenium>();
public static Properties p; public Properties p;
private final static String PROPERTIES_SEPARATOR = "=";
public void open() public void open()
throws Exception throws Exception
@ -63,6 +58,21 @@ public abstract class AbstractSeleniumTest
p.load( this.getClass().getClassLoader().getResourceAsStream( "test.properties" ) ); p.load( this.getClass().getClassLoader().getResourceAsStream( "test.properties" ) );
} }
/**
* Close selenium session.
*/
@After
public void close()
throws Exception
{
if ( getSelenium() != null )
{
getSelenium().stop();
selenium.set( null );
}
}
/** /**
* Initialize selenium * Initialize selenium
*/ */
@ -91,6 +101,49 @@ public abstract class AbstractSeleniumTest
} }
} }
public void assertAdminCreated()
throws Exception
{
initializeArchiva( System.getProperty( "baseUrl" ), System.getProperty( "browser" ),
Integer.getInteger( "maxWaitTimeInMs" ), System.getProperty( "seleniumHost", "localhost" ),
Integer.getInteger( "seleniumPort", 4444 ) );
}
public void initializeArchiva( String baseUrl, String browser, int maxWaitTimeInMs, String seleniumHost,
int seleniumPort )
throws Exception
{
open( baseUrl, browser, seleniumHost, seleniumPort, Integer.toString( maxWaitTimeInMs ) );
getSelenium().open( baseUrl );
waitPage();
// if not admin user created create one
if ( isElementVisible( "create-admin-link" ) )
{
Assert.assertFalse( getSelenium().isVisible( "login-link-a" ) );
Assert.assertFalse( getSelenium().isVisible( "register-link-a" ) );
clickLinkWithLocator( "create-admin-link-a", false );
assertCreateAdmin();
String fullname = getProperty( "ADMIN_FULLNAME" );
String username = getAdminUsername();
String mail = getProperty( "ADMIN_EMAIL" );
String password = getProperty( "ADMIN_PASSWORD" );
submitAdminData( fullname, mail, password );
assertUserLoggedIn( username );
clickLinkWithLocator( "logout-link-a" );
}
else
{
Assert.assertTrue( getSelenium().isVisible( "login-link-a" ) );
Assert.assertTrue( getSelenium().isVisible( "register-link-a" ) );
login( getAdminUsername(), getAdminPassword() );
}
}
public static Selenium getSelenium() public static Selenium getSelenium()
{ {
return selenium == null ? null : selenium.get(); return selenium == null ? null : selenium.get();
@ -101,48 +154,139 @@ public abstract class AbstractSeleniumTest
return p.getProperty( key ); return p.getProperty( key );
} }
protected String getEscapeProperty( String key ) public String getAdminUsername()
{ {
InputStream input = this.getClass().getClassLoader().getResourceAsStream( "test.properties" ); String adminUsername = getProperty( "ADMIN_USERNAME" );
String value = null; return adminUsername;
List<String> lines;
try
{
lines = IOUtils.readLines( input );
}
catch ( IOException e )
{
lines = new ArrayList<String>();
}
for ( String l : lines )
{
if ( l != null && l.startsWith( key ) )
{
int indexSeparator = l.indexOf( PROPERTIES_SEPARATOR );
value = l.substring( indexSeparator + 1 ).trim();
break;
}
}
return value;
} }
/** public String getAdminPassword()
* Close selenium session.
*/
public void close()
throws Exception
{ {
if ( getSelenium() != null ) String adminPassword = getProperty( "ADMIN_PASSWORD" );
return adminPassword;
}
public void submitAdminData( String fullname, String email, String password )
{
setFieldValue( "fullname", fullname );
setFieldValue( "email", email );
setFieldValue( "password", password );
setFieldValue( "confirmPassword", password );
clickButtonWithLocator( "user-create-form-register-button" );
}
public void login( String username, String password )
{
login( username, password, true, "Login Page" );
}
public void login( String username, String password, boolean valid, String assertReturnPage )
{
if ( isElementVisible( "login-link-a" ) )//isElementPresent( "loginLink" ) )
{ {
getSelenium().stop(); goToLoginPage();
selenium.set( null );
submitLoginPage( username, password, false, valid, assertReturnPage );
} }
if ( valid )
{
assertUserLoggedIn( username );
}
}
// Go to Login Page
public void goToLoginPage()
{
getSelenium().open( baseUrl );
waitPage();
// are we already logged in ?
if ( isElementVisible( "logout-link" ) ) //isElementPresent( "logoutLink" ) )
{
// so logout
clickLinkWithLocator( "logout-link-a", false );
clickLinkWithLocator( "login-link-a" );
}
else if ( isElementVisible( "login-link-a" ) )
{
clickLinkWithLocator( "login-link-a" );
}
assertLoginModal();
}
public void assertLoginModal()
{
assertElementPresent( "user-login-form" );
Assert.assertTrue( isElementVisible( "register-link" ) );
assertElementPresent( "user-login-form-username" );
assertElementPresent( "user-login-form-password" );
assertButtonWithIdPresent( "modal-login-ok" );
}
public void submitLoginPage( String username, String password )
{
submitLoginPage( username, password, false, true, "Login Page" );
}
public void submitLoginPage( String username, String password, boolean validUsernamePassword )
{
submitLoginPage( username, password, false, validUsernamePassword, "Login Page" );
}
public void submitLoginPage( String username, String password, boolean rememberMe, boolean validUsernamePassword,
String assertReturnPage )
{
clickLinkWithLocator( "login-link-a", false );
setFieldValue( "user-login-form-username", username );
setFieldValue( "user-login-form-password", password );
/*
if ( rememberMe )
{
checkField( "rememberMe" );
}*/
clickButtonWithLocator( "modal-login-ok" );
if ( validUsernamePassword )
{
assertUserLoggedIn( username );
}
/*
else
{
if ( "Login Page".equals( assertReturnPage ) )
{
assertLoginPage();
}
else
{
assertPage( assertReturnPage );
}
}*/
} }
// ******************************************************* // *******************************************************
// Auxiliar methods. This method help us and simplify test. // Auxiliar methods. This method help us and simplify test.
// ******************************************************* // *******************************************************
protected void assertUserLoggedIn( String username )
{
Assert.assertFalse( isElementVisible( "login-link" ) );
Assert.assertTrue( isElementVisible( "logout-link" ) );
Assert.assertFalse( isElementVisible( "register-link" ) );
Assert.assertFalse( isElementVisible( "create-admin-link" ) );
}
public void assertCreateAdmin()
{
assertElementPresent( "user-create" );
assertFieldValue( "admin", "username" );
assertElementPresent( "fullname" );
assertElementPresent( "password" );
assertElementPresent( "confirmPassword" );
assertElementPresent( "email" );
}
public void assertFieldValue( String fieldValue, String fieldName ) public void assertFieldValue( String fieldValue, String fieldName )
{ {
assertElementPresent( fieldName ); assertElementPresent( fieldName );