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.ScreenshotCaptureRule;
25 import org.junit.Assert;
26 import org.junit.Rule;
29 import java.text.SimpleDateFormat;
30 import java.util.Arrays;
31 import java.util.Date;
32 import java.util.Iterator;
33 import java.util.List;
35 import java.util.Map.Entry;
36 import java.util.Properties;
39 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
40 * @version $Id: AbstractSeleniumTestCase.java 761154 2009-04-02 03:31:19Z wsmoak $
43 public abstract class AbstractSeleniumTest
47 public ScreenshotCaptureRule screenshotCaptureRule = new ScreenshotCaptureRule();
49 public String browser = System.getProperty( "browser" );
51 public String baseUrl = System.getProperty( "baseUrl" );
53 public int maxWaitTimeInMs = Integer.getInteger( "maxWaitTimeInMs" );
55 public String seleniumHost = System.getProperty( "seleniumHost", "localhost" );
57 public int seleniumPort = Integer.getInteger( "seleniumPort", 4444 );
59 private Selenium selenium = null;
64 * this method is called by the Rule before executing a test
71 p.load( this.getClass().getClassLoader().getResourceAsStream( "test.properties" ) );
72 open( baseUrl, browser, seleniumHost, seleniumPort, maxWaitTimeInMs );
73 screenshotCaptureRule.selenium = selenium;
78 * this method is called by the Rule after executing a tests
82 if ( getSelenium() != null )
91 public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort, int maxWaitTimeInMs )
96 if ( getSelenium() == null )
98 selenium = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
100 selenium.setTimeout( Integer.toString( maxWaitTimeInMs ) );
103 catch ( Exception e )
106 System.out.print( e.getMessage() );
111 public void assertAdminCreated()
114 initializeArchiva( baseUrl, browser, maxWaitTimeInMs, seleniumHost, seleniumPort );
117 public void initializeArchiva( String baseUrl, String browser, int maxWaitTimeInMs, String seleniumHost,
122 open( baseUrl, browser, seleniumHost, seleniumPort, maxWaitTimeInMs );
124 getSelenium().open( baseUrl );
128 // if not admin user created create one
129 if ( isElementVisible( "create-admin-link" ) )
131 Assert.assertFalse( getSelenium().isVisible( "login-link-a" ) );
132 Assert.assertFalse( getSelenium().isVisible( "register-link-a" ) );
133 clickLinkWithLocator( "create-admin-link-a", false );
135 String fullname = getProperty( "ADMIN_FULLNAME" );
136 String username = getAdminUsername();
137 String mail = getProperty( "ADMIN_EMAIL" );
138 String password = getProperty( "ADMIN_PASSWORD" );
139 submitAdminData( fullname, mail, password );
140 assertUserLoggedIn( username );
141 clickLinkWithLocator( "logout-link-a" );
145 Assert.assertTrue( getSelenium().isVisible( "login-link-a" ) );
146 Assert.assertTrue( getSelenium().isVisible( "register-link-a" ) );
147 login( getAdminUsername(), getAdminPassword() );
152 public Selenium getSelenium()
157 protected String getProperty( String key )
159 return p.getProperty( key );
162 public String getAdminUsername()
164 String adminUsername = getProperty( "ADMIN_USERNAME" );
165 return adminUsername;
168 public String getAdminPassword()
170 String adminPassword = getProperty( "ADMIN_PASSWORD" );
171 return adminPassword;
174 public void submitAdminData( String fullname, String email, String password )
176 setFieldValue( "fullname", fullname );
177 setFieldValue( "email", email );
178 setFieldValue( "password", password );
179 setFieldValue( "confirmPassword", password );
180 clickButtonWithLocator( "user-create-form-register-button" );
183 public void login( String username, String password )
185 login( username, password, true, "Login Page" );
188 public void login( String username, String password, boolean valid, String assertReturnPage )
190 if ( isElementVisible( "login-link-a" ) )//isElementPresent( "loginLink" ) )
194 submitLoginPage( username, password, false, valid, assertReturnPage );
198 assertUserLoggedIn( username );
203 public void goToLoginPage()
205 getSelenium().open( baseUrl );
207 // are we already logged in ?
208 if ( isElementVisible( "logout-link" ) ) //isElementPresent( "logoutLink" ) )
211 clickLinkWithLocator( "logout-link-a", false );
212 clickLinkWithLocator( "login-link-a" );
214 else if ( isElementVisible( "login-link-a" ) )
216 clickLinkWithLocator( "login-link-a" );
222 public void assertLoginModal()
224 assertElementPresent( "user-login-form" );
225 Assert.assertTrue( isElementVisible( "register-link" ) );
226 assertElementPresent( "user-login-form-username" );
227 assertElementPresent( "user-login-form-password" );
228 assertButtonWithIdPresent( "modal-login-ok" );
232 public void submitLoginPage( String username, String password )
234 submitLoginPage( username, password, false, true, "Login Page" );
237 public void submitLoginPage( String username, String password, boolean validUsernamePassword )
239 submitLoginPage( username, password, false, validUsernamePassword, "Login Page" );
242 public void submitLoginPage( String username, String password, boolean rememberMe, boolean validUsernamePassword,
243 String assertReturnPage )
245 clickLinkWithLocator( "login-link-a", false );
246 setFieldValue( "user-login-form-username", username );
247 setFieldValue( "user-login-form-password", password );
251 checkField( "rememberMe" );
254 clickButtonWithLocator( "modal-login-ok" );
255 if ( validUsernamePassword )
257 assertUserLoggedIn( username );
262 if ( "Login Page".equals( assertReturnPage ) )
268 assertPage( assertReturnPage );
273 // *******************************************************
274 // Auxiliar methods. This method help us and simplify test.
275 // *******************************************************
277 protected void assertUserLoggedIn( String username )
279 Assert.assertFalse( isElementVisible( "login-link" ) );
280 Assert.assertTrue( isElementVisible( "logout-link" ) );
281 Assert.assertFalse( isElementVisible( "register-link" ) );
282 Assert.assertFalse( isElementVisible( "create-admin-link" ) );
285 public void assertCreateAdmin()
287 assertElementPresent( "user-create" );
288 assertFieldValue( "admin", "username" );
289 assertElementPresent( "fullname" );
290 assertElementPresent( "password" );
291 assertElementPresent( "confirmPassword" );
292 assertElementPresent( "email" );
295 public void assertFieldValue( String fieldValue, String fieldName )
297 assertElementPresent( fieldName );
298 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
301 public void assertPage( String title )
303 Assert.assertEquals( getTitle(), title );
306 public String getTitle()
309 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
312 public String getHtmlContent()
314 return getSelenium().getHtmlSource();
317 public String getText( String locator )
319 return getSelenium().getText( locator );
322 public void assertTextPresent( String text )
324 Assert.assertTrue( "'" + text + "' isn't present.", getSelenium().isTextPresent( text ) );
328 * one of text args must be in the page so use en and fr text (olamy use en locale :-) )
332 public void assertTextPresent( String... texts )
334 boolean present = false;
335 StringBuilder sb = new StringBuilder();
336 for ( String text : texts )
338 present = present || getSelenium().isTextPresent( text );
339 sb.append( " " + text + " " );
341 Assert.assertTrue( "'one of the following test " + sb.toString() + "' isn't present.", present );
344 public void assertTextNotPresent( String text )
346 Assert.assertFalse( "'" + text + "' is present.", getSelenium().isTextPresent( text ) );
349 public void assertElementPresent( String elementLocator )
351 Assert.assertTrue( "'" + elementLocator + "' isn't present.", isElementPresent( elementLocator ) );
354 public void assertElementNotPresent( String elementLocator )
356 Assert.assertFalse( "'" + elementLocator + "' is present.", isElementPresent( elementLocator ) );
359 public void assertLinkPresent( String text )
361 Assert.assertTrue( "The link '" + text + "' isn't present.", isElementPresent( "link=" + text ) );
364 public void assertLinkNotPresent( String text )
366 Assert.assertFalse( "The link('" + text + "' is present.", isElementPresent( "link=" + text ) );
369 public void assertLinkNotVisible( String text )
371 Assert.assertFalse( "The link('" + text + "' is visible.", isElementVisible( "link=" + text ) );
374 public void assertLinkVisible( String text )
376 Assert.assertTrue( "The link('" + text + "' is not visible.", isElementVisible( "link=" + text ) );
379 public void assertImgWithAlt( String alt )
381 assertElementPresent( "/¯img[@alt='" + alt + "']" );
384 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
386 String locator = "//tr[" + row + "]/td[" + column + "]/";
387 locator += isALink ? "a/" : "";
388 locator += "img[@alt='" + alt + "']";
390 assertElementPresent( locator );
393 public void assertImgWithAltNotPresent( String alt )
395 assertElementNotPresent( "/¯img[@alt='" + alt + "']" );
398 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
400 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
403 public boolean isTextPresent( String text )
405 return getSelenium().isTextPresent( text );
408 public boolean isLinkPresent( String text )
410 return isElementPresent( "link=" + text );
413 public boolean isElementPresent( String locator )
415 return getSelenium().isElementPresent( locator );
418 public boolean isElementVisible( String locator )
420 return getSelenium().isVisible( locator );
424 public void waitPage()
426 // TODO define a smaller maxWaitTimeJsInMs for wait javascript response for browser side validation
427 //getSelenium().w .wait( Long.parseLong( maxWaitTimeInMs ) );
428 //getSelenium().waitForPageToLoad( maxWaitTimeInMs );
429 // http://jira.openqa.org/browse/SRC-302
430 // those hack looks to break some tests :-(
431 // getSelenium().waitForCondition( "selenium.isElementPresent('document.body');", maxWaitTimeInMs );
432 //getSelenium().waitForCondition( "selenium.isElementPresent('footer');", maxWaitTimeInMs );
433 //getSelenium().waitForCondition( "selenium.browserbot.getCurrentWindow().document.getElementById('footer')",
434 // maxWaitTimeInMs );
435 // so the only hack is to not use a too small wait time
439 Thread.sleep( maxWaitTimeInMs );
441 catch ( InterruptedException e )
443 throw new RuntimeException( "issue on Thread.sleep : " + e.getMessage(), e );
447 public String getFieldValue( String fieldName )
449 return getSelenium().getValue( fieldName );
452 public String getCellValueFromTable( String tableElement, int row, int column )
454 return getSelenium().getTable( tableElement + "." + row + "." + column );
457 public void selectValue( String locator, String value )
459 getSelenium().select( locator, "label=" + value );
463 public void assertOptionPresent( String selectField, String[] options )
465 assertElementPresent( selectField );
466 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
467 List<String> expected = Arrays.asList( options );
468 List<String> present = Arrays.asList( optionsPresent );
469 Assert.assertTrue( "Options expected are not included in present options", present.containsAll( expected ) );
472 public void assertSelectedValue( String value, String fieldName )
474 assertElementPresent( fieldName );
475 String optionsPresent = getSelenium().getSelectedLabel( value );
476 Assert.assertEquals( optionsPresent, value );
481 clickLinkWithXPath( "//input[@type='submit']" );
484 public void assertButtonWithValuePresent( String text )
486 Assert.assertTrue( "'" + text + "' button isn't present", isButtonWithValuePresent( text ) );
489 public void assertButtonWithIdPresent( String id )
491 Assert.assertTrue( "'Button with id =" + id + "' isn't present", isButtonWithIdPresent( id ) );
494 public void assertButtonWithValueNotPresent( String text )
496 Assert.assertFalse( "'" + text + "' button is present", isButtonWithValuePresent( text ) );
499 public boolean isButtonWithValuePresent( String text )
501 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent(
502 "//input[@value='" + text + "']" );
505 public boolean isButtonWithIdPresent( String text )
507 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
510 public void clickButtonWithName( String text, boolean wait )
512 clickLinkWithXPath( "//input[@name='" + text + "']", wait );
515 public void clickButtonWithValue( String text )
517 clickButtonWithValue( text, true );
520 public void clickButtonWithValue( String text, boolean wait )
522 assertButtonWithValuePresent( text );
524 if ( isElementPresent( "//button[@value='" + text + "']" ) )
526 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
530 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
534 public void clickSubmitWithLocator( String locator )
536 clickLinkWithLocator( locator );
539 public void clickSubmitWithLocator( String locator, boolean wait )
541 clickLinkWithLocator( locator, wait );
544 public void clickImgWithAlt( String alt )
546 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
549 public void clickLinkWithText( String text )
551 clickLinkWithText( text, true );
554 public void clickLinkWithText( String text, boolean wait )
556 clickLinkWithLocator( "link=" + text, wait );
559 public void clickLinkWithXPath( String xpath )
561 clickLinkWithXPath( xpath, true );
564 public void clickLinkWithXPath( String xpath, boolean wait )
566 clickLinkWithLocator( "xpath=" + xpath, wait );
569 public void clickLinkWithLocator( String locator )
571 clickLinkWithLocator( locator, true );
574 public void clickLinkWithLocator( String locator, boolean wait )
576 assertElementPresent( locator );
577 getSelenium().click( locator );
584 public void clickButtonWithLocator( String locator )
586 clickButtonWithLocator( locator, true );
589 public void clickButtonWithLocator( String locator, boolean wait )
591 assertElementPresent( locator );
592 getSelenium().click( locator );
599 public void setFieldValues( Map<String, String> fieldMap )
601 Map.Entry<String, String> entry;
603 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
605 entry = entries.next();
607 getSelenium().type( entry.getKey(), entry.getValue() );
611 public void setFieldValue( String fieldName, String value )
613 getSelenium().type( fieldName, value );
616 public void checkField( String locator )
618 getSelenium().check( locator );
621 public void uncheckField( String locator )
623 getSelenium().uncheck( locator );
626 public boolean isChecked( String locator )
628 return getSelenium().isChecked( locator );
631 public void assertIsChecked( String locator )
633 Assert.assertTrue( getSelenium().isChecked( locator ) );
636 public void assertIsNotChecked( String locator )
638 Assert.assertFalse( getSelenium().isChecked( locator ) );
641 public void assertXpathCount( String locator, int expectedCount )
643 int count = getSelenium().getXpathCount( locator ).intValue();
644 Assert.assertEquals( count, expectedCount );
647 public void assertElementValue( String locator, String expectedValue )
649 Assert.assertEquals( getSelenium().getValue( locator ), expectedValue );
652 public void captureScreenShotOnFailure( Throwable failure, String methodName, String className )
654 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" );
655 String time = sdf.format( new Date() );
656 File targetPath = new File( "target", "screenshots" );
660 for ( StackTraceElement stackTrace : failure.getStackTrace() )
662 if ( stackTrace.getClassName().equals( this.getClass().getName() ) )
664 lineNumber = stackTrace.getLineNumber();
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" );