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.Map.Entry;
30 import java.util.Properties;
32 import com.thoughtworks.selenium.DefaultSelenium;
33 import com.thoughtworks.selenium.Selenium;
34 import org.apache.commons.io.IOUtils;
35 import org.testng.Assert;
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" ) );
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();
118 * Close selenium session. Called from AfterSuite method of sub-class
123 if ( getSelenium() != null )
125 getSelenium().stop();
126 selenium.set( null );
130 // *******************************************************
131 // Auxiliar methods. This method help us and simplify test.
132 // *******************************************************
134 public void assertFieldValue( String fieldValue, String fieldName )
136 assertElementPresent( fieldName );
137 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
140 public void assertPage( String title )
142 Assert.assertEquals( getTitle(), title );
145 public String getTitle()
148 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
151 public String getHtmlContent()
153 return getSelenium().getHtmlSource();
156 public String getText( String locator )
158 return getSelenium().getText( locator );
161 public void assertTextPresent( String text )
163 Assert.assertTrue( getSelenium().isTextPresent( text ), "'" + text + "' isn't present." );
166 public void assertTextNotPresent( String text )
168 Assert.assertFalse( getSelenium().isTextPresent( text ), "'" + text + "' is present." );
171 public void assertElementPresent( String elementLocator )
173 Assert.assertTrue( isElementPresent( elementLocator ), "'" + elementLocator + "' isn't present." );
176 public void assertElementNotPresent( String elementLocator )
178 Assert.assertFalse( isElementPresent( elementLocator ), "'" + elementLocator + "' is present." );
181 public void assertLinkPresent( String text )
183 Assert.assertTrue( isElementPresent( "link=" + text ), "The link '" + text + "' isn't present." );
186 public void assertLinkNotPresent( String text )
188 Assert.assertFalse( isElementPresent( "link=" + text ), "The link('" + text + "' is present." );
191 public void assertImgWithAlt( String alt )
193 assertElementPresent( "/¯img[@alt='" + alt + "']" );
196 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
198 String locator = "//tr[" + row + "]/td[" + column + "]/";
199 locator += isALink ? "a/" : "";
200 locator += "img[@alt='" + alt + "']";
202 assertElementPresent( locator );
205 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
207 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
210 public boolean isTextPresent( String text )
212 return getSelenium().isTextPresent( text );
215 public boolean isLinkPresent( String text )
217 return isElementPresent( "link=" + text );
220 public boolean isElementPresent( String locator )
222 return getSelenium().isElementPresent( locator );
225 public void waitPage()
227 getSelenium().waitForPageToLoad( maxWaitTimeInMs );
230 public String getFieldValue( String fieldName )
232 return getSelenium().getValue( fieldName );
235 public String getCellValueFromTable( String tableElement, int row, int column )
237 return getSelenium().getTable( tableElement + "." + row + "." + column );
240 public void selectValue( String locator, String value )
242 getSelenium().select( locator, "label=" + value );
245 public void assertOptionPresent( String selectField, String[] options )
247 assertElementPresent( selectField );
248 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
249 List<String> expected = Arrays.asList( options );
250 List<String> present = Arrays.asList( optionsPresent );
251 Assert.assertTrue( present.containsAll( expected ), "Options expected are not included in present options" );
254 public void assertSelectedValue( String value, String fieldName )
256 assertElementPresent( fieldName );
257 String optionsPresent = getSelenium().getSelectedLabel( value );
258 Assert.assertEquals( optionsPresent, value );
263 clickLinkWithXPath( "//input[@type='submit']" );
266 public void assertButtonWithValuePresent( String text )
268 Assert.assertTrue( isButtonWithValuePresent( text ), "'" + text + "' button isn't present" );
271 public void assertButtonWithIdPresent( String id )
273 Assert.assertTrue( isButtonWithIdPresent( id ), "'Button with id =" + id + "' isn't present" );
276 public void assertButtonWithValueNotPresent( String text )
278 Assert.assertFalse( isButtonWithValuePresent( text ), "'" + text + "' button is present" );
281 public boolean isButtonWithValuePresent( String text )
283 return isElementPresent( "//button[@value='" + text + "']" )
284 || isElementPresent( "//input[@value='" + text + "']" );
287 public boolean isButtonWithIdPresent( String text )
289 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
292 public void clickButtonWithValue( String text )
294 clickButtonWithValue( text, true );
297 public void clickButtonWithValue( String text, boolean wait )
299 assertButtonWithValuePresent( text );
301 if ( isElementPresent( "//button[@value='" + text + "']" ) )
303 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
307 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
311 public void clickSubmitWithLocator( String locator )
313 clickLinkWithLocator( locator );
316 public void clickSubmitWithLocator( String locator, boolean wait )
318 clickLinkWithLocator( locator, wait );
321 public void clickImgWithAlt( String alt )
323 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
326 public void clickLinkWithText( String text )
328 clickLinkWithText( text, true );
331 public void clickLinkWithText( String text, boolean wait )
333 clickLinkWithLocator( "link=" + text, wait );
336 public void clickLinkWithXPath( String xpath )
338 clickLinkWithXPath( xpath, true );
341 public void clickLinkWithXPath( String xpath, boolean wait )
343 clickLinkWithLocator( "xpath=" + xpath, wait );
346 public void clickLinkWithLocator( String locator )
348 clickLinkWithLocator( locator, true );
351 public void clickLinkWithLocator( String locator, boolean wait )
353 assertElementPresent( locator );
354 getSelenium().click( locator );
361 public void setFieldValues( Map<String, String> fieldMap )
363 Map.Entry<String, String> entry;
365 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
367 entry = entries.next();
369 getSelenium().type( entry.getKey(), entry.getValue() );
373 public void setFieldValue( String fieldName, String value )
375 getSelenium().type( fieldName, value );
378 public void checkField( String locator )
380 getSelenium().check( locator );
383 public void uncheckField( String locator )
385 getSelenium().uncheck( locator );
388 public boolean isChecked( String locator )
390 return getSelenium().isChecked( locator );
393 public void assertIsChecked( String locator )
395 Assert.assertTrue( getSelenium().isChecked( locator ) );
398 public void assertIsNotChecked( String locator )
400 Assert.assertFalse( getSelenium().isChecked( locator ) );