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.commons.io.IOUtils;
26 import org.junit.Assert;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.text.SimpleDateFormat;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Date;
35 import java.util.Iterator;
36 import java.util.List;
38 import java.util.Map.Entry;
39 import java.util.Properties;
40 import java.util.regex.Pattern;
43 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
44 * @version $Id: AbstractSeleniumTestCase.java 761154 2009-04-02 03:31:19Z wsmoak $
47 public abstract class AbstractSeleniumTest
50 public static String baseUrl;
52 public static String maxWaitTimeInMs;
54 private static ThreadLocal<Selenium> selenium = new ThreadLocal<Selenium>();
56 public static Properties p;
58 private final static String PROPERTIES_SEPARATOR = "=";
64 p.load( this.getClass().getClassLoader().getResourceAsStream( "test.properties" ) );
70 public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort, String maxWaitTimeInMs )
75 AbstractSeleniumTest.baseUrl = baseUrl;
76 AbstractSeleniumTest.maxWaitTimeInMs = maxWaitTimeInMs;
78 if ( getSelenium() == null )
80 DefaultSelenium s = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
83 s.setTimeout( maxWaitTimeInMs );
90 System.out.print( e.getMessage() );
95 public static Selenium getSelenium()
97 return selenium == null ? null : selenium.get();
100 protected String getProperty( String key )
102 return p.getProperty( key );
105 protected String getEscapeProperty( String key )
107 InputStream input = this.getClass().getClassLoader().getResourceAsStream( "test.properties" );
112 lines = IOUtils.readLines( input );
114 catch ( IOException e )
116 lines = new ArrayList<String>();
118 for ( String l : lines )
120 if ( l != null && l.startsWith( key ) )
122 int indexSeparator = l.indexOf( PROPERTIES_SEPARATOR );
123 value = l.substring( indexSeparator + 1 ).trim();
131 * Close selenium session.
136 if ( getSelenium() != null )
138 getSelenium().stop();
139 selenium.set( null );
143 // *******************************************************
144 // Auxiliar methods. This method help us and simplify test.
145 // *******************************************************
147 public void assertFieldValue( String fieldValue, String fieldName )
149 assertElementPresent( fieldName );
150 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
153 public void assertPage( String title )
155 Assert.assertEquals( getTitle(), title );
158 public String getTitle()
161 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
164 public String getHtmlContent()
166 return getSelenium().getHtmlSource();
169 public String getText( String locator )
171 return getSelenium().getText( locator );
174 public void assertTextPresent( String text )
176 Assert.assertTrue( "'" + text + "' isn't present.", getSelenium().isTextPresent( text ) );
180 * one of text args must be in the page so use en and fr text (olamy use en locale :-) )
184 public void assertTextPresent( String... texts )
186 boolean present = false;
187 StringBuilder sb = new StringBuilder();
188 for ( String text : texts )
190 present = present || getSelenium().isTextPresent( text );
191 sb.append( " " + text + " " );
193 Assert.assertTrue( "'one of the following test " + sb.toString() + "' isn't present.", present );
196 public void assertTextNotPresent( String text )
198 Assert.assertFalse( "'" + text + "' is present.", getSelenium().isTextPresent( text ) );
201 public void assertElementPresent( String elementLocator )
203 Assert.assertTrue( "'" + elementLocator + "' isn't present.", isElementPresent( elementLocator ) );
206 public void assertElementNotPresent( String elementLocator )
208 Assert.assertFalse( "'" + elementLocator + "' is present.", isElementPresent( elementLocator ) );
211 public void assertLinkPresent( String text )
213 Assert.assertTrue( "The link '" + text + "' isn't present.", isElementPresent( "link=" + text ) );
216 public void assertLinkNotPresent( String text )
218 Assert.assertFalse( "The link('" + text + "' is present.", isElementPresent( "link=" + text ) );
221 public void assertLinkNotVisible( String text )
223 Assert.assertFalse( "The link('" + text + "' is visible.", isElementVisible( "link=" + text ) );
226 public void assertLinkVisible( String text )
228 Assert.assertTrue( "The link('" + text + "' is not visible.", isElementVisible( "link=" + text ) );
231 public void assertImgWithAlt( String alt )
233 assertElementPresent( "/¯img[@alt='" + alt + "']" );
236 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
238 String locator = "//tr[" + row + "]/td[" + column + "]/";
239 locator += isALink ? "a/" : "";
240 locator += "img[@alt='" + alt + "']";
242 assertElementPresent( locator );
245 public void assertImgWithAltNotPresent( String alt )
247 assertElementNotPresent( "/¯img[@alt='" + alt + "']" );
250 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
252 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
255 public boolean isTextPresent( String text )
257 return getSelenium().isTextPresent( text );
260 public boolean isLinkPresent( String text )
262 return isElementPresent( "link=" + text );
265 public boolean isElementPresent( String locator )
267 return getSelenium().isElementPresent( locator );
270 public boolean isElementVisible( String locator )
272 return getSelenium().isVisible( locator );
276 public void waitPage()
278 // TODO define a smaller maxWaitTimeJsInMs for wait javascript response for browser side validation
279 //getSelenium().w .wait( Long.parseLong( maxWaitTimeInMs ) );
280 //getSelenium().waitForPageToLoad( maxWaitTimeInMs );
281 // http://jira.openqa.org/browse/SRC-302
282 // those hack looks to break some tests :-(
283 // getSelenium().waitForCondition( "selenium.isElementPresent('document.body');", maxWaitTimeInMs );
284 //getSelenium().waitForCondition( "selenium.isElementPresent('footer');", maxWaitTimeInMs );
285 //getSelenium().waitForCondition( "selenium.browserbot.getCurrentWindow().document.getElementById('footer')",
286 // maxWaitTimeInMs );
287 // so the only hack is to not use a too small wait time
291 Thread.sleep( Long.parseLong( maxWaitTimeInMs ) );
293 catch ( InterruptedException e )
295 throw new RuntimeException( "issue on Thread.sleep : " + e.getMessage(), e );
299 public String getFieldValue( String fieldName )
301 return getSelenium().getValue( fieldName );
304 public String getCellValueFromTable( String tableElement, int row, int column )
306 return getSelenium().getTable( tableElement + "." + row + "." + column );
309 public void selectValue( String locator, String value )
311 getSelenium().select( locator, "label=" + value );
315 public void assertOptionPresent( String selectField, String[] options )
317 assertElementPresent( selectField );
318 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
319 List<String> expected = Arrays.asList( options );
320 List<String> present = Arrays.asList( optionsPresent );
321 Assert.assertTrue( "Options expected are not included in present options", present.containsAll( expected ) );
324 public void assertSelectedValue( String value, String fieldName )
326 assertElementPresent( fieldName );
327 String optionsPresent = getSelenium().getSelectedLabel( value );
328 Assert.assertEquals( optionsPresent, value );
333 clickLinkWithXPath( "//input[@type='submit']" );
336 public void assertButtonWithValuePresent( String text )
338 Assert.assertTrue( "'" + text + "' button isn't present", isButtonWithValuePresent( text ) );
341 public void assertButtonWithIdPresent( String id )
343 Assert.assertTrue( "'Button with id =" + id + "' isn't present", isButtonWithIdPresent( id ) );
346 public void assertButtonWithValueNotPresent( String text )
348 Assert.assertFalse( "'" + text + "' button is present", isButtonWithValuePresent( text ) );
351 public boolean isButtonWithValuePresent( String text )
353 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent(
354 "//input[@value='" + text + "']" );
357 public boolean isButtonWithIdPresent( String text )
359 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
362 public void clickButtonWithName( String text, boolean wait )
364 clickLinkWithXPath( "//input[@name='" + text + "']", wait );
367 public void clickButtonWithValue( String text )
369 clickButtonWithValue( text, true );
372 public void clickButtonWithValue( String text, boolean wait )
374 assertButtonWithValuePresent( text );
376 if ( isElementPresent( "//button[@value='" + text + "']" ) )
378 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
382 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
386 public void clickSubmitWithLocator( String locator )
388 clickLinkWithLocator( locator );
391 public void clickSubmitWithLocator( String locator, boolean wait )
393 clickLinkWithLocator( locator, wait );
396 public void clickImgWithAlt( String alt )
398 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
401 public void clickLinkWithText( String text )
403 clickLinkWithText( text, true );
406 public void clickLinkWithText( String text, boolean wait )
408 clickLinkWithLocator( "link=" + text, wait );
411 public void clickLinkWithXPath( String xpath )
413 clickLinkWithXPath( xpath, true );
416 public void clickLinkWithXPath( String xpath, boolean wait )
418 clickLinkWithLocator( "xpath=" + xpath, wait );
421 public void clickLinkWithLocator( String locator )
423 clickLinkWithLocator( locator, true );
426 public void clickLinkWithLocator( String locator, boolean wait )
428 assertElementPresent( locator );
429 getSelenium().click( locator );
436 public void clickButtonWithLocator( String locator )
438 clickButtonWithLocator( locator, true );
441 public void clickButtonWithLocator( String locator, boolean wait )
443 assertElementPresent( locator );
444 getSelenium().click( locator );
451 public void setFieldValues( Map<String, String> fieldMap )
453 Map.Entry<String, String> entry;
455 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
457 entry = entries.next();
459 getSelenium().type( entry.getKey(), entry.getValue() );
463 public void setFieldValue( String fieldName, String value )
465 getSelenium().type( fieldName, value );
468 public void checkField( String locator )
470 getSelenium().check( locator );
473 public void uncheckField( String locator )
475 getSelenium().uncheck( locator );
478 public boolean isChecked( String locator )
480 return getSelenium().isChecked( locator );
483 public void assertIsChecked( String locator )
485 Assert.assertTrue( getSelenium().isChecked( locator ) );
488 public void assertIsNotChecked( String locator )
490 Assert.assertFalse( getSelenium().isChecked( locator ) );
493 public void assertXpathCount( String locator, int expectedCount )
495 int count = getSelenium().getXpathCount( locator ).intValue();
496 Assert.assertEquals( count, expectedCount );
499 public void assertElementValue( String locator, String expectedValue )
501 Assert.assertEquals( getSelenium().getValue( locator ), expectedValue );
504 @AfterSeleniumFailure
505 public void captureScreenShotOnFailure( Throwable failure )
507 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" );
508 String time = sdf.format( new Date() );
509 File targetPath = new File( "target", "screenshots" );
510 StackTraceElement stackTrace[] = failure.getStackTrace();
511 String cName = this.getClass().getName();
512 int index = getStackTraceIndexOfCallingClass( cName, stackTrace );
513 String methodName = stackTrace[index].getMethodName();
514 int lNumber = stackTrace[index].getLineNumber();
515 String lineNumber = Integer.toString( lNumber );
516 String className = cName.substring( cName.lastIndexOf( '.' ) + 1 );
518 Selenium selenium = AbstractSeleniumTest.getSelenium();
519 String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;
521 selenium.windowMaximize();
523 File fileName = new File( targetPath, fileBaseName + ".png" );
524 selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "background=#FFFFFF" );
528 private int getStackTraceIndexOfCallingClass( String nameOfClass, StackTraceElement stackTrace[] )
530 boolean match = false;
534 String className = stackTrace[i].getClassName();
535 match = Pattern.matches( nameOfClass, className );
538 while ( match == false );