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 )
143 Assert.assertEquals( getTitle(), title );
146 public String getTitle()
149 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
152 public String getHtmlContent()
154 return getSelenium().getHtmlSource();
157 public void assertTextPresent( String text )
159 Assert.assertTrue( getSelenium().isTextPresent( text ), "'" + text + "' isn't present." );
162 public void assertTextNotPresent( String text )
164 Assert.assertFalse( getSelenium().isTextPresent( text ), "'" + text + "' is present." );
167 public void assertElementPresent( String elementLocator )
169 Assert.assertTrue( isElementPresent( elementLocator ), "'" + elementLocator + "' isn't present." );
172 public void assertElementNotPresent( String elementLocator )
174 Assert.assertFalse( isElementPresent( elementLocator ), "'" + elementLocator + "' is present." );
177 public void assertLinkPresent( String text )
179 Assert.assertTrue( isElementPresent( "link=" + text ), "The link '" + text + "' isî't present." );
182 public void assertLinkNotPresent( String text )
184 Assert.assertFalse( isElementPresent( "link=" + text ), "The link('" + text + "' is present." );
187 public void assertImgWithAlt( String alt )
189 assertElementPresent( "/¯img[@alt='" + alt + "']" );
192 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
194 String locator = "//tr[" + row + "]/td[" + column + "]/";
195 locator += isALink ? "a/" : "";
196 locator += "img[@alt='" + alt + "']";
198 assertElementPresent( locator );
201 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
203 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
206 public boolean isTextPresent( String text )
208 return getSelenium().isTextPresent( text );
211 public boolean isLinkPresent( String text )
213 return isElementPresent( "link=" + text );
216 public boolean isElementPresent( String locator )
218 return getSelenium().isElementPresent( locator );
221 public void waitPage()
223 getSelenium().waitForPageToLoad( maxWaitTimeInMs );
226 public String getFieldValue( String fieldName )
228 return getSelenium().getValue( fieldName );
231 public String getCellValueFromTable( String tableElement, int row, int column )
233 return getSelenium().getTable( tableElement + "." + row + "." + column );
236 public void selectValue( String locator, String value )
238 getSelenium().select( locator, "label=" + value );
241 public void assertOptionPresent( String selectField, String[] options )
243 assertElementPresent( selectField );
244 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
245 List<String> expected = Arrays.asList( options );
246 List<String> present = Arrays.asList( optionsPresent );
247 Assert.assertTrue( present.containsAll( expected ), "Options expected are not included in present options" );
250 public void assertSelectedValue( String value, String fieldName )
252 assertElementPresent( fieldName );
253 String optionsPresent = getSelenium().getSelectedLabel( value );
254 Assert.assertEquals( optionsPresent, value );
259 clickLinkWithXPath( "//input[@type='submit']" );
262 public void assertButtonWithValuePresent( String text )
264 Assert.assertTrue( isButtonWithValuePresent( text ), "'" + text + "' button isn't present" );
267 public void assertButtonWithIdPresent( String id )
269 Assert.assertTrue( isButtonWithIdPresent( id ), "'Button with id =" + id + "' isn't present" );
272 public void assertButtonWithValueNotPresent( String text )
274 Assert.assertFalse( isButtonWithValuePresent( text ), "'" + text + "' button is present" );
277 public boolean isButtonWithValuePresent( String text )
279 return isElementPresent( "//button[@value='" + text + "']" )
280 || isElementPresent( "//input[@value='" + text + "']" );
283 public boolean isButtonWithIdPresent( String text )
285 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
288 public void clickButtonWithValue( String text )
290 clickButtonWithValue( text, true );
293 public void clickButtonWithValue( String text, boolean wait )
295 assertButtonWithValuePresent( text );
297 if ( isElementPresent( "//button[@value='" + text + "']" ) )
299 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
303 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
307 public void clickSubmitWithLocator( String locator )
309 clickLinkWithLocator( locator );
312 public void clickSubmitWithLocator( String locator, boolean wait )
314 clickLinkWithLocator( locator, wait );
317 public void clickImgWithAlt( String alt )
319 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
322 public void clickLinkWithText( String text )
324 clickLinkWithText( text, true );
327 public void clickLinkWithText( String text, boolean wait )
329 clickLinkWithLocator( "link=" + text, wait );
332 public void clickLinkWithXPath( String xpath )
334 clickLinkWithXPath( xpath, true );
337 public void clickLinkWithXPath( String xpath, boolean wait )
339 clickLinkWithLocator( "xpath=" + xpath, wait );
342 public void clickLinkWithLocator( String locator )
344 clickLinkWithLocator( locator, true );
347 public void clickLinkWithLocator( String locator, boolean wait )
349 assertElementPresent( locator );
350 getSelenium().click( locator );
357 public void setFieldValues( Map<String, String> fieldMap )
359 Map.Entry<String, String> entry;
361 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
363 entry = entries.next();
365 getSelenium().type( entry.getKey(), entry.getValue() );
369 public void setFieldValue( String fieldName, String value )
371 getSelenium().type( fieldName, value );
374 public void checkField( String locator )
376 getSelenium().check( locator );
379 public void uncheckField( String locator )
381 getSelenium().uncheck( locator );
384 public boolean isChecked( String locator )
386 return getSelenium().isChecked( locator );
389 public void assertIsChecked( String locator )
391 Assert.assertTrue( getSelenium().isChecked( locator ) );
394 public void assertIsNotChecked( String locator )
396 Assert.assertFalse( getSelenium().isChecked( locator ) );