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.util.Arrays;
23 import java.util.Iterator;
24 import java.util.List;
26 import java.util.Properties;
27 import java.util.Map.Entry;
29 import org.codehaus.plexus.util.StringUtils;
31 import org.testng.Assert;
32 import com.thoughtworks.selenium.DefaultSelenium;
33 import com.thoughtworks.selenium.Selenium;
36 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
37 * @version $Id: AbstractSeleniumTestCase.java 761154 2009-04-02 03:31:19Z wsmoak $
40 public abstract class AbstractSeleniumTest {
42 public static String baseUrl;
44 public static String maxWaitTimeInMs;
46 private static ThreadLocal<Selenium> selenium;
48 public static Properties p;
54 p.load( this.getClass().getClassLoader().getResourceAsStream( "testng.properties" ) );
56 baseUrl = p.getProperty( "BASE_URL" );
57 maxWaitTimeInMs = p.getProperty( "MAX_WAIT_TIME_IN_MS" );
59 String seleniumHost = p.getProperty( "SELENIUM_HOST" );
60 int seleniumPort = Integer.parseInt( ( p.getProperty( "SELENIUM_PORT" ) ) );
62 String seleniumBrowser = System.getProperty( "browser" );
63 if ( StringUtils.isEmpty( seleniumBrowser ) )
65 seleniumBrowser = p.getProperty( "SELENIUM_BROWSER" );
68 final Selenium s = new DefaultSelenium( seleniumHost, seleniumPort, seleniumBrowser, baseUrl );
69 selenium = new ThreadLocal<Selenium>()
71 protected Selenium initialValue()
76 getSelenium().start();
79 protected static Selenium getSelenium()
81 return selenium.get();
90 // *******************************************************
91 // Auxiliar methods. This method help us and simplify test.
92 // *******************************************************
94 public void assertFieldValue( String fieldValue, String fieldName )
96 assertElementPresent( fieldName );
97 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
100 public void assertPage( String title )
102 Assert.assertEquals( getSelenium().getTitle(), title );
105 public String getTitle()
107 return getSelenium().getTitle();
110 public String getHtmlContent()
112 return getSelenium().getHtmlSource();
115 public void assertTextPresent( String text )
117 Assert.assertTrue( getSelenium().isTextPresent( text ), "'" + text + "' isn't present." );
120 public void assertTextNotPresent( String text )
122 Assert.assertFalse( getSelenium().isTextPresent( text ), "'" + text + "' is present." );
125 public void assertElementPresent( String elementLocator )
127 Assert.assertTrue( isElementPresent( elementLocator ), "'" + elementLocator + "' isn't present." );
130 public void assertElementNotPresent( String elementLocator )
132 Assert.assertFalse( isElementPresent( elementLocator ), "'" + elementLocator + "' is present." );
135 public void assertLinkPresent( String text )
137 Assert.assertTrue( isElementPresent( "link=" + text ), "The link '" + text + "' isî't present." );
140 public void assertLinkNotPresenu( String text )
142 Assert.assertFalse( isElementPresent( "link=" + text ), "The link('" + text + "' is present." );
145 public void assertImgWithAlt( String alt )
147 assertElementPresent( "/¯img[@alt='" + alt + "']" );
150 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
152 String locator = "//tr[" + row + "]/td[" + column + "]/";
153 locator += isALink ? "a/" : "";
154 locator += "img[@alt='" + alt + "']";
156 assertElementPresent( locator );
159 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
161 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
164 public boolean isTextPresent( String text )
166 return getSelenium().isTextPresent( text );
169 public boolean isLinkPresent( String text )
171 return isElementPresent( "link=" + text );
174 public boolean isElementPresent( String locator )
176 return getSelenium().isElementPresent( locator );
179 public void waitPage()
181 getSelenium().waitForPageToLoad( maxWaitTimeInMs );
184 public String getFieldValue( String fieldName )
186 return getSelenium().getValue( fieldName );
189 public String getCellValueFromTable( String tableElement, int row, int column )
191 return getSelenium().getTable( tableElement + "." + row + "." + column );
194 public void selectValue( String locator, String value )
196 getSelenium().select( locator, "label=" + value );
199 public void assertOptionPresent( String selectField, String[] options )
201 assertElementPresent( selectField );
202 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
203 List<String> expected = Arrays.asList( options );
204 List<String> present = Arrays.asList( optionsPresent );
205 Assert.assertTrue( present.containsAll( expected ), "Options expected are not included in present options" );
208 public void assertSelectedValue( String value, String fieldName )
210 assertElementPresent( fieldName );
211 String optionsPresent = getSelenium().getSelectedLabel( value );
212 Assert.assertEquals( optionsPresent, value );
217 clickLinkWithXPath( "//input[@type='submit']" );
220 public void assertButtonWithValuePresent( String text )
222 Assert.assertTrue( isButtonWithValuePresent( text ), "'" + text + "' button isn't present" );
225 public void assertButtonWithIdPresent( String id )
227 Assert.assertTrue( isButtonWithIdPresent( id ), "'Button with id =" + id + "' isn't present" );
230 public void assertButtonWithValueNotPresent( String text )
232 Assert.assertFalse( isButtonWithValuePresent( text ), "'" + text + "' button is present" );
235 public boolean isButtonWithValuePresent( String text )
237 return isElementPresent( "//button[@value='" + text + "']" )
238 || isElementPresent( "//input[@value='" + text + "']" );
241 public boolean isButtonWithIdPresent( String text )
243 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
246 public void clickButtonWithValue( String text )
248 clickButtonWithValue( text, true );
251 public void clickButtonWithValue( String text, boolean wait )
253 assertButtonWithValuePresent( text );
255 if ( isElementPresent( "//button[@value='" + text + "']" ) )
257 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
261 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
265 public void clickSubmitWithLocator( String locator )
267 clickLinkWithLocator( locator );
270 public void clickSubmitWithLocator( String locator, boolean wait )
272 clickLinkWithLocator( locator, wait );
275 public void clickImgWithAlt( String alt )
277 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
280 public void clickLinkWithText( String text )
282 clickLinkWithText( text, true );
285 public void clickLinkWithText( String text, boolean wait )
287 clickLinkWithLocator( "link=" + text, wait );
290 public void clickLinkWithXPath( String xpath )
292 clickLinkWithXPath( xpath, true );
295 public void clickLinkWithXPath( String xpath, boolean wait )
297 clickLinkWithLocator( "xpath=" + xpath, wait );
300 public void clickLinkWithLocator( String locator )
302 clickLinkWithLocator( locator, true );
305 public void clickLinkWithLocator( String locator, boolean wait )
307 assertElementPresent( locator );
308 getSelenium().click( locator );
315 public void setFieldValues( Map<String, String> fieldMap )
317 Map.Entry<String, String> entry;
319 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
321 entry = entries.next();
323 getSelenium().type( entry.getKey(), entry.getValue() );
327 public void setFieldValue( String fieldName, String value )
329 getSelenium().type( fieldName, value );
332 public void checkField( String locator )
334 getSelenium().check( locator );
337 public void uncheckField( String locator )
339 getSelenium().uncheck( locator );
342 public boolean isChecked( String locator )
344 return getSelenium().isChecked( locator );
347 public void assertIsChecked( String locator )
349 Assert.assertTrue( getSelenium().isChecked( locator ) );
352 public void assertIsNotChecked( String locator )
354 Assert.assertFalse( getSelenium().isChecked( locator ) );