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.testng.Assert;
30 import com.thoughtworks.selenium.DefaultSelenium;
31 import com.thoughtworks.selenium.Selenium;
34 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
35 * @version $Id: AbstractSeleniumTestCase.java 761154 2009-04-02 03:31:19Z wsmoak $
38 public abstract class AbstractSeleniumTest {
40 public static String baseUrl;
42 public static String maxWaitTimeInMs;
44 private static ThreadLocal<Selenium> selenium;
46 public static Properties p;
52 p.load( this.getClass().getClassLoader().getResourceAsStream( "testng.properties" ) );
54 baseUrl = p.getProperty( "BASE_URL" );
55 maxWaitTimeInMs = p.getProperty( "MAX_WAIT_TIME_IN_MS" );
57 String seleniumHost = p.getProperty( "SELENIUM_HOST" );
58 int seleniumPort = Integer.parseInt( ( p.getProperty( "SELENIUM_PORT" ) ) );
59 String seleniumBrowser = p.getProperty( "SELENIUM_BROWSER" );
60 final Selenium s = new DefaultSelenium( seleniumHost, seleniumPort, seleniumBrowser, baseUrl );
61 selenium = new ThreadLocal<Selenium>()
63 protected Selenium initialValue()
68 getSelenium().start();
71 protected static Selenium getSelenium()
73 return selenium.get();
82 // *******************************************************
83 // Auxiliar methods. This method help us and simplify test.
84 // *******************************************************
86 public void assertFieldValue( String fieldValue, String fieldName )
88 assertElementPresent( fieldName );
89 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
92 public void assertPage( String title )
94 Assert.assertEquals( getSelenium().getTitle(), title );
97 public String getTitle()
99 return getSelenium().getTitle();
102 public String getHtmlContent()
104 return getSelenium().getHtmlSource();
107 public void assertTextPresent( String text )
109 Assert.assertTrue( getSelenium().isTextPresent( text ), "'" + text + "' isn't present." );
112 public void assertTextNotPresent( String text )
114 Assert.assertFalse( getSelenium().isTextPresent( text ), "'" + text + "' is present." );
117 public void assertElementPresent( String elementLocator )
119 Assert.assertTrue( isElementPresent( elementLocator ), "'" + elementLocator + "' isn't present." );
122 public void assertElementNotPresent( String elementLocator )
124 Assert.assertFalse( isElementPresent( elementLocator ), "'" + elementLocator + "' is present." );
127 public void assertLinkPresent( String text )
129 Assert.assertTrue( isElementPresent( "link=" + text ), "The link '" + text + "' isî't present." );
132 public void assertLinkNotPresenu( String text )
134 Assert.assertFalse( isElementPresent( "link=" + text ), "The link('" + text + "' is present." );
137 public void assertImgWithAlt( String alt )
139 assertElementPresent( "/¯img[@alt='" + alt + "']" );
142 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
144 String locator = "//tr[" + row + "]/td[" + column + "]/";
145 locator += isALink ? "a/" : "";
146 locator += "img[@alt='" + alt + "']";
148 assertElementPresent( locator );
151 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
153 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
156 public boolean isTextPresent( String text )
158 return getSelenium().isTextPresent( text );
161 public boolean isLinkPresent( String text )
163 return isElementPresent( "link=" + text );
166 public boolean isElementPresent( String locator )
168 return getSelenium().isElementPresent( locator );
171 public void waitPage()
173 getSelenium().waitForPageToLoad( maxWaitTimeInMs );
176 public String getFieldValue( String fieldName )
178 return getSelenium().getValue( fieldName );
181 public String getCellValueFromTable( String tableElement, int row, int column )
183 return getSelenium().getTable( tableElement + "." + row + "." + column );
186 public void selectValue( String locator, String value )
188 getSelenium().select( locator, "label=" + value );
191 public void assertOptionPresent( String selectField, String[] options )
193 assertElementPresent( selectField );
194 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
195 List<String> expected = Arrays.asList( options );
196 List<String> present = Arrays.asList( optionsPresent );
197 Assert.assertTrue( present.containsAll( expected ), "Options expected are not included in present options" );
200 public void assertSelectedValue( String value, String fieldName )
202 assertElementPresent( fieldName );
203 String optionsPresent = getSelenium().getSelectedLabel( value );
204 Assert.assertEquals( optionsPresent, value );
209 clickLinkWithXPath( "//input[@type='submit']" );
212 public void assertButtonWithValuePresent( String text )
214 Assert.assertTrue( isButtonWithValuePresent( text ), "'" + text + "' button isn't present" );
217 public void assertButtonWithIdPresent( String id )
219 Assert.assertTrue( isButtonWithIdPresent( id ), "'Button with id =" + id + "' isn't present" );
222 public void assertButtonWithValueNotPresent( String text )
224 Assert.assertFalse( isButtonWithValuePresent( text ), "'" + text + "' button is present" );
227 public boolean isButtonWithValuePresent( String text )
229 return isElementPresent( "//button[@value='" + text + "']" )
230 || isElementPresent( "//input[@value='" + text + "']" );
233 public boolean isButtonWithIdPresent( String text )
235 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
238 public void clickButtonWithValue( String text )
240 clickButtonWithValue( text, true );
243 public void clickButtonWithValue( String text, boolean wait )
245 assertButtonWithValuePresent( text );
247 if ( isElementPresent( "//button[@value='" + text + "']" ) )
249 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
253 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
257 public void clickSubmitWithLocator( String locator )
259 clickLinkWithLocator( locator );
262 public void clickSubmitWithLocator( String locator, boolean wait )
264 clickLinkWithLocator( locator, wait );
267 public void clickImgWithAlt( String alt )
269 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
272 public void clickLinkWithText( String text )
274 clickLinkWithText( text, true );
277 public void clickLinkWithText( String text, boolean wait )
279 clickLinkWithLocator( "link=" + text, wait );
282 public void clickLinkWithXPath( String xpath )
284 clickLinkWithXPath( xpath, true );
287 public void clickLinkWithXPath( String xpath, boolean wait )
289 clickLinkWithLocator( "xpath=" + xpath, wait );
292 public void clickLinkWithLocator( String locator )
294 clickLinkWithLocator( locator, true );
297 public void clickLinkWithLocator( String locator, boolean wait )
299 assertElementPresent( locator );
300 getSelenium().click( locator );
307 public void setFieldValues( Map<String, String> fieldMap )
309 Map.Entry<String, String> entry;
311 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
313 entry = entries.next();
315 getSelenium().type( entry.getKey(), entry.getValue() );
319 public void setFieldValue( String fieldName, String value )
321 getSelenium().type( fieldName, value );
324 public void checkField( String locator )
326 getSelenium().check( locator );
329 public void uncheckField( String locator )
331 getSelenium().uncheck( locator );
334 public boolean isChecked( String locator )
336 return getSelenium().isChecked( locator );
339 public void assertIsChecked( String locator )
341 Assert.assertTrue( getSelenium().isChecked( locator ) );
344 public void assertIsNotChecked( String locator )
346 Assert.assertFalse( getSelenium().isChecked( locator ) );