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
23 import java.util.Arrays;
24 import java.util.Date;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.Properties;
29 import java.util.Map.Entry;
31 import org.codehaus.plexus.util.StringUtils;
33 import org.testng.Assert;
34 import com.thoughtworks.selenium.DefaultSelenium;
35 import com.thoughtworks.selenium.Selenium;
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 {
44 public static String baseUrl;
46 public static String maxWaitTimeInMs;
48 private static ThreadLocal<Selenium> selenium;
50 public static Properties p;
56 p.load( this.getClass().getClassLoader().getResourceAsStream( "testng.properties" ) );
58 baseUrl = p.getProperty( "BASE_URL" );
59 maxWaitTimeInMs = p.getProperty( "MAX_WAIT_TIME_IN_MS" );
61 String seleniumHost = p.getProperty( "SELENIUM_HOST" );
62 int seleniumPort = Integer.parseInt( ( p.getProperty( "SELENIUM_PORT" ) ) );
64 String seleniumBrowser = System.getProperty( "browser" );
65 if ( StringUtils.isEmpty( seleniumBrowser ) )
67 seleniumBrowser = p.getProperty( "SELENIUM_BROWSER" );
70 final Selenium s = new DefaultSelenium( seleniumHost, seleniumPort, seleniumBrowser, baseUrl );
71 selenium = new ThreadLocal<Selenium>()
73 protected Selenium initialValue()
78 getSelenium().start();
81 public static Selenium getSelenium()
83 return selenium.get();
92 // *******************************************************
93 // Auxiliar methods. This method help us and simplify test.
94 // *******************************************************
96 public void assertFieldValue( String fieldValue, String fieldName )
98 assertElementPresent( fieldName );
99 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
102 public void assertPage( String title )
104 Assert.assertEquals( getSelenium().getTitle(), title );
107 public String getTitle()
109 return getSelenium().getTitle();
112 public String getHtmlContent()
114 return getSelenium().getHtmlSource();
117 public void assertTextPresent( String text )
119 Assert.assertTrue( getSelenium().isTextPresent( text ), "'" + text + "' isn't present." );
122 public void assertTextNotPresent( String text )
124 Assert.assertFalse( getSelenium().isTextPresent( text ), "'" + text + "' is present." );
127 public void assertElementPresent( String elementLocator )
129 Assert.assertTrue( isElementPresent( elementLocator ), "'" + elementLocator + "' isn't present." );
132 public void assertElementNotPresent( String elementLocator )
134 Assert.assertFalse( isElementPresent( elementLocator ), "'" + elementLocator + "' is present." );
137 public void assertLinkPresent( String text )
139 Assert.assertTrue( isElementPresent( "link=" + text ), "The link '" + text + "' isî't present." );
142 public void assertLinkNotPresent( String text )
144 Assert.assertFalse( isElementPresent( "link=" + text ), "The link('" + text + "' is present." );
147 public void assertImgWithAlt( String alt )
149 assertElementPresent( "/¯img[@alt='" + alt + "']" );
152 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
154 String locator = "//tr[" + row + "]/td[" + column + "]/";
155 locator += isALink ? "a/" : "";
156 locator += "img[@alt='" + alt + "']";
158 assertElementPresent( locator );
161 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
163 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
166 public boolean isTextPresent( String text )
168 return getSelenium().isTextPresent( text );
171 public boolean isLinkPresent( String text )
173 return isElementPresent( "link=" + text );
176 public boolean isElementPresent( String locator )
178 return getSelenium().isElementPresent( locator );
181 public void waitPage()
183 getSelenium().waitForPageToLoad( maxWaitTimeInMs );
186 public String getFieldValue( String fieldName )
188 return getSelenium().getValue( fieldName );
191 public String getCellValueFromTable( String tableElement, int row, int column )
193 return getSelenium().getTable( tableElement + "." + row + "." + column );
196 public void selectValue( String locator, String value )
198 getSelenium().select( locator, "label=" + value );
201 public void assertOptionPresent( String selectField, String[] options )
203 assertElementPresent( selectField );
204 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
205 List<String> expected = Arrays.asList( options );
206 List<String> present = Arrays.asList( optionsPresent );
207 Assert.assertTrue( present.containsAll( expected ), "Options expected are not included in present options" );
210 public void assertSelectedValue( String value, String fieldName )
212 assertElementPresent( fieldName );
213 String optionsPresent = getSelenium().getSelectedLabel( value );
214 Assert.assertEquals( optionsPresent, value );
219 clickLinkWithXPath( "//input[@type='submit']" );
222 public void assertButtonWithValuePresent( String text )
224 Assert.assertTrue( isButtonWithValuePresent( text ), "'" + text + "' button isn't present" );
227 public void assertButtonWithIdPresent( String id )
229 Assert.assertTrue( isButtonWithIdPresent( id ), "'Button with id =" + id + "' isn't present" );
232 public void assertButtonWithValueNotPresent( String text )
234 Assert.assertFalse( isButtonWithValuePresent( text ), "'" + text + "' button is present" );
237 public boolean isButtonWithValuePresent( String text )
239 return isElementPresent( "//button[@value='" + text + "']" )
240 || isElementPresent( "//input[@value='" + text + "']" );
243 public boolean isButtonWithIdPresent( String text )
245 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
248 public void clickButtonWithValue( String text )
250 clickButtonWithValue( text, true );
253 public void clickButtonWithValue( String text, boolean wait )
255 assertButtonWithValuePresent( text );
257 if ( isElementPresent( "//button[@value='" + text + "']" ) )
259 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
263 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
267 public void clickSubmitWithLocator( String locator )
269 clickLinkWithLocator( locator );
272 public void clickSubmitWithLocator( String locator, boolean wait )
274 clickLinkWithLocator( locator, wait );
277 public void clickImgWithAlt( String alt )
279 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
282 public void clickLinkWithText( String text )
284 clickLinkWithText( text, true );
287 public void clickLinkWithText( String text, boolean wait )
289 clickLinkWithLocator( "link=" + text, wait );
292 public void clickLinkWithXPath( String xpath )
294 clickLinkWithXPath( xpath, true );
297 public void clickLinkWithXPath( String xpath, boolean wait )
299 clickLinkWithLocator( "xpath=" + xpath, wait );
302 public void clickLinkWithLocator( String locator )
304 clickLinkWithLocator( locator, true );
307 public void clickLinkWithLocator( String locator, boolean wait )
309 assertElementPresent( locator );
310 getSelenium().click( locator );
317 public void setFieldValues( Map<String, String> fieldMap )
319 Map.Entry<String, String> entry;
321 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
323 entry = entries.next();
325 getSelenium().type( entry.getKey(), entry.getValue() );
329 public void setFieldValue( String fieldName, String value )
331 getSelenium().type( fieldName, value );
334 public void checkField( String locator )
336 getSelenium().check( locator );
339 public void uncheckField( String locator )
341 getSelenium().uncheck( locator );
344 public boolean isChecked( String locator )
346 return getSelenium().isChecked( locator );
349 public void assertIsChecked( String locator )
351 Assert.assertTrue( getSelenium().isChecked( locator ) );
354 public void assertIsNotChecked( String locator )
356 Assert.assertFalse( getSelenium().isChecked( locator ) );