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.apache.archiva.web.test.tools.ScreenshotCaptureRule;
26 import org.junit.Assert;
27 import org.junit.Rule;
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
48 public ScreenshotCaptureRule screenshotCaptureRule = new ScreenshotCaptureRule();
50 public String browser = System.getProperty( "browser" );
52 public String baseUrl = System.getProperty( "baseUrl" );
54 public int maxWaitTimeInMs = Integer.getInteger( "maxWaitTimeInMs" );
56 public String seleniumHost = System.getProperty( "seleniumHost", "localhost" );
58 public int seleniumPort = Integer.getInteger( "seleniumPort", 4444 );
60 private Selenium selenium = null;
68 p.load( this.getClass().getClassLoader().getResourceAsStream( "test.properties" ) );
69 open( baseUrl, browser, seleniumHost, seleniumPort, maxWaitTimeInMs );
70 screenshotCaptureRule.selenium = selenium;
75 * Close selenium session.
79 if ( getSelenium() != null )
88 public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort, int maxWaitTimeInMs )
93 if ( getSelenium() == null )
95 selenium = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
97 selenium.setTimeout( Integer.toString( maxWaitTimeInMs ) );
100 catch ( Exception e )
103 System.out.print( e.getMessage() );
108 public void assertAdminCreated()
111 initializeArchiva( baseUrl, browser, maxWaitTimeInMs, seleniumHost, seleniumPort );
114 public void initializeArchiva( String baseUrl, String browser, int maxWaitTimeInMs, String seleniumHost,
119 open( baseUrl, browser, seleniumHost, seleniumPort, maxWaitTimeInMs );
121 getSelenium().open( baseUrl );
125 // if not admin user created create one
126 if ( isElementVisible( "create-admin-link" ) )
128 Assert.assertFalse( getSelenium().isVisible( "login-link-a" ) );
129 Assert.assertFalse( getSelenium().isVisible( "register-link-a" ) );
130 clickLinkWithLocator( "create-admin-link-a", false );
132 String fullname = getProperty( "ADMIN_FULLNAME" );
133 String username = getAdminUsername();
134 String mail = getProperty( "ADMIN_EMAIL" );
135 String password = getProperty( "ADMIN_PASSWORD" );
136 submitAdminData( fullname, mail, password );
137 assertUserLoggedIn( username );
138 clickLinkWithLocator( "logout-link-a" );
142 Assert.assertTrue( getSelenium().isVisible( "login-link-a" ) );
143 Assert.assertTrue( getSelenium().isVisible( "register-link-a" ) );
144 login( getAdminUsername(), getAdminPassword() );
149 public Selenium getSelenium()
154 protected String getProperty( String key )
156 return p.getProperty( key );
159 public String getAdminUsername()
161 String adminUsername = getProperty( "ADMIN_USERNAME" );
162 return adminUsername;
165 public String getAdminPassword()
167 String adminPassword = getProperty( "ADMIN_PASSWORD" );
168 return adminPassword;
171 public void submitAdminData( String fullname, String email, String password )
173 setFieldValue( "fullname", fullname );
174 setFieldValue( "email", email );
175 setFieldValue( "password", password );
176 setFieldValue( "confirmPassword", password );
177 clickButtonWithLocator( "user-create-form-register-button" );
180 public void login( String username, String password )
182 login( username, password, true, "Login Page" );
185 public void login( String username, String password, boolean valid, String assertReturnPage )
187 if ( isElementVisible( "login-link-a" ) )//isElementPresent( "loginLink" ) )
191 submitLoginPage( username, password, false, valid, assertReturnPage );
195 assertUserLoggedIn( username );
200 public void goToLoginPage()
202 getSelenium().open( baseUrl );
204 // are we already logged in ?
205 if ( isElementVisible( "logout-link" ) ) //isElementPresent( "logoutLink" ) )
208 clickLinkWithLocator( "logout-link-a", false );
209 clickLinkWithLocator( "login-link-a" );
211 else if ( isElementVisible( "login-link-a" ) )
213 clickLinkWithLocator( "login-link-a" );
219 public void assertLoginModal()
221 assertElementPresent( "user-login-form" );
222 Assert.assertTrue( isElementVisible( "register-link" ) );
223 assertElementPresent( "user-login-form-username" );
224 assertElementPresent( "user-login-form-password" );
225 assertButtonWithIdPresent( "modal-login-ok" );
229 public void submitLoginPage( String username, String password )
231 submitLoginPage( username, password, false, true, "Login Page" );
234 public void submitLoginPage( String username, String password, boolean validUsernamePassword )
236 submitLoginPage( username, password, false, validUsernamePassword, "Login Page" );
239 public void submitLoginPage( String username, String password, boolean rememberMe, boolean validUsernamePassword,
240 String assertReturnPage )
242 clickLinkWithLocator( "login-link-a", false );
243 setFieldValue( "user-login-form-username", username );
244 setFieldValue( "user-login-form-password", password );
248 checkField( "rememberMe" );
251 clickButtonWithLocator( "modal-login-ok" );
252 if ( validUsernamePassword )
254 assertUserLoggedIn( username );
259 if ( "Login Page".equals( assertReturnPage ) )
265 assertPage( assertReturnPage );
270 // *******************************************************
271 // Auxiliar methods. This method help us and simplify test.
272 // *******************************************************
274 protected void assertUserLoggedIn( String username )
276 Assert.assertFalse( isElementVisible( "login-link" ) );
277 Assert.assertTrue( isElementVisible( "logout-link" ) );
278 Assert.assertFalse( isElementVisible( "register-link" ) );
279 Assert.assertFalse( isElementVisible( "create-admin-link" ) );
282 public void assertCreateAdmin()
284 assertElementPresent( "user-create" );
285 assertFieldValue( "admin", "username" );
286 assertElementPresent( "fullname" );
287 assertElementPresent( "password" );
288 assertElementPresent( "confirmPassword" );
289 assertElementPresent( "email" );
292 public void assertFieldValue( String fieldValue, String fieldName )
294 assertElementPresent( fieldName );
295 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
298 public void assertPage( String title )
300 Assert.assertEquals( getTitle(), title );
303 public String getTitle()
306 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
309 public String getHtmlContent()
311 return getSelenium().getHtmlSource();
314 public String getText( String locator )
316 return getSelenium().getText( locator );
319 public void assertTextPresent( String text )
321 Assert.assertTrue( "'" + text + "' isn't present.", getSelenium().isTextPresent( text ) );
325 * one of text args must be in the page so use en and fr text (olamy use en locale :-) )
329 public void assertTextPresent( String... texts )
331 boolean present = false;
332 StringBuilder sb = new StringBuilder();
333 for ( String text : texts )
335 present = present || getSelenium().isTextPresent( text );
336 sb.append( " " + text + " " );
338 Assert.assertTrue( "'one of the following test " + sb.toString() + "' isn't present.", present );
341 public void assertTextNotPresent( String text )
343 Assert.assertFalse( "'" + text + "' is present.", getSelenium().isTextPresent( text ) );
346 public void assertElementPresent( String elementLocator )
348 Assert.assertTrue( "'" + elementLocator + "' isn't present.", isElementPresent( elementLocator ) );
351 public void assertElementNotPresent( String elementLocator )
353 Assert.assertFalse( "'" + elementLocator + "' is present.", isElementPresent( elementLocator ) );
356 public void assertLinkPresent( String text )
358 Assert.assertTrue( "The link '" + text + "' isn't present.", isElementPresent( "link=" + text ) );
361 public void assertLinkNotPresent( String text )
363 Assert.assertFalse( "The link('" + text + "' is present.", isElementPresent( "link=" + text ) );
366 public void assertLinkNotVisible( String text )
368 Assert.assertFalse( "The link('" + text + "' is visible.", isElementVisible( "link=" + text ) );
371 public void assertLinkVisible( String text )
373 Assert.assertTrue( "The link('" + text + "' is not visible.", isElementVisible( "link=" + text ) );
376 public void assertImgWithAlt( String alt )
378 assertElementPresent( "/¯img[@alt='" + alt + "']" );
381 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
383 String locator = "//tr[" + row + "]/td[" + column + "]/";
384 locator += isALink ? "a/" : "";
385 locator += "img[@alt='" + alt + "']";
387 assertElementPresent( locator );
390 public void assertImgWithAltNotPresent( String alt )
392 assertElementNotPresent( "/¯img[@alt='" + alt + "']" );
395 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
397 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
400 public boolean isTextPresent( String text )
402 return getSelenium().isTextPresent( text );
405 public boolean isLinkPresent( String text )
407 return isElementPresent( "link=" + text );
410 public boolean isElementPresent( String locator )
412 return getSelenium().isElementPresent( locator );
415 public boolean isElementVisible( String locator )
417 return getSelenium().isVisible( locator );
421 public void waitPage()
423 // TODO define a smaller maxWaitTimeJsInMs for wait javascript response for browser side validation
424 //getSelenium().w .wait( Long.parseLong( maxWaitTimeInMs ) );
425 //getSelenium().waitForPageToLoad( maxWaitTimeInMs );
426 // http://jira.openqa.org/browse/SRC-302
427 // those hack looks to break some tests :-(
428 // getSelenium().waitForCondition( "selenium.isElementPresent('document.body');", maxWaitTimeInMs );
429 //getSelenium().waitForCondition( "selenium.isElementPresent('footer');", maxWaitTimeInMs );
430 //getSelenium().waitForCondition( "selenium.browserbot.getCurrentWindow().document.getElementById('footer')",
431 // maxWaitTimeInMs );
432 // so the only hack is to not use a too small wait time
436 Thread.sleep( maxWaitTimeInMs );
438 catch ( InterruptedException e )
440 throw new RuntimeException( "issue on Thread.sleep : " + e.getMessage(), e );
444 public String getFieldValue( String fieldName )
446 return getSelenium().getValue( fieldName );
449 public String getCellValueFromTable( String tableElement, int row, int column )
451 return getSelenium().getTable( tableElement + "." + row + "." + column );
454 public void selectValue( String locator, String value )
456 getSelenium().select( locator, "label=" + value );
460 public void assertOptionPresent( String selectField, String[] options )
462 assertElementPresent( selectField );
463 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
464 List<String> expected = Arrays.asList( options );
465 List<String> present = Arrays.asList( optionsPresent );
466 Assert.assertTrue( "Options expected are not included in present options", present.containsAll( expected ) );
469 public void assertSelectedValue( String value, String fieldName )
471 assertElementPresent( fieldName );
472 String optionsPresent = getSelenium().getSelectedLabel( value );
473 Assert.assertEquals( optionsPresent, value );
478 clickLinkWithXPath( "//input[@type='submit']" );
481 public void assertButtonWithValuePresent( String text )
483 Assert.assertTrue( "'" + text + "' button isn't present", isButtonWithValuePresent( text ) );
486 public void assertButtonWithIdPresent( String id )
488 Assert.assertTrue( "'Button with id =" + id + "' isn't present", isButtonWithIdPresent( id ) );
491 public void assertButtonWithValueNotPresent( String text )
493 Assert.assertFalse( "'" + text + "' button is present", isButtonWithValuePresent( text ) );
496 public boolean isButtonWithValuePresent( String text )
498 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent(
499 "//input[@value='" + text + "']" );
502 public boolean isButtonWithIdPresent( String text )
504 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
507 public void clickButtonWithName( String text, boolean wait )
509 clickLinkWithXPath( "//input[@name='" + text + "']", wait );
512 public void clickButtonWithValue( String text )
514 clickButtonWithValue( text, true );
517 public void clickButtonWithValue( String text, boolean wait )
519 assertButtonWithValuePresent( text );
521 if ( isElementPresent( "//button[@value='" + text + "']" ) )
523 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
527 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
531 public void clickSubmitWithLocator( String locator )
533 clickLinkWithLocator( locator );
536 public void clickSubmitWithLocator( String locator, boolean wait )
538 clickLinkWithLocator( locator, wait );
541 public void clickImgWithAlt( String alt )
543 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
546 public void clickLinkWithText( String text )
548 clickLinkWithText( text, true );
551 public void clickLinkWithText( String text, boolean wait )
553 clickLinkWithLocator( "link=" + text, wait );
556 public void clickLinkWithXPath( String xpath )
558 clickLinkWithXPath( xpath, true );
561 public void clickLinkWithXPath( String xpath, boolean wait )
563 clickLinkWithLocator( "xpath=" + xpath, wait );
566 public void clickLinkWithLocator( String locator )
568 clickLinkWithLocator( locator, true );
571 public void clickLinkWithLocator( String locator, boolean wait )
573 assertElementPresent( locator );
574 getSelenium().click( locator );
581 public void clickButtonWithLocator( String locator )
583 clickButtonWithLocator( locator, true );
586 public void clickButtonWithLocator( String locator, boolean wait )
588 assertElementPresent( locator );
589 getSelenium().click( locator );
596 public void setFieldValues( Map<String, String> fieldMap )
598 Map.Entry<String, String> entry;
600 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
602 entry = entries.next();
604 getSelenium().type( entry.getKey(), entry.getValue() );
608 public void setFieldValue( String fieldName, String value )
610 getSelenium().type( fieldName, value );
613 public void checkField( String locator )
615 getSelenium().check( locator );
618 public void uncheckField( String locator )
620 getSelenium().uncheck( locator );
623 public boolean isChecked( String locator )
625 return getSelenium().isChecked( locator );
628 public void assertIsChecked( String locator )
630 Assert.assertTrue( getSelenium().isChecked( locator ) );
633 public void assertIsNotChecked( String locator )
635 Assert.assertFalse( getSelenium().isChecked( locator ) );
638 public void assertXpathCount( String locator, int expectedCount )
640 int count = getSelenium().getXpathCount( locator ).intValue();
641 Assert.assertEquals( count, expectedCount );
644 public void assertElementValue( String locator, String expectedValue )
646 Assert.assertEquals( getSelenium().getValue( locator ), expectedValue );
649 @AfterSeleniumFailure
650 public void captureScreenShotOnFailure( Throwable failure, String methodName, String className )
652 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" );
653 String time = sdf.format( new Date() );
654 File targetPath = new File( "target", "screenshots" );
658 for ( StackTraceElement stackTrace : failure.getStackTrace() )
660 if ( stackTrace.getClassName().equals( this.getClass().getName() ) )
662 lineNumber = stackTrace.getLineNumber();
668 Selenium selenium = getSelenium();
669 String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;
671 selenium.windowMaximize();
673 File fileName = new File( targetPath, fileBaseName + ".png" );
674 selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "background=#FFFFFF" );