1 package org.apache.archiva.web.test.parent;
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 org.apache.archiva.web.test.tools.AfterSeleniumFailure;
25 import org.junit.After;
26 import org.junit.Assert;
27 import org.junit.Before;
30 import java.text.SimpleDateFormat;
31 import java.util.Arrays;
32 import java.util.Date;
33 import java.util.Iterator;
34 import java.util.List;
36 import java.util.Map.Entry;
37 import java.util.Properties;
40 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
41 * @version $Id: AbstractSeleniumTestCase.java 761154 2009-04-02 03:31:19Z wsmoak $
44 public abstract class AbstractSeleniumTest
46 public String browser = System.getProperty( "browser" );
48 public String baseUrl = System.getProperty( "baseUrl" );
50 public int maxWaitTimeInMs = Integer.getInteger( "maxWaitTimeInMs" );
52 public String seleniumHost = System.getProperty( "seleniumHost", "localhost" );
54 public int seleniumPort = Integer.getInteger( "seleniumPort", 4444 );
56 private Selenium selenium = null;
65 p.load( this.getClass().getClassLoader().getResourceAsStream( "test.properties" ) );
66 open( baseUrl, browser, seleniumHost, seleniumPort, maxWaitTimeInMs );
71 * Close selenium session.
77 if ( getSelenium() != null )
86 public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort, int maxWaitTimeInMs )
91 if ( getSelenium() == null )
93 selenium = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
95 selenium.setTimeout( Integer.toString( maxWaitTimeInMs ) );
101 System.out.print( e.getMessage() );
106 public void assertAdminCreated()
109 initializeArchiva( baseUrl, browser, maxWaitTimeInMs, seleniumHost, seleniumPort );
112 public void initializeArchiva( String baseUrl, String browser, int maxWaitTimeInMs, String seleniumHost,
117 open( baseUrl, browser, seleniumHost, seleniumPort, maxWaitTimeInMs );
119 getSelenium().open( baseUrl );
123 // if not admin user created create one
124 if ( isElementVisible( "create-admin-link" ) )
126 Assert.assertFalse( getSelenium().isVisible( "login-link-a" ) );
127 Assert.assertFalse( getSelenium().isVisible( "register-link-a" ) );
128 clickLinkWithLocator( "create-admin-link-a", false );
130 String fullname = getProperty( "ADMIN_FULLNAME" );
131 String username = getAdminUsername();
132 String mail = getProperty( "ADMIN_EMAIL" );
133 String password = getProperty( "ADMIN_PASSWORD" );
134 submitAdminData( fullname, mail, password );
135 assertUserLoggedIn( username );
136 clickLinkWithLocator( "logout-link-a" );
140 Assert.assertTrue( getSelenium().isVisible( "login-link-a" ) );
141 Assert.assertTrue( getSelenium().isVisible( "register-link-a" ) );
142 login( getAdminUsername(), getAdminPassword() );
147 public Selenium getSelenium()
152 protected String getProperty( String key )
154 return p.getProperty( key );
157 public String getAdminUsername()
159 String adminUsername = getProperty( "ADMIN_USERNAME" );
160 return adminUsername;
163 public String getAdminPassword()
165 String adminPassword = getProperty( "ADMIN_PASSWORD" );
166 return adminPassword;
169 public void submitAdminData( String fullname, String email, String password )
171 setFieldValue( "fullname", fullname );
172 setFieldValue( "email", email );
173 setFieldValue( "password", password );
174 setFieldValue( "confirmPassword", password );
175 clickButtonWithLocator( "user-create-form-register-button" );
178 public void login( String username, String password )
180 login( username, password, true, "Login Page" );
183 public void login( String username, String password, boolean valid, String assertReturnPage )
185 if ( isElementVisible( "login-link-a" ) )//isElementPresent( "loginLink" ) )
189 submitLoginPage( username, password, false, valid, assertReturnPage );
193 assertUserLoggedIn( username );
198 public void goToLoginPage()
200 getSelenium().open( baseUrl );
202 // are we already logged in ?
203 if ( isElementVisible( "logout-link" ) ) //isElementPresent( "logoutLink" ) )
206 clickLinkWithLocator( "logout-link-a", false );
207 clickLinkWithLocator( "login-link-a" );
209 else if ( isElementVisible( "login-link-a" ) )
211 clickLinkWithLocator( "login-link-a" );
217 public void assertLoginModal()
219 assertElementPresent( "user-login-form" );
220 Assert.assertTrue( isElementVisible( "register-link" ) );
221 assertElementPresent( "user-login-form-username" );
222 assertElementPresent( "user-login-form-password" );
223 assertButtonWithIdPresent( "modal-login-ok" );
227 public void submitLoginPage( String username, String password )
229 submitLoginPage( username, password, false, true, "Login Page" );
232 public void submitLoginPage( String username, String password, boolean validUsernamePassword )
234 submitLoginPage( username, password, false, validUsernamePassword, "Login Page" );
237 public void submitLoginPage( String username, String password, boolean rememberMe, boolean validUsernamePassword,
238 String assertReturnPage )
240 clickLinkWithLocator( "login-link-a", false );
241 setFieldValue( "user-login-form-username", username );
242 setFieldValue( "user-login-form-password", password );
246 checkField( "rememberMe" );
249 clickButtonWithLocator( "modal-login-ok" );
250 if ( validUsernamePassword )
252 assertUserLoggedIn( username );
257 if ( "Login Page".equals( assertReturnPage ) )
263 assertPage( assertReturnPage );
268 // *******************************************************
269 // Auxiliar methods. This method help us and simplify test.
270 // *******************************************************
272 protected void assertUserLoggedIn( String username )
274 Assert.assertFalse( isElementVisible( "login-link" ) );
275 Assert.assertTrue( isElementVisible( "logout-link" ) );
276 Assert.assertFalse( isElementVisible( "register-link" ) );
277 Assert.assertFalse( isElementVisible( "create-admin-link" ) );
280 public void assertCreateAdmin()
282 assertElementPresent( "user-create" );
283 assertFieldValue( "admin", "username" );
284 assertElementPresent( "fullname" );
285 assertElementPresent( "password" );
286 assertElementPresent( "confirmPassword" );
287 assertElementPresent( "email" );
290 public void assertFieldValue( String fieldValue, String fieldName )
292 assertElementPresent( fieldName );
293 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
296 public void assertPage( String title )
298 Assert.assertEquals( getTitle(), title );
301 public String getTitle()
304 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
307 public String getHtmlContent()
309 return getSelenium().getHtmlSource();
312 public String getText( String locator )
314 return getSelenium().getText( locator );
317 public void assertTextPresent( String text )
319 Assert.assertTrue( "'" + text + "' isn't present.", getSelenium().isTextPresent( text ) );
323 * one of text args must be in the page so use en and fr text (olamy use en locale :-) )
327 public void assertTextPresent( String... texts )
329 boolean present = false;
330 StringBuilder sb = new StringBuilder();
331 for ( String text : texts )
333 present = present || getSelenium().isTextPresent( text );
334 sb.append( " " + text + " " );
336 Assert.assertTrue( "'one of the following test " + sb.toString() + "' isn't present.", present );
339 public void assertTextNotPresent( String text )
341 Assert.assertFalse( "'" + text + "' is present.", getSelenium().isTextPresent( text ) );
344 public void assertElementPresent( String elementLocator )
346 Assert.assertTrue( "'" + elementLocator + "' isn't present.", isElementPresent( elementLocator ) );
349 public void assertElementNotPresent( String elementLocator )
351 Assert.assertFalse( "'" + elementLocator + "' is present.", isElementPresent( elementLocator ) );
354 public void assertLinkPresent( String text )
356 Assert.assertTrue( "The link '" + text + "' isn't present.", isElementPresent( "link=" + text ) );
359 public void assertLinkNotPresent( String text )
361 Assert.assertFalse( "The link('" + text + "' is present.", isElementPresent( "link=" + text ) );
364 public void assertLinkNotVisible( String text )
366 Assert.assertFalse( "The link('" + text + "' is visible.", isElementVisible( "link=" + text ) );
369 public void assertLinkVisible( String text )
371 Assert.assertTrue( "The link('" + text + "' is not visible.", isElementVisible( "link=" + text ) );
374 public void assertImgWithAlt( String alt )
376 assertElementPresent( "/¯img[@alt='" + alt + "']" );
379 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
381 String locator = "//tr[" + row + "]/td[" + column + "]/";
382 locator += isALink ? "a/" : "";
383 locator += "img[@alt='" + alt + "']";
385 assertElementPresent( locator );
388 public void assertImgWithAltNotPresent( String alt )
390 assertElementNotPresent( "/¯img[@alt='" + alt + "']" );
393 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
395 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
398 public boolean isTextPresent( String text )
400 return getSelenium().isTextPresent( text );
403 public boolean isLinkPresent( String text )
405 return isElementPresent( "link=" + text );
408 public boolean isElementPresent( String locator )
410 return getSelenium().isElementPresent( locator );
413 public boolean isElementVisible( String locator )
415 return getSelenium().isVisible( locator );
419 public void waitPage()
421 // TODO define a smaller maxWaitTimeJsInMs for wait javascript response for browser side validation
422 //getSelenium().w .wait( Long.parseLong( maxWaitTimeInMs ) );
423 //getSelenium().waitForPageToLoad( maxWaitTimeInMs );
424 // http://jira.openqa.org/browse/SRC-302
425 // those hack looks to break some tests :-(
426 // getSelenium().waitForCondition( "selenium.isElementPresent('document.body');", maxWaitTimeInMs );
427 //getSelenium().waitForCondition( "selenium.isElementPresent('footer');", maxWaitTimeInMs );
428 //getSelenium().waitForCondition( "selenium.browserbot.getCurrentWindow().document.getElementById('footer')",
429 // maxWaitTimeInMs );
430 // so the only hack is to not use a too small wait time
434 Thread.sleep( maxWaitTimeInMs );
436 catch ( InterruptedException e )
438 throw new RuntimeException( "issue on Thread.sleep : " + e.getMessage(), e );
442 public String getFieldValue( String fieldName )
444 return getSelenium().getValue( fieldName );
447 public String getCellValueFromTable( String tableElement, int row, int column )
449 return getSelenium().getTable( tableElement + "." + row + "." + column );
452 public void selectValue( String locator, String value )
454 getSelenium().select( locator, "label=" + value );
458 public void assertOptionPresent( String selectField, String[] options )
460 assertElementPresent( selectField );
461 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
462 List<String> expected = Arrays.asList( options );
463 List<String> present = Arrays.asList( optionsPresent );
464 Assert.assertTrue( "Options expected are not included in present options", present.containsAll( expected ) );
467 public void assertSelectedValue( String value, String fieldName )
469 assertElementPresent( fieldName );
470 String optionsPresent = getSelenium().getSelectedLabel( value );
471 Assert.assertEquals( optionsPresent, value );
476 clickLinkWithXPath( "//input[@type='submit']" );
479 public void assertButtonWithValuePresent( String text )
481 Assert.assertTrue( "'" + text + "' button isn't present", isButtonWithValuePresent( text ) );
484 public void assertButtonWithIdPresent( String id )
486 Assert.assertTrue( "'Button with id =" + id + "' isn't present", isButtonWithIdPresent( id ) );
489 public void assertButtonWithValueNotPresent( String text )
491 Assert.assertFalse( "'" + text + "' button is present", isButtonWithValuePresent( text ) );
494 public boolean isButtonWithValuePresent( String text )
496 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent(
497 "//input[@value='" + text + "']" );
500 public boolean isButtonWithIdPresent( String text )
502 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
505 public void clickButtonWithName( String text, boolean wait )
507 clickLinkWithXPath( "//input[@name='" + text + "']", wait );
510 public void clickButtonWithValue( String text )
512 clickButtonWithValue( text, true );
515 public void clickButtonWithValue( String text, boolean wait )
517 assertButtonWithValuePresent( text );
519 if ( isElementPresent( "//button[@value='" + text + "']" ) )
521 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
525 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
529 public void clickSubmitWithLocator( String locator )
531 clickLinkWithLocator( locator );
534 public void clickSubmitWithLocator( String locator, boolean wait )
536 clickLinkWithLocator( locator, wait );
539 public void clickImgWithAlt( String alt )
541 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
544 public void clickLinkWithText( String text )
546 clickLinkWithText( text, true );
549 public void clickLinkWithText( String text, boolean wait )
551 clickLinkWithLocator( "link=" + text, wait );
554 public void clickLinkWithXPath( String xpath )
556 clickLinkWithXPath( xpath, true );
559 public void clickLinkWithXPath( String xpath, boolean wait )
561 clickLinkWithLocator( "xpath=" + xpath, wait );
564 public void clickLinkWithLocator( String locator )
566 clickLinkWithLocator( locator, true );
569 public void clickLinkWithLocator( String locator, boolean wait )
571 assertElementPresent( locator );
572 getSelenium().click( locator );
579 public void clickButtonWithLocator( String locator )
581 clickButtonWithLocator( locator, true );
584 public void clickButtonWithLocator( String locator, boolean wait )
586 assertElementPresent( locator );
587 getSelenium().click( locator );
594 public void setFieldValues( Map<String, String> fieldMap )
596 Map.Entry<String, String> entry;
598 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
600 entry = entries.next();
602 getSelenium().type( entry.getKey(), entry.getValue() );
606 public void setFieldValue( String fieldName, String value )
608 getSelenium().type( fieldName, value );
611 public void checkField( String locator )
613 getSelenium().check( locator );
616 public void uncheckField( String locator )
618 getSelenium().uncheck( locator );
621 public boolean isChecked( String locator )
623 return getSelenium().isChecked( locator );
626 public void assertIsChecked( String locator )
628 Assert.assertTrue( getSelenium().isChecked( locator ) );
631 public void assertIsNotChecked( String locator )
633 Assert.assertFalse( getSelenium().isChecked( locator ) );
636 public void assertXpathCount( String locator, int expectedCount )
638 int count = getSelenium().getXpathCount( locator ).intValue();
639 Assert.assertEquals( count, expectedCount );
642 public void assertElementValue( String locator, String expectedValue )
644 Assert.assertEquals( getSelenium().getValue( locator ), expectedValue );
647 @AfterSeleniumFailure
648 public void captureScreenShotOnFailure( Throwable failure )
650 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" );
651 String time = sdf.format( new Date() );
652 File targetPath = new File( "target", "screenshots" );
653 String cName = this.getClass().getName();
655 String methodName = "";
658 for ( StackTraceElement stackTrace : failure.getStackTrace() )
660 if ( stackTrace.getClassName().equals( this.getClass().getName() ) )
662 methodName = stackTrace.getMethodName();
663 lineNumber = stackTrace.getLineNumber();
668 String className = cName.substring( cName.lastIndexOf( '.' ) + 1 );
670 Selenium selenium = getSelenium();
671 String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;
673 selenium.windowMaximize();
675 File fileName = new File( targetPath, fileBaseName + ".png" );
676 selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "background=#FFFFFF" );