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.ArchivaSeleniumExecutionRule;
25 import org.junit.Assert;
26 import org.junit.Rule;
29 import java.io.FileInputStream;
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 ArchivaSeleniumExecutionRule archivaSeleniumExecutionRule = new ArchivaSeleniumExecutionRule();
50 public String browser = System.getProperty( "browser" );
52 public String baseUrl =
53 "http://localhost:" + System.getProperty( "tomcat.maven.http.port" ) + "/archiva/index.html?request_lang=en";
55 public int maxWaitTimeInMs = Integer.getInteger( "maxWaitTimeInMs" );
57 public String seleniumHost = System.getProperty( "seleniumHost", "localhost" );
59 public int seleniumPort = Integer.getInteger( "seleniumPort", 4444 );
61 private Selenium selenium = null;
66 * this method is called by the Rule before executing a test
74 p.load( this.getClass().getClassLoader().getResourceAsStream( "test.properties" ) );
76 Properties tomcatPortProperties = new Properties();
77 tomcatPortProperties.load(
78 new FileInputStream( new File( System.getProperty( "tomcat.propertiesPortFilePath" ) ) ) );
80 int tomcatPort = Integer.parseInt( tomcatPortProperties.getProperty( "tomcat.maven.http.port" ) );
82 baseUrl = "http://localhost:" + tomcatPort + "/archiva/index.html?request_lang=en";
84 open( baseUrl, browser, seleniumHost, seleniumPort, maxWaitTimeInMs );
85 archivaSeleniumExecutionRule.selenium = selenium;
90 * this method is called by the Rule after executing a tests
94 if ( getSelenium() != null )
101 * Initialize selenium
103 public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort, int maxWaitTimeInMs )
108 if ( getSelenium() == null )
110 selenium = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
112 selenium.setTimeout( Integer.toString( maxWaitTimeInMs ) );
115 catch ( Exception e )
118 System.out.print( e.getMessage() );
123 public void assertAdminCreated()
126 initializeArchiva( baseUrl, browser, maxWaitTimeInMs, seleniumHost, seleniumPort );
129 public void initializeArchiva( String baseUrl, String browser, int maxWaitTimeInMs, String seleniumHost,
134 open( baseUrl, browser, seleniumHost, seleniumPort, maxWaitTimeInMs );
136 getSelenium().open( baseUrl );
140 // if not admin user created create one
141 if ( isElementVisible( "create-admin-link" ) )
143 Assert.assertFalse( getSelenium().isVisible( "login-link-a" ) );
144 Assert.assertFalse( getSelenium().isVisible( "register-link-a" ) );
145 clickLinkWithLocator( "create-admin-link-a", false );
147 String fullname = getProperty( "ADMIN_FULLNAME" );
148 String username = getAdminUsername();
149 String mail = getProperty( "ADMIN_EMAIL" );
150 String password = getProperty( "ADMIN_PASSWORD" );
151 submitAdminData( fullname, mail, password );
152 assertUserLoggedIn( username );
153 clickLinkWithLocator( "logout-link-a" );
157 Assert.assertTrue( getSelenium().isVisible( "login-link-a" ) );
158 Assert.assertTrue( getSelenium().isVisible( "register-link-a" ) );
159 login( getAdminUsername(), getAdminPassword() );
164 public Selenium getSelenium()
169 protected String getProperty( String key )
171 return p.getProperty( key );
174 public String getAdminUsername()
176 String adminUsername = getProperty( "ADMIN_USERNAME" );
177 return adminUsername;
180 public String getAdminPassword()
182 String adminPassword = getProperty( "ADMIN_PASSWORD" );
183 return adminPassword;
186 public void submitAdminData( String fullname, String email, String password )
188 setFieldValue( "fullname", fullname );
189 setFieldValue( "email", email );
190 setFieldValue( "password", password );
191 setFieldValue( "confirmPassword", password );
192 clickButtonWithLocator( "user-create-form-register-button" );
195 public void login( String username, String password )
197 login( username, password, true, "Login Page" );
200 public void login( String username, String password, boolean valid, String assertReturnPage )
202 if ( isElementVisible( "login-link-a" ) )//isElementPresent( "loginLink" ) )
206 submitLoginPage( username, password, false, valid, assertReturnPage );
210 assertUserLoggedIn( username );
215 public void goToLoginPage()
217 getSelenium().open( baseUrl );
219 // are we already logged in ?
220 if ( isElementVisible( "logout-link" ) ) //isElementPresent( "logoutLink" ) )
223 clickLinkWithLocator( "logout-link-a", false );
224 clickLinkWithLocator( "login-link-a" );
226 else if ( isElementVisible( "login-link-a" ) )
228 clickLinkWithLocator( "login-link-a" );
234 public void assertLoginModal()
236 assertElementPresent( "user-login-form" );
237 Assert.assertTrue( isElementVisible( "register-link" ) );
238 assertElementPresent( "user-login-form-username" );
239 assertElementPresent( "user-login-form-password" );
240 assertButtonWithIdPresent( "modal-login-ok" );
244 public void submitLoginPage( String username, String password )
246 submitLoginPage( username, password, false, true, "Login Page" );
249 public void submitLoginPage( String username, String password, boolean validUsernamePassword )
251 submitLoginPage( username, password, false, validUsernamePassword, "Login Page" );
254 public void submitLoginPage( String username, String password, boolean rememberMe, boolean validUsernamePassword,
255 String assertReturnPage )
257 clickLinkWithLocator( "login-link-a", false );
258 setFieldValue( "user-login-form-username", username );
259 setFieldValue( "user-login-form-password", password );
263 checkField( "rememberMe" );
266 clickButtonWithLocator( "modal-login-ok" );
267 if ( validUsernamePassword )
269 assertUserLoggedIn( username );
274 if ( "Login Page".equals( assertReturnPage ) )
280 assertPage( assertReturnPage );
285 // *******************************************************
286 // Auxiliar methods. This method help us and simplify test.
287 // *******************************************************
289 protected void assertUserLoggedIn( String username )
291 Assert.assertFalse( isElementVisible( "login-link" ) );
292 Assert.assertTrue( isElementVisible( "logout-link" ) );
293 Assert.assertFalse( isElementVisible( "register-link" ) );
294 Assert.assertFalse( isElementVisible( "create-admin-link" ) );
297 public void assertCreateAdmin()
299 assertElementPresent( "user-create" );
300 assertFieldValue( "admin", "username" );
301 assertElementPresent( "fullname" );
302 assertElementPresent( "password" );
303 assertElementPresent( "confirmPassword" );
304 assertElementPresent( "email" );
307 public void assertFieldValue( String fieldValue, String fieldName )
309 assertElementPresent( fieldName );
310 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
313 public void assertPage( String title )
315 Assert.assertEquals( getTitle(), title );
318 public String getTitle()
321 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
324 public String getHtmlContent()
326 return getSelenium().getHtmlSource();
329 public String getText( String locator )
331 return getSelenium().getText( locator );
334 public void assertTextPresent( String text )
336 Assert.assertTrue( "'" + text + "' isn't present.", getSelenium().isTextPresent( text ) );
340 * one of text args must be in the page so use en and fr text (olamy use en locale :-) )
344 public void assertTextPresent( String... texts )
346 boolean present = false;
347 StringBuilder sb = new StringBuilder();
348 for ( String text : texts )
350 present = present || getSelenium().isTextPresent( text );
351 sb.append( " " + text + " " );
353 Assert.assertTrue( "'one of the following test " + sb.toString() + "' isn't present.", present );
356 public void assertTextNotPresent( String text )
358 Assert.assertFalse( "'" + text + "' is present.", getSelenium().isTextPresent( text ) );
361 public void assertElementPresent( String elementLocator )
363 Assert.assertTrue( "'" + elementLocator + "' isn't present.", isElementPresent( elementLocator ) );
366 public void assertElementNotPresent( String elementLocator )
368 Assert.assertFalse( "'" + elementLocator + "' is present.", isElementPresent( elementLocator ) );
371 public void assertLinkPresent( String text )
373 Assert.assertTrue( "The link '" + text + "' isn't present.", isElementPresent( "link=" + text ) );
376 public void assertLinkNotPresent( String text )
378 Assert.assertFalse( "The link('" + text + "' is present.", isElementPresent( "link=" + text ) );
381 public void assertLinkNotVisible( String text )
383 Assert.assertFalse( "The link('" + text + "' is visible.", isElementVisible( "link=" + text ) );
386 public void assertLinkVisible( String text )
388 Assert.assertTrue( "The link('" + text + "' is not visible.", isElementVisible( "link=" + text ) );
391 public void assertImgWithAlt( String alt )
393 assertElementPresent( "/¯img[@alt='" + alt + "']" );
396 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
398 String locator = "//tr[" + row + "]/td[" + column + "]/";
399 locator += isALink ? "a/" : "";
400 locator += "img[@alt='" + alt + "']";
402 assertElementPresent( locator );
405 public void assertImgWithAltNotPresent( String alt )
407 assertElementNotPresent( "/¯img[@alt='" + alt + "']" );
410 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
412 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
415 public boolean isTextPresent( String text )
417 return getSelenium().isTextPresent( text );
420 public boolean isLinkPresent( String text )
422 return isElementPresent( "link=" + text );
425 public boolean isElementPresent( String locator )
427 return getSelenium().isElementPresent( locator );
430 public boolean isElementVisible( String locator )
432 return getSelenium().isVisible( locator );
436 public void waitPage()
438 // TODO define a smaller maxWaitTimeJsInMs for wait javascript response for browser side validation
439 //getSelenium().w .wait( Long.parseLong( maxWaitTimeInMs ) );
440 //getSelenium().waitForPageToLoad( maxWaitTimeInMs );
441 // http://jira.openqa.org/browse/SRC-302
442 // those hack looks to break some tests :-(
443 // getSelenium().waitForCondition( "selenium.isElementPresent('document.body');", maxWaitTimeInMs );
444 //getSelenium().waitForCondition( "selenium.isElementPresent('footer');", maxWaitTimeInMs );
445 //getSelenium().waitForCondition( "selenium.browserbot.getCurrentWindow().document.getElementById('footer')",
446 // maxWaitTimeInMs );
447 // so the only hack is to not use a too small wait time
451 Thread.sleep( maxWaitTimeInMs );
453 catch ( InterruptedException e )
455 throw new RuntimeException( "issue on Thread.sleep : " + e.getMessage(), e );
459 public String getFieldValue( String fieldName )
461 return getSelenium().getValue( fieldName );
464 public String getCellValueFromTable( String tableElement, int row, int column )
466 return getSelenium().getTable( tableElement + "." + row + "." + column );
469 public void selectValue( String locator, String value )
471 getSelenium().select( locator, "label=" + value );
475 public void assertOptionPresent( String selectField, String[] options )
477 assertElementPresent( selectField );
478 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
479 List<String> expected = Arrays.asList( options );
480 List<String> present = Arrays.asList( optionsPresent );
481 Assert.assertTrue( "Options expected are not included in present options", present.containsAll( expected ) );
484 public void assertSelectedValue( String value, String fieldName )
486 assertElementPresent( fieldName );
487 String optionsPresent = getSelenium().getSelectedLabel( value );
488 Assert.assertEquals( optionsPresent, value );
493 clickLinkWithXPath( "//input[@type='submit']" );
496 public void assertButtonWithValuePresent( String text )
498 Assert.assertTrue( "'" + text + "' button isn't present", isButtonWithValuePresent( text ) );
501 public void assertButtonWithIdPresent( String id )
503 Assert.assertTrue( "'Button with id =" + id + "' isn't present", isButtonWithIdPresent( id ) );
506 public void assertButtonWithValueNotPresent( String text )
508 Assert.assertFalse( "'" + text + "' button is present", isButtonWithValuePresent( text ) );
511 public boolean isButtonWithValuePresent( String text )
513 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent(
514 "//input[@value='" + text + "']" );
517 public boolean isButtonWithIdPresent( String text )
519 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
522 public void clickButtonWithName( String text, boolean wait )
524 clickLinkWithXPath( "//input[@name='" + text + "']", wait );
527 public void clickButtonWithValue( String text )
529 clickButtonWithValue( text, true );
532 public void clickButtonWithValue( String text, boolean wait )
534 assertButtonWithValuePresent( text );
536 if ( isElementPresent( "//button[@value='" + text + "']" ) )
538 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
542 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
546 public void clickSubmitWithLocator( String locator )
548 clickLinkWithLocator( locator );
551 public void clickSubmitWithLocator( String locator, boolean wait )
553 clickLinkWithLocator( locator, wait );
556 public void clickImgWithAlt( String alt )
558 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
561 public void clickLinkWithText( String text )
563 clickLinkWithText( text, true );
566 public void clickLinkWithText( String text, boolean wait )
568 clickLinkWithLocator( "link=" + text, wait );
571 public void clickLinkWithXPath( String xpath )
573 clickLinkWithXPath( xpath, true );
576 public void clickLinkWithXPath( String xpath, boolean wait )
578 clickLinkWithLocator( "xpath=" + xpath, wait );
581 public void clickLinkWithLocator( String locator )
583 clickLinkWithLocator( locator, true );
586 public void clickLinkWithLocator( String locator, boolean wait )
588 assertElementPresent( locator );
589 getSelenium().click( locator );
596 public void clickButtonWithLocator( String locator )
598 clickButtonWithLocator( locator, true );
601 public void clickButtonWithLocator( String locator, boolean wait )
603 assertElementPresent( locator );
604 getSelenium().click( locator );
611 public void setFieldValues( Map<String, String> fieldMap )
613 Map.Entry<String, String> entry;
615 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
617 entry = entries.next();
619 getSelenium().type( entry.getKey(), entry.getValue() );
623 public void setFieldValue( String fieldName, String value )
625 getSelenium().type( fieldName, value );
628 public void checkField( String locator )
630 getSelenium().check( locator );
633 public void uncheckField( String locator )
635 getSelenium().uncheck( locator );
638 public boolean isChecked( String locator )
640 return getSelenium().isChecked( locator );
643 public void assertIsChecked( String locator )
645 Assert.assertTrue( getSelenium().isChecked( locator ) );
648 public void assertIsNotChecked( String locator )
650 Assert.assertFalse( getSelenium().isChecked( locator ) );
653 public void assertXpathCount( String locator, int expectedCount )
655 int count = getSelenium().getXpathCount( locator ).intValue();
656 Assert.assertEquals( count, expectedCount );
659 public void assertElementValue( String locator, String expectedValue )
661 Assert.assertEquals( getSelenium().getValue( locator ), expectedValue );
664 public String captureScreenShotOnFailure( Throwable failure, String methodName, String className )
666 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" );
667 String time = sdf.format( new Date() );
668 File targetPath = new File( "target", "screenshots" );
672 for ( StackTraceElement stackTrace : failure.getStackTrace() )
674 if ( stackTrace.getClassName().equals( this.getClass().getName() ) )
676 lineNumber = stackTrace.getLineNumber();
682 Selenium selenium = getSelenium();
683 String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;
685 selenium.windowMaximize();
687 File fileName = new File( targetPath, fileBaseName + ".png" );
689 selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "background=#FFFFFF" );
691 return fileName.getAbsolutePath();