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 assertImgWithAltNotPresent( String alt )
207 assertElementNotPresent( "/¯img[@alt='" + alt + "']" );
210 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
212 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
215 public boolean isTextPresent( String text )
217 return getSelenium().isTextPresent( text );
220 public boolean isLinkPresent( String text )
222 return isElementPresent( "link=" + text );
225 public boolean isElementPresent( String locator )
227 return getSelenium().isElementPresent( locator );
230 public void waitPage()
232 getSelenium().waitForPageToLoad( maxWaitTimeInMs );
235 public String getFieldValue( String fieldName )
237 return getSelenium().getValue( fieldName );
240 public String getCellValueFromTable( String tableElement, int row, int column )
242 return getSelenium().getTable( tableElement + "." + row + "." + column );
245 public void selectValue( String locator, String value )
247 getSelenium().select( locator, "label=" + value );
250 public void assertOptionPresent( String selectField, String[] options )
252 assertElementPresent( selectField );
253 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
254 List<String> expected = Arrays.asList( options );
255 List<String> present = Arrays.asList( optionsPresent );
256 Assert.assertTrue( present.containsAll( expected ), "Options expected are not included in present options" );
259 public void assertSelectedValue( String value, String fieldName )
261 assertElementPresent( fieldName );
262 String optionsPresent = getSelenium().getSelectedLabel( value );
263 Assert.assertEquals( optionsPresent, value );
268 clickLinkWithXPath( "//input[@type='submit']" );
271 public void assertButtonWithValuePresent( String text )
273 Assert.assertTrue( isButtonWithValuePresent( text ), "'" + text + "' button isn't present" );
276 public void assertButtonWithIdPresent( String id )
278 Assert.assertTrue( isButtonWithIdPresent( id ), "'Button with id =" + id + "' isn't present" );
281 public void assertButtonWithValueNotPresent( String text )
283 Assert.assertFalse( isButtonWithValuePresent( text ), "'" + text + "' button is present" );
286 public boolean isButtonWithValuePresent( String text )
288 return isElementPresent( "//button[@value='" + text + "']" )
289 || isElementPresent( "//input[@value='" + text + "']" );
292 public boolean isButtonWithIdPresent( String text )
294 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
297 public void clickButtonWithValue( String text )
299 clickButtonWithValue( text, true );
302 public void clickButtonWithValue( String text, boolean wait )
304 assertButtonWithValuePresent( text );
306 if ( isElementPresent( "//button[@value='" + text + "']" ) )
308 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
312 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
316 public void clickSubmitWithLocator( String locator )
318 clickLinkWithLocator( locator );
321 public void clickSubmitWithLocator( String locator, boolean wait )
323 clickLinkWithLocator( locator, wait );
326 public void clickImgWithAlt( String alt )
328 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
331 public void clickLinkWithText( String text )
333 clickLinkWithText( text, true );
336 public void clickLinkWithText( String text, boolean wait )
338 clickLinkWithLocator( "link=" + text, wait );
341 public void clickLinkWithXPath( String xpath )
343 clickLinkWithXPath( xpath, true );
346 public void clickLinkWithXPath( String xpath, boolean wait )
348 clickLinkWithLocator( "xpath=" + xpath, wait );
351 public void clickLinkWithLocator( String locator )
353 clickLinkWithLocator( locator, true );
356 public void clickLinkWithLocator( String locator, boolean wait )
358 assertElementPresent( locator );
359 getSelenium().click( locator );
366 public void setFieldValues( Map<String, String> fieldMap )
368 Map.Entry<String, String> entry;
370 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
372 entry = entries.next();
374 getSelenium().type( entry.getKey(), entry.getValue() );
378 public void setFieldValue( String fieldName, String value )
380 getSelenium().type( fieldName, value );
383 public void checkField( String locator )
385 getSelenium().check( locator );
388 public void uncheckField( String locator )
390 getSelenium().uncheck( locator );
393 public boolean isChecked( String locator )
395 return getSelenium().isChecked( locator );
398 public void assertIsChecked( String locator )
400 Assert.assertTrue( getSelenium().isChecked( locator ) );
403 public void assertIsNotChecked( String locator )
405 Assert.assertFalse( getSelenium().isChecked( locator ) );