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.commons.io.IOUtils;
25 import org.testng.Assert;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Iterator;
32 import java.util.List;
34 import java.util.Map.Entry;
35 import java.util.Properties;
38 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
39 * @version $Id: AbstractSeleniumTestCase.java 761154 2009-04-02 03:31:19Z wsmoak $
42 public abstract class AbstractSeleniumTest
45 public static String baseUrl;
47 public static String maxWaitTimeInMs;
49 private static ThreadLocal<Selenium> selenium = new ThreadLocal<Selenium>();
51 public static Properties p;
53 private final static String PROPERTIES_SEPARATOR = "=";
59 p.load( this.getClass().getClassLoader().getResourceAsStream( "testng.properties" ) );
65 public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort, String maxWaitTimeInMs )
70 AbstractSeleniumTest.baseUrl = baseUrl;
71 AbstractSeleniumTest.maxWaitTimeInMs = maxWaitTimeInMs;
73 if ( getSelenium() == null )
75 DefaultSelenium s = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
78 s.setTimeout( maxWaitTimeInMs );
85 System.out.print( e.getMessage() );
90 public static Selenium getSelenium()
92 return selenium == null ? null : selenium.get();
95 protected String getProperty( String key )
97 return p.getProperty( key );
100 protected String getEscapeProperty( String key )
102 InputStream input = this.getClass().getClassLoader().getResourceAsStream( "testng.properties" );
107 lines = IOUtils.readLines( input );
109 catch ( IOException e )
111 lines = new ArrayList<String>();
113 for ( String l : lines )
115 if ( l != null && l.startsWith( key ) )
117 int indexSeparator = l.indexOf( PROPERTIES_SEPARATOR );
118 value = l.substring( indexSeparator + 1 ).trim();
126 * Close selenium session. Called from AfterSuite method of sub-class
131 if ( getSelenium() != null )
133 getSelenium().stop();
134 selenium.set( null );
138 // *******************************************************
139 // Auxiliar methods. This method help us and simplify test.
140 // *******************************************************
142 public void assertFieldValue( String fieldValue, String fieldName )
144 assertElementPresent( fieldName );
145 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
148 public void assertPage( String title )
150 Assert.assertEquals( getTitle(), title );
153 public String getTitle()
156 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
159 public String getHtmlContent()
161 return getSelenium().getHtmlSource();
164 public String getText( String locator )
166 return getSelenium().getText( locator );
169 public void assertTextPresent( String text )
171 Assert.assertTrue( getSelenium().isTextPresent( text ), "'" + text + "' isn't present." );
175 * one of text args must be in the page so use en and fr text (olamy use en locale :-) )
179 public void assertTextPresent( String... texts )
181 boolean present = false;
182 StringBuilder sb = new StringBuilder();
183 for ( String text : texts )
185 present = present || getSelenium().isTextPresent( text );
186 sb.append( " " + text + " " );
188 Assert.assertTrue( present, "'one of the following test " + sb.toString() + "' isn't present." );
191 public void assertTextNotPresent( String text )
193 Assert.assertFalse( getSelenium().isTextPresent( text ), "'" + text + "' is present." );
196 public void assertElementPresent( String elementLocator )
198 Assert.assertTrue( isElementPresent( elementLocator ), "'" + elementLocator + "' isn't present." );
201 public void assertElementNotPresent( String elementLocator )
203 Assert.assertFalse( isElementPresent( elementLocator ), "'" + elementLocator + "' is present." );
206 public void assertLinkPresent( String text )
208 Assert.assertTrue( isElementPresent( "link=" + text ), "The link '" + text + "' isn't present." );
211 public void assertLinkNotPresent( String text )
213 Assert.assertFalse( isElementPresent( "link=" + text ), "The link('" + text + "' is present." );
216 public void assertLinkNotVisible( String text )
218 Assert.assertFalse( isElementVisible( "link=" + text ), "The link('" + text + "' is visible." );
221 public void assertLinkVisible( String text )
223 Assert.assertTrue( isElementVisible( "link=" + text ), "The link('" + text + "' is not visible." );
226 public void assertImgWithAlt( String alt )
228 assertElementPresent( "/¯img[@alt='" + alt + "']" );
231 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
233 String locator = "//tr[" + row + "]/td[" + column + "]/";
234 locator += isALink ? "a/" : "";
235 locator += "img[@alt='" + alt + "']";
237 assertElementPresent( locator );
240 public void assertImgWithAltNotPresent( String alt )
242 assertElementNotPresent( "/¯img[@alt='" + alt + "']" );
245 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
247 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
250 public boolean isTextPresent( String text )
252 return getSelenium().isTextPresent( text );
255 public boolean isLinkPresent( String text )
257 return isElementPresent( "link=" + text );
260 public boolean isElementPresent( String locator )
262 return getSelenium().isElementPresent( locator );
265 public boolean isElementVisible( String locator )
267 return getSelenium().isVisible( locator );
271 public void waitPage()
273 // TODO define a smaller maxWaitTimeJsInMs for wait javascript response for browser side validation
274 //getSelenium().w .wait( Long.parseLong( maxWaitTimeInMs ) );
275 //getSelenium().waitForPageToLoad( maxWaitTimeInMs );
276 // http://jira.openqa.org/browse/SRC-302
277 // those hack looks to break some tests :-(
278 // getSelenium().waitForCondition( "selenium.isElementPresent('document.body');", maxWaitTimeInMs );
279 //getSelenium().waitForCondition( "selenium.isElementPresent('footer');", maxWaitTimeInMs );
280 //getSelenium().waitForCondition( "selenium.browserbot.getCurrentWindow().document.getElementById('footer')",
281 // maxWaitTimeInMs );
282 // so the only hack is to not use a too small wait time
286 Thread.sleep( Long.parseLong( maxWaitTimeInMs ) );
288 catch ( InterruptedException e )
290 throw new RuntimeException( "issue on Thread.sleep : " + e.getMessage(), e );
294 public String getFieldValue( String fieldName )
296 return getSelenium().getValue( fieldName );
299 public String getCellValueFromTable( String tableElement, int row, int column )
301 return getSelenium().getTable( tableElement + "." + row + "." + column );
304 public void selectValue( String locator, String value )
306 getSelenium().select( locator, "label=" + value );
310 public void assertOptionPresent( String selectField, String[] options )
312 assertElementPresent( selectField );
313 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
314 List<String> expected = Arrays.asList( options );
315 List<String> present = Arrays.asList( optionsPresent );
316 Assert.assertTrue( present.containsAll( expected ), "Options expected are not included in present options" );
319 public void assertSelectedValue( String value, String fieldName )
321 assertElementPresent( fieldName );
322 String optionsPresent = getSelenium().getSelectedLabel( value );
323 Assert.assertEquals( optionsPresent, value );
328 clickLinkWithXPath( "//input[@type='submit']" );
331 public void assertButtonWithValuePresent( String text )
333 Assert.assertTrue( isButtonWithValuePresent( text ), "'" + text + "' button isn't present" );
336 public void assertButtonWithIdPresent( String id )
338 Assert.assertTrue( isButtonWithIdPresent( id ), "'Button with id =" + id + "' isn't present" );
341 public void assertButtonWithValueNotPresent( String text )
343 Assert.assertFalse( isButtonWithValuePresent( text ), "'" + text + "' button is present" );
346 public boolean isButtonWithValuePresent( String text )
348 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent(
349 "//input[@value='" + text + "']" );
352 public boolean isButtonWithIdPresent( String text )
354 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
357 public void clickButtonWithName( String text, boolean wait )
359 clickLinkWithXPath( "//input[@name='" + text + "']", wait );
362 public void clickButtonWithValue( String text )
364 clickButtonWithValue( text, true );
367 public void clickButtonWithValue( String text, boolean wait )
369 assertButtonWithValuePresent( text );
371 if ( isElementPresent( "//button[@value='" + text + "']" ) )
373 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
377 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
381 public void clickSubmitWithLocator( String locator )
383 clickLinkWithLocator( locator );
386 public void clickSubmitWithLocator( String locator, boolean wait )
388 clickLinkWithLocator( locator, wait );
391 public void clickImgWithAlt( String alt )
393 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
396 public void clickLinkWithText( String text )
398 clickLinkWithText( text, true );
401 public void clickLinkWithText( String text, boolean wait )
403 clickLinkWithLocator( "link=" + text, wait );
406 public void clickLinkWithXPath( String xpath )
408 clickLinkWithXPath( xpath, true );
411 public void clickLinkWithXPath( String xpath, boolean wait )
413 clickLinkWithLocator( "xpath=" + xpath, wait );
416 public void clickLinkWithLocator( String locator )
418 clickLinkWithLocator( locator, true );
421 public void clickLinkWithLocator( String locator, boolean wait )
423 assertElementPresent( locator );
424 getSelenium().click( locator );
431 public void clickButtonWithLocator( String locator )
433 clickButtonWithLocator( locator, true );
436 public void clickButtonWithLocator( String locator, boolean wait )
438 assertElementPresent( locator );
439 getSelenium().click( locator );
446 public void setFieldValues( Map<String, String> fieldMap )
448 Map.Entry<String, String> entry;
450 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
452 entry = entries.next();
454 getSelenium().type( entry.getKey(), entry.getValue() );
458 public void setFieldValue( String fieldName, String value )
460 getSelenium().type( fieldName, value );
463 public void checkField( String locator )
465 getSelenium().check( locator );
468 public void uncheckField( String locator )
470 getSelenium().uncheck( locator );
473 public boolean isChecked( String locator )
475 return getSelenium().isChecked( locator );
478 public void assertIsChecked( String locator )
480 Assert.assertTrue( getSelenium().isChecked( locator ) );
483 public void assertIsNotChecked( String locator )
485 Assert.assertFalse( getSelenium().isChecked( locator ) );
488 public void assertXpathCount( String locator, int expectedCount )
490 int count = getSelenium().getXpathCount( locator ).intValue();
491 Assert.assertEquals( count, expectedCount );
494 public void assertElementValue( String locator, String expectedValue )
496 Assert.assertEquals( getSelenium().getValue( locator ), expectedValue );