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;
42 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
43 * @version $Id: AbstractSeleniumTestCase.java 761154 2009-04-02 03:31:19Z wsmoak $
46 public abstract class AbstractSeleniumTest
49 public static String baseUrl;
51 public static String maxWaitTimeInMs;
53 private static ThreadLocal<Selenium> selenium = new ThreadLocal<Selenium>();
55 public static Properties p;
57 private final static String PROPERTIES_SEPARATOR = "=";
63 p.load( this.getClass().getClassLoader().getResourceAsStream( "test.properties" ) );
69 public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort, String maxWaitTimeInMs )
74 AbstractSeleniumTest.baseUrl = baseUrl;
75 AbstractSeleniumTest.maxWaitTimeInMs = maxWaitTimeInMs;
77 if ( getSelenium() == null )
79 DefaultSelenium s = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
82 s.setTimeout( maxWaitTimeInMs );
89 System.out.print( e.getMessage() );
94 public static Selenium getSelenium()
96 return selenium == null ? null : selenium.get();
99 protected String getProperty( String key )
101 return p.getProperty( key );
104 protected String getEscapeProperty( String key )
106 InputStream input = this.getClass().getClassLoader().getResourceAsStream( "test.properties" );
111 lines = IOUtils.readLines( input );
113 catch ( IOException e )
115 lines = new ArrayList<String>();
117 for ( String l : lines )
119 if ( l != null && l.startsWith( key ) )
121 int indexSeparator = l.indexOf( PROPERTIES_SEPARATOR );
122 value = l.substring( indexSeparator + 1 ).trim();
130 * Close selenium session.
135 if ( getSelenium() != null )
137 getSelenium().stop();
138 selenium.set( null );
142 // *******************************************************
143 // Auxiliar methods. This method help us and simplify test.
144 // *******************************************************
146 public void assertFieldValue( String fieldValue, String fieldName )
148 assertElementPresent( fieldName );
149 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
152 public void assertPage( String title )
154 Assert.assertEquals( getTitle(), title );
157 public String getTitle()
160 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
163 public String getHtmlContent()
165 return getSelenium().getHtmlSource();
168 public String getText( String locator )
170 return getSelenium().getText( locator );
173 public void assertTextPresent( String text )
175 Assert.assertTrue( "'" + text + "' isn't present.", getSelenium().isTextPresent( text ) );
179 * one of text args must be in the page so use en and fr text (olamy use en locale :-) )
183 public void assertTextPresent( String... texts )
185 boolean present = false;
186 StringBuilder sb = new StringBuilder();
187 for ( String text : texts )
189 present = present || getSelenium().isTextPresent( text );
190 sb.append( " " + text + " " );
192 Assert.assertTrue( "'one of the following test " + sb.toString() + "' isn't present.", present );
195 public void assertTextNotPresent( String text )
197 Assert.assertFalse( "'" + text + "' is present.", getSelenium().isTextPresent( text ) );
200 public void assertElementPresent( String elementLocator )
202 Assert.assertTrue( "'" + elementLocator + "' isn't present.", isElementPresent( elementLocator ) );
205 public void assertElementNotPresent( String elementLocator )
207 Assert.assertFalse( "'" + elementLocator + "' is present.", isElementPresent( elementLocator ) );
210 public void assertLinkPresent( String text )
212 Assert.assertTrue( "The link '" + text + "' isn't present.", isElementPresent( "link=" + text ) );
215 public void assertLinkNotPresent( String text )
217 Assert.assertFalse( "The link('" + text + "' is present.", isElementPresent( "link=" + text ) );
220 public void assertLinkNotVisible( String text )
222 Assert.assertFalse( "The link('" + text + "' is visible.", isElementVisible( "link=" + text ) );
225 public void assertLinkVisible( String text )
227 Assert.assertTrue( "The link('" + text + "' is not visible.", isElementVisible( "link=" + text ) );
230 public void assertImgWithAlt( String alt )
232 assertElementPresent( "/¯img[@alt='" + alt + "']" );
235 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
237 String locator = "//tr[" + row + "]/td[" + column + "]/";
238 locator += isALink ? "a/" : "";
239 locator += "img[@alt='" + alt + "']";
241 assertElementPresent( locator );
244 public void assertImgWithAltNotPresent( String alt )
246 assertElementNotPresent( "/¯img[@alt='" + alt + "']" );
249 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
251 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
254 public boolean isTextPresent( String text )
256 return getSelenium().isTextPresent( text );
259 public boolean isLinkPresent( String text )
261 return isElementPresent( "link=" + text );
264 public boolean isElementPresent( String locator )
266 return getSelenium().isElementPresent( locator );
269 public boolean isElementVisible( String locator )
271 return getSelenium().isVisible( locator );
275 public void waitPage()
277 // TODO define a smaller maxWaitTimeJsInMs for wait javascript response for browser side validation
278 //getSelenium().w .wait( Long.parseLong( maxWaitTimeInMs ) );
279 //getSelenium().waitForPageToLoad( maxWaitTimeInMs );
280 // http://jira.openqa.org/browse/SRC-302
281 // those hack looks to break some tests :-(
282 // getSelenium().waitForCondition( "selenium.isElementPresent('document.body');", maxWaitTimeInMs );
283 //getSelenium().waitForCondition( "selenium.isElementPresent('footer');", maxWaitTimeInMs );
284 //getSelenium().waitForCondition( "selenium.browserbot.getCurrentWindow().document.getElementById('footer')",
285 // maxWaitTimeInMs );
286 // so the only hack is to not use a too small wait time
290 Thread.sleep( Long.parseLong( maxWaitTimeInMs ) );
292 catch ( InterruptedException e )
294 throw new RuntimeException( "issue on Thread.sleep : " + e.getMessage(), e );
298 public String getFieldValue( String fieldName )
300 return getSelenium().getValue( fieldName );
303 public String getCellValueFromTable( String tableElement, int row, int column )
305 return getSelenium().getTable( tableElement + "." + row + "." + column );
308 public void selectValue( String locator, String value )
310 getSelenium().select( locator, "label=" + value );
314 public void assertOptionPresent( String selectField, String[] options )
316 assertElementPresent( selectField );
317 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
318 List<String> expected = Arrays.asList( options );
319 List<String> present = Arrays.asList( optionsPresent );
320 Assert.assertTrue( "Options expected are not included in present options", present.containsAll( expected ) );
323 public void assertSelectedValue( String value, String fieldName )
325 assertElementPresent( fieldName );
326 String optionsPresent = getSelenium().getSelectedLabel( value );
327 Assert.assertEquals( optionsPresent, value );
332 clickLinkWithXPath( "//input[@type='submit']" );
335 public void assertButtonWithValuePresent( String text )
337 Assert.assertTrue( "'" + text + "' button isn't present", isButtonWithValuePresent( text ) );
340 public void assertButtonWithIdPresent( String id )
342 Assert.assertTrue( "'Button with id =" + id + "' isn't present", isButtonWithIdPresent( id ) );
345 public void assertButtonWithValueNotPresent( String text )
347 Assert.assertFalse( "'" + text + "' button is present", isButtonWithValuePresent( text ) );
350 public boolean isButtonWithValuePresent( String text )
352 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent(
353 "//input[@value='" + text + "']" );
356 public boolean isButtonWithIdPresent( String text )
358 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
361 public void clickButtonWithName( String text, boolean wait )
363 clickLinkWithXPath( "//input[@name='" + text + "']", wait );
366 public void clickButtonWithValue( String text )
368 clickButtonWithValue( text, true );
371 public void clickButtonWithValue( String text, boolean wait )
373 assertButtonWithValuePresent( text );
375 if ( isElementPresent( "//button[@value='" + text + "']" ) )
377 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
381 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
385 public void clickSubmitWithLocator( String locator )
387 clickLinkWithLocator( locator );
390 public void clickSubmitWithLocator( String locator, boolean wait )
392 clickLinkWithLocator( locator, wait );
395 public void clickImgWithAlt( String alt )
397 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
400 public void clickLinkWithText( String text )
402 clickLinkWithText( text, true );
405 public void clickLinkWithText( String text, boolean wait )
407 clickLinkWithLocator( "link=" + text, wait );
410 public void clickLinkWithXPath( String xpath )
412 clickLinkWithXPath( xpath, true );
415 public void clickLinkWithXPath( String xpath, boolean wait )
417 clickLinkWithLocator( "xpath=" + xpath, wait );
420 public void clickLinkWithLocator( String locator )
422 clickLinkWithLocator( locator, true );
425 public void clickLinkWithLocator( String locator, boolean wait )
427 assertElementPresent( locator );
428 getSelenium().click( locator );
435 public void clickButtonWithLocator( String locator )
437 clickButtonWithLocator( locator, true );
440 public void clickButtonWithLocator( String locator, boolean wait )
442 assertElementPresent( locator );
443 getSelenium().click( locator );
450 public void setFieldValues( Map<String, String> fieldMap )
452 Map.Entry<String, String> entry;
454 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
456 entry = entries.next();
458 getSelenium().type( entry.getKey(), entry.getValue() );
462 public void setFieldValue( String fieldName, String value )
464 getSelenium().type( fieldName, value );
467 public void checkField( String locator )
469 getSelenium().check( locator );
472 public void uncheckField( String locator )
474 getSelenium().uncheck( locator );
477 public boolean isChecked( String locator )
479 return getSelenium().isChecked( locator );
482 public void assertIsChecked( String locator )
484 Assert.assertTrue( getSelenium().isChecked( locator ) );
487 public void assertIsNotChecked( String locator )
489 Assert.assertFalse( getSelenium().isChecked( locator ) );
492 public void assertXpathCount( String locator, int expectedCount )
494 int count = getSelenium().getXpathCount( locator ).intValue();
495 Assert.assertEquals( count, expectedCount );
498 public void assertElementValue( String locator, String expectedValue )
500 Assert.assertEquals( getSelenium().getValue( locator ), expectedValue );
503 @AfterSeleniumFailure
504 public void captureScreenShotOnFailure( Throwable failure )
506 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" );
507 String time = sdf.format( new Date() );
508 File targetPath = new File( "target", "screenshots" );
509 String cName = this.getClass().getName();
511 String methodName = "";
514 for ( StackTraceElement stackTrace : failure.getStackTrace() )
516 if ( stackTrace.getClassName().equals( this.getClass().getName() ) )
518 methodName = stackTrace.getMethodName();
519 lineNumber = stackTrace.getLineNumber();
524 String className = cName.substring( cName.lastIndexOf( '.' ) + 1 );
526 Selenium selenium = AbstractSeleniumTest.getSelenium();
527 String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;
529 selenium.windowMaximize();
531 File fileName = new File( targetPath, fileBaseName + ".png" );
532 selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "background=#FFFFFF" );