1 package org.apache.maven.archiva.web.test;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.thoughtworks.selenium.DefaultSelenium;
23 import com.thoughtworks.selenium.Selenium;
24 import junit.framework.TestCase;
25 import org.codehaus.plexus.util.StringUtils;
28 import java.util.Calendar;
29 import java.util.Iterator;
31 import java.util.Properties;
34 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
37 public abstract class AbstractSeleniumTestCase
40 public static final String CHECKBOX_CHECK = "on";
42 public static final String CHECKBOX_UNCHECK = "off";
46 protected String adminUsername;
48 protected String adminPassword;
50 protected String adminFullName = getApplicationName() + " Admin";
52 protected String adminEmail = "admin@localhost.localdomain";
54 protected String maxWaitTimeInMs;
56 protected String baseUrl;
63 Properties p = new Properties();
64 p.load ( this.getClass().getClassLoader().getResourceAsStream( "it.properties" ) );
66 baseUrl = p.getProperty( "BASE_URL" );
67 maxWaitTimeInMs = p.getProperty( "MAX_WAIT_TIME_IN_MS" );
68 adminUsername = p.getProperty( "ADMIN_USERNAME" );
69 adminPassword = p.getProperty( "ADMIN_PASSWORD" );
70 String seleniumHost = p.getProperty( "SELENIUM_HOST" );
71 int seleniumPort = Integer.parseInt( (p.getProperty( "SELENIUM_PORT" ) ) );
73 String browser = System.getProperty( "browser" );
74 if ( StringUtils.isEmpty( browser ) )
76 browser = p.getProperty( "SELENIUM_BROWSER" );
79 sel = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
84 public void tearDown()
90 public Selenium getSelenium()
95 public abstract String getBaseUrl();
98 * We create an admin user if it doesn't exist
100 protected void initialize()
102 open( getWebContext() );
104 if ( getTitle().endsWith( "Create Admin User" ) )
106 assertCreateAdminUserPage();
107 submitCreateAdminUserPage( adminFullName, adminEmail, adminPassword, adminPassword );
109 submitLoginPage( adminUsername, adminPassword );
110 postAdminUserCreation();
116 * where webapp initial configurations can be done
118 protected void postAdminUserCreation()
120 if ( getTitle().endsWith( "Continuum - Configuration" ) )
122 setFieldValue("baseUrl", baseUrl);
123 clickButtonWithValue( "Save" );
127 protected abstract String getApplicationName();
132 * @return the page prefix set by the webapp
134 protected String getTitlePrefix()
139 protected abstract String getInceptionYear();
141 protected String getWebContext()
146 public void open( String url )
151 public String getTitle()
153 return sel.getTitle();
156 public String getHtmlContent()
158 return getSelenium().getHtmlSource();
161 public void assertTextPresent( String text )
163 assertTrue( "'" + text + "' isn't present.", sel.isTextPresent( text ) );
166 public void assertTextNotPresent( String text )
168 assertFalse( "'" + text + "' is present.", sel.isTextPresent( text ) );
171 public void assertElementPresent( String elementLocator )
173 assertTrue( "'" + elementLocator + "' isn't present.", isElementPresent( elementLocator ) );
176 public void assertElementNotPresent( String elementLocator )
178 assertFalse( "'" + elementLocator + "' is present.", isElementPresent( elementLocator ) );
181 public void assertLinkPresent( String text )
183 assertTrue( "The link '" + text + "' isn't present.", isElementPresent( "link=" + text ) );
186 public void assertLinkNotPresent( String text )
188 assertFalse( "The link '" + text + "' is present.", isElementPresent( "link=" + text ) );
191 public void assertImgWithAlt( String alt )
193 assertElementPresent( "//img[@alt='" + alt + "']" );
196 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
198 String locator = "//tr[" + row + "]/td[" + column + "]/";
199 locator += isALink ? "a/" : "";
200 locator += "img[@alt='" + alt + "']";
202 assertElementPresent( locator );
205 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
207 assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
210 public boolean isTextPresent( String text )
212 return sel.isTextPresent( text );
215 public boolean isLinkPresent( String text )
217 return isElementPresent( "link=" + text );
220 public boolean isElementPresent( String locator )
222 return sel.isElementPresent( locator );
225 public void waitPage()
230 public void waitPage( int nbMillisecond )
232 sel.waitForPageToLoad( String.valueOf( nbMillisecond ) );
235 public void assertPage( String title )
237 assertEquals( getTitlePrefix() + title, getTitle() );
242 public abstract void assertHeader();
245 public void assertFooter()
247 int currentYear = Calendar.getInstance().get( Calendar.YEAR );
248 assertTrue( getSelenium().getText( "xpath=//div[@id='footer']/div[1]" ).endsWith(
249 "Copyright © " + getInceptionYear() + "-" + currentYear + " The Apache Software Foundation" ) );
252 public String getFieldValue( String fieldName )
254 return sel.getValue( fieldName );
257 public String getCellValueFromTable( String tableElement, int row, int column )
259 return getSelenium().getTable( tableElement + "." + row + "." + column );
262 public void selectValue( String locator, String value )
264 getSelenium().select( locator, "label=" + value );
269 clickLinkWithXPath( "//input[@type='submit']" );
272 public void assertButtonWithValuePresent( String text )
274 assertTrue( "'" + text + "' button isn't present", isButtonWithValuePresent( text ) );
277 public void assertButtonWithValueNotPresent( String text )
279 assertFalse( "'" + text + "' button is present", isButtonWithValuePresent( text ) );
282 public boolean isButtonWithValuePresent( String text )
284 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent( "//input[@value='" + text + "']" );
287 public void clickButtonWithValue( String text )
289 clickButtonWithValue( text, true );
292 public void clickButtonWithValue( String text, boolean wait )
294 assertButtonWithValuePresent( text );
296 if ( isElementPresent( "//button[@value='" + text + "']" ) )
298 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
302 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
306 public void clickSubmitWithLocator( String locator )
308 clickLinkWithLocator( locator );
311 public void clickSubmitWithLocator( String locator, boolean wait )
313 clickLinkWithLocator( locator, wait );
316 public void clickImgWithAlt( String alt )
318 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
321 public void clickLinkWithText( String text )
323 clickLinkWithText( text, true );
326 public void clickLinkWithText( String text, boolean wait )
328 clickLinkWithLocator( "link=" + text, wait );
331 public void clickLinkWithXPath( String xpath )
333 clickLinkWithXPath( xpath, true );
336 public void clickLinkWithXPath( String xpath, boolean wait )
338 clickLinkWithLocator( "xpath=" + xpath, wait );
341 public void clickLinkWithLocator( String locator )
343 clickLinkWithLocator( locator, true );
346 public void clickLinkWithLocator( String locator, boolean wait )
348 assertElementPresent( locator );
349 sel.click( locator );
356 public void setFieldValues( Map fieldMap )
360 for ( Iterator entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
362 entry = (Map.Entry) entries.next();
364 sel.type( (String) entry.getKey(), (String) entry.getValue() );
368 public void setFieldValue( String fieldName, String value )
370 sel.type( fieldName, value );
373 public void checkField( String locator )
375 sel.check( locator );
378 public void uncheckField( String locator )
380 sel.uncheck( locator );
383 public boolean isChecked( String locator )
385 return sel.isChecked( locator );
388 //////////////////////////////////////
390 //////////////////////////////////////
391 public void goToLoginPage()
393 clickLinkWithText( "Login" );
398 public void login( String username, String password )
400 login( username, password, true, "Login Page" );
403 public void login( String username, String password, boolean valid, String assertReturnPage )
405 if ( isLinkPresent( "Login" ) )
409 submitLoginPage( username, password, false, valid, assertReturnPage );
413 public void assertLoginPage()
415 assertPage( "Login Page" );
416 assertTextPresent( "Login" );
417 assertTextPresent( "Username" );
418 assertTextPresent( "Password" );
419 assertTextPresent( "Remember Me" );
420 assertFalse( isChecked( "rememberMe" ) );
423 public void submitLoginPage( String username, String password )
425 submitLoginPage( username, password, false, true, "Login Page" );
428 public void submitLoginPage( String username, String password, boolean validUsernamePassword )
430 submitLoginPage( username, password, false, validUsernamePassword, "Login Page" );
433 public void submitLoginPage( String username, String password, boolean rememberMe, boolean validUsernamePassword,
434 String assertReturnPage )
437 setFieldValue( "username", username );
438 setFieldValue( "password", password );
441 checkField( "rememberMe" );
443 clickButtonWithValue( "Login" );
445 if ( validUsernamePassword )
447 //assertTextPresent( "Current User:" );
448 assertTextPresent( username );
449 assertLinkPresent( "Edit Details" );
450 assertLinkPresent( "Logout" );
454 if ( "Login Page".equals( assertReturnPage ) )
460 assertPage( assertReturnPage );
465 public boolean isAuthenticated()
467 return !( isLinkPresent( "Login" ) && isLinkPresent( "Register" ) );
470 //////////////////////////////////////
472 //////////////////////////////////////
475 assertTrue( "User wasn't authenticated.", isAuthenticated() );
476 clickLinkWithText( "Logout" );
477 assertFalse( "The user is always authenticated after a logout.", isAuthenticated() );
480 //////////////////////////////////////
482 //////////////////////////////////////
483 public void goToMyAccount()
485 clickLinkWithText( "Edit Details" );
488 public void assertMyAccountDetails( String username, String newFullName, String newEmailAddress )
491 assertPage( "Account Details" );
493 //isTextPresent( "Username" );
494 assertTextPresent( "Username:" );
495 assertElementPresent( "registerForm_user_username" );
496 assertCellValueFromTable( username, "//form/table", 0, 1 );
498 assertTextPresent( "Full Name*:" );
499 assertElementPresent( "user.fullName" );
500 assertEquals( newFullName, getFieldValue( "user.fullName" ) );
502 assertTextPresent( "Email Address*:" );
503 assertElementPresent( "user.email" );
504 assertEquals( newEmailAddress, getFieldValue( "user.email" ) );
506 assertTextPresent("Current Password*:");
507 assertElementPresent("oldPassword");
509 assertTextPresent( "New Password*:" );
510 assertElementPresent( "user.password" );
512 assertTextPresent( "Confirm Password*:" );
513 assertElementPresent( "user.confirmPassword" );
515 assertTextPresent( "Last Password Change" );
516 assertElementPresent( "registerForm_user_timestampLastPasswordChange" );
520 public void editMyUserInfo( String newFullName, String newEmailAddress, String oldPassword, String newPassword,
521 String confirmNewPassword )
525 setFieldValue( "user.fullName", newFullName );
526 setFieldValue( "user.email", newEmailAddress );
527 setFieldValue( "oldPassword", oldPassword );
528 setFieldValue( "user.password", newPassword );
529 setFieldValue( "user.confirmPassword", confirmNewPassword );
530 clickButtonWithValue( "Submit" );
533 //////////////////////////////////////
535 //////////////////////////////////////
536 public void assertUsersListPage()
538 assertPage( "[Admin] User List" );
541 public void assertCreateUserPage()
543 assertPage( "[Admin] User Create" );
544 assertTextPresent( "Username" );
545 assertTextPresent( "Full Name" );
546 assertTextPresent( "Email Address" );
547 assertTextPresent( "Password" );
548 assertTextPresent( "Confirm Password" );
551 public void assertUserRolesPage()
553 assertPage( "[Admin] User Edit" );
554 assertTextPresent( "[Admin] User Roles" );
555 assertTextPresent( "Assigned Roles" );
556 assertTextPresent( "Available Roles" );
559 public void assertDeleteUserPage( String username )
561 assertPage( "[Admin] User Delete" );
562 assertTextPresent( "[Admin] User Delete" );
563 assertTextPresent( "The following user will be deleted: " + username );
564 assertButtonWithValuePresent( "Delete User" );
567 //////////////////////////////////////
569 //////////////////////////////////////
570 public void assertCreateAdminUserPage()
572 assertPage( "Create Admin User" );
573 assertTextPresent( "Create Admin User" );
574 assertTextPresent( "Username" );
575 assertElementPresent( "user.username" );
576 assertTextPresent( "Full Name" );
577 assertElementPresent( "user.fullName" );
578 assertTextPresent( "Email Address" );
579 assertElementPresent( "user.email" );
580 assertTextPresent( "Password" );
581 assertElementPresent( "user.password" );
582 assertTextPresent( "Confirm Password" );
583 assertElementPresent( "user.confirmPassword" );
586 public void submitCreateAdminUserPage( String fullName, String email, String password, String confirmPassword )
588 setFieldValue( "user.fullName", fullName );
589 setFieldValue( "user.email", email );
590 setFieldValue( "user.password", password );
591 setFieldValue( "user.confirmPassword", confirmPassword );
596 public String getBasedir()
598 String basedir = System.getProperty( "basedir" );
600 if ( basedir == null )
602 basedir = new File( "" ).getAbsolutePath();