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 java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Iterator;
27 import java.util.List;
29 import java.util.Properties;
30 import java.util.Map.Entry;
32 import org.apache.commons.io.IOUtils;
33 import org.testng.Assert;
35 import com.thoughtworks.selenium.DefaultSelenium;
36 import com.thoughtworks.selenium.Selenium;
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 {
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" ) );
61 //baseUrl = getProperty( "BASE_URL" );
62 maxWaitTimeInMs = getProperty( "MAX_WAIT_TIME_IN_MS" );
68 public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort )
71 this.baseUrl = baseUrl;
73 if ( getSelenium() == null )
75 DefaultSelenium s = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
77 s.setTimeout( maxWaitTimeInMs );
82 public static Selenium getSelenium()
84 return selenium == null ? null : selenium.get();
87 protected String getProperty( String key )
89 return p.getProperty( key );
92 protected String getEscapeProperty( String key )
94 InputStream input = this.getClass().getClassLoader().getResourceAsStream( "testng.properties" );
99 lines = IOUtils.readLines( input );
101 catch ( IOException e )
103 lines = new ArrayList<String>();
105 for ( String l : lines )
107 if ( l != null && l.startsWith( key ) )
109 int indexSeparator = l.indexOf( PROPERTIES_SEPARATOR );
110 value = l.substring( indexSeparator + 1 ).trim();
119 * Close selenium session. Called from AfterSuite method of sub-class
124 if ( getSelenium() != null )
126 getSelenium().stop();
127 selenium.set( null );
131 // *******************************************************
132 // Auxiliar methods. This method help us and simplify test.
133 // *******************************************************
135 public void assertFieldValue( String fieldValue, String fieldName )
137 assertElementPresent( fieldName );
138 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
141 public void assertPage( String title )
144 String actualTitle = getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
145 Assert.assertEquals( actualTitle, title );
148 public String getTitle()
150 return getSelenium().getTitle();
153 public String getHtmlContent()
155 return getSelenium().getHtmlSource();
158 public void assertTextPresent( String text )
160 Assert.assertTrue( getSelenium().isTextPresent( text ), "'" + text + "' isn't present." );
163 public void assertTextNotPresent( String text )
165 Assert.assertFalse( getSelenium().isTextPresent( text ), "'" + text + "' is present." );
168 public void assertElementPresent( String elementLocator )
170 Assert.assertTrue( isElementPresent( elementLocator ), "'" + elementLocator + "' isn't present." );
173 public void assertElementNotPresent( String elementLocator )
175 Assert.assertFalse( isElementPresent( elementLocator ), "'" + elementLocator + "' is present." );
178 public void assertLinkPresent( String text )
180 Assert.assertTrue( isElementPresent( "link=" + text ), "The link '" + text + "' isî't present." );
183 public void assertLinkNotPresent( String text )
185 Assert.assertFalse( isElementPresent( "link=" + text ), "The link('" + text + "' is present." );
188 public void assertImgWithAlt( String alt )
190 assertElementPresent( "/¯img[@alt='" + alt + "']" );
193 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
195 String locator = "//tr[" + row + "]/td[" + column + "]/";
196 locator += isALink ? "a/" : "";
197 locator += "img[@alt='" + alt + "']";
199 assertElementPresent( locator );
202 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
204 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
207 public boolean isTextPresent( String text )
209 return getSelenium().isTextPresent( text );
212 public boolean isLinkPresent( String text )
214 return isElementPresent( "link=" + text );
217 public boolean isElementPresent( String locator )
219 return getSelenium().isElementPresent( locator );
222 public void waitPage()
224 getSelenium().waitForPageToLoad( maxWaitTimeInMs );
227 public String getFieldValue( String fieldName )
229 return getSelenium().getValue( fieldName );
232 public String getCellValueFromTable( String tableElement, int row, int column )
234 return getSelenium().getTable( tableElement + "." + row + "." + column );
237 public void selectValue( String locator, String value )
239 getSelenium().select( locator, "label=" + value );
242 public void assertOptionPresent( String selectField, String[] options )
244 assertElementPresent( selectField );
245 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
246 List<String> expected = Arrays.asList( options );
247 List<String> present = Arrays.asList( optionsPresent );
248 Assert.assertTrue( present.containsAll( expected ), "Options expected are not included in present options" );
251 public void assertSelectedValue( String value, String fieldName )
253 assertElementPresent( fieldName );
254 String optionsPresent = getSelenium().getSelectedLabel( value );
255 Assert.assertEquals( optionsPresent, value );
260 clickLinkWithXPath( "//input[@type='submit']" );
263 public void assertButtonWithValuePresent( String text )
265 Assert.assertTrue( isButtonWithValuePresent( text ), "'" + text + "' button isn't present" );
268 public void assertButtonWithIdPresent( String id )
270 Assert.assertTrue( isButtonWithIdPresent( id ), "'Button with id =" + id + "' isn't present" );
273 public void assertButtonWithValueNotPresent( String text )
275 Assert.assertFalse( isButtonWithValuePresent( text ), "'" + text + "' button is present" );
278 public boolean isButtonWithValuePresent( String text )
280 return isElementPresent( "//button[@value='" + text + "']" )
281 || isElementPresent( "//input[@value='" + text + "']" );
284 public boolean isButtonWithIdPresent( String text )
286 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
289 public void clickButtonWithValue( String text )
291 clickButtonWithValue( text, true );
294 public void clickButtonWithValue( String text, boolean wait )
296 assertButtonWithValuePresent( text );
298 if ( isElementPresent( "//button[@value='" + text + "']" ) )
300 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
304 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
308 public void clickSubmitWithLocator( String locator )
310 clickLinkWithLocator( locator );
313 public void clickSubmitWithLocator( String locator, boolean wait )
315 clickLinkWithLocator( locator, wait );
318 public void clickImgWithAlt( String alt )
320 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
323 public void clickLinkWithText( String text )
325 clickLinkWithText( text, true );
328 public void clickLinkWithText( String text, boolean wait )
330 clickLinkWithLocator( "link=" + text, wait );
333 public void clickLinkWithXPath( String xpath )
335 clickLinkWithXPath( xpath, true );
338 public void clickLinkWithXPath( String xpath, boolean wait )
340 clickLinkWithLocator( "xpath=" + xpath, wait );
343 public void clickLinkWithLocator( String locator )
345 clickLinkWithLocator( locator, true );
348 public void clickLinkWithLocator( String locator, boolean wait )
350 assertElementPresent( locator );
351 getSelenium().click( locator );
358 public void setFieldValues( Map<String, String> fieldMap )
360 Map.Entry<String, String> entry;
362 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
364 entry = entries.next();
366 getSelenium().type( entry.getKey(), entry.getValue() );
370 public void setFieldValue( String fieldName, String value )
372 getSelenium().type( fieldName, value );
375 public void checkField( String locator )
377 getSelenium().check( locator );
380 public void uncheckField( String locator )
382 getSelenium().uncheck( locator );
385 public boolean isChecked( String locator )
387 return getSelenium().isChecked( locator );
390 public void assertIsChecked( String locator )
392 Assert.assertTrue( getSelenium().isChecked( locator ) );
395 public void assertIsNotChecked( String locator )
397 Assert.assertFalse( getSelenium().isChecked( locator ) );