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 com.thoughtworks.selenium.DefaultSelenium;
23 import com.thoughtworks.selenium.Selenium;
24 import org.apache.commons.io.IOUtils;
25 import org.testng.Assert;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Iterator;
32 import java.util.List;
34 import java.util.Map.Entry;
35 import java.util.Properties;
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" ) );
65 public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort, String maxWaitTimeInMs )
70 AbstractSeleniumTest.baseUrl = baseUrl;
71 AbstractSeleniumTest.maxWaitTimeInMs = maxWaitTimeInMs;
73 if ( getSelenium() == null )
75 DefaultSelenium s = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
77 s.setTimeout( maxWaitTimeInMs );
84 System.out.print( e.getMessage() );
89 public static Selenium getSelenium()
91 return selenium == null ? null : selenium.get();
94 protected String getProperty( String key )
96 return p.getProperty( key );
99 protected String getEscapeProperty( String key )
101 InputStream input = this.getClass().getClassLoader().getResourceAsStream( "testng.properties" );
106 lines = IOUtils.readLines( input );
108 catch ( IOException e )
110 lines = new ArrayList<String>();
112 for ( String l : lines )
114 if ( l != null && l.startsWith( key ) )
116 int indexSeparator = l.indexOf( PROPERTIES_SEPARATOR );
117 value = l.substring( indexSeparator + 1 ).trim();
125 * Close selenium session. Called from AfterSuite method of sub-class
130 if ( getSelenium() != null )
132 getSelenium().stop();
133 selenium.set( null );
137 // *******************************************************
138 // Auxiliar methods. This method help us and simplify test.
139 // *******************************************************
141 public void assertFieldValue( String fieldValue, String fieldName )
143 assertElementPresent( fieldName );
144 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
147 public void assertPage( String title )
149 Assert.assertEquals( getTitle(), title );
152 public String getTitle()
155 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
158 public String getHtmlContent()
160 return getSelenium().getHtmlSource();
163 public String getText( String locator )
165 return getSelenium().getText( locator );
168 public void assertTextPresent( String text )
170 Assert.assertTrue( getSelenium().isTextPresent( text ), "'" + text + "' isn't present." );
174 * one of text args must be in the page so use en and fr text (olamy use en locale :-) )
178 public void assertTextPresent( String... texts )
180 boolean present = false;
181 StringBuilder sb = new StringBuilder();
182 for ( String text : texts )
184 present = present || getSelenium().isTextPresent( text );
185 sb.append( " " + text + " " );
187 Assert.assertTrue( present, "'one of the following test " + sb.toString() + "' isn't present." );
190 public void assertTextNotPresent( String text )
192 Assert.assertFalse( getSelenium().isTextPresent( text ), "'" + text + "' is present." );
195 public void assertElementPresent( String elementLocator )
197 Assert.assertTrue( isElementPresent( elementLocator ), "'" + elementLocator + "' isn't present." );
200 public void assertElementNotPresent( String elementLocator )
202 Assert.assertFalse( isElementPresent( elementLocator ), "'" + elementLocator + "' is present." );
205 public void assertLinkPresent( String text )
207 Assert.assertTrue( isElementPresent( "link=" + text ), "The link '" + text + "' isn't present." );
210 public void assertLinkNotPresent( String text )
212 Assert.assertFalse( isElementPresent( "link=" + text ), "The link('" + text + "' is present." );
215 public void assertImgWithAlt( String alt )
217 assertElementPresent( "/¯img[@alt='" + alt + "']" );
220 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
222 String locator = "//tr[" + row + "]/td[" + column + "]/";
223 locator += isALink ? "a/" : "";
224 locator += "img[@alt='" + alt + "']";
226 assertElementPresent( locator );
229 public void assertImgWithAltNotPresent( String alt )
231 assertElementNotPresent( "/¯img[@alt='" + alt + "']" );
234 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
236 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
239 public boolean isTextPresent( String text )
241 return getSelenium().isTextPresent( text );
244 public boolean isLinkPresent( String text )
246 return isElementPresent( "link=" + text );
249 public boolean isElementPresent( String locator )
251 return getSelenium().isElementPresent( locator );
254 public boolean isElementVisible( String locator )
256 return getSelenium().isVisible( locator );
260 public void waitPage()
262 // TODO define a smaller maxWaitTimeJsInMs for wait javascript response for browser side validation
263 //getSelenium().w .wait( Long.parseLong( maxWaitTimeInMs ) );
264 //getSelenium().waitForPageToLoad( maxWaitTimeInMs );
265 // http://jira.openqa.org/browse/SRC-302
266 // those hack looks to break some tests :-(
267 // getSelenium().waitForCondition( "selenium.isElementPresent('document.body');", maxWaitTimeInMs );
268 //getSelenium().waitForCondition( "selenium.isElementPresent('footer');", maxWaitTimeInMs );
269 //getSelenium().waitForCondition( "selenium.browserbot.getCurrentWindow().document.getElementById('footer')",
270 // maxWaitTimeInMs );
271 // so the only hack is to not use a too small wait time
275 Thread.sleep( Long.parseLong( maxWaitTimeInMs ) );
277 catch ( InterruptedException e )
279 throw new RuntimeException( "issue on Thread.sleep : " + e.getMessage(), e );
283 public String getFieldValue( String fieldName )
285 return getSelenium().getValue( fieldName );
288 public String getCellValueFromTable( String tableElement, int row, int column )
290 return getSelenium().getTable( tableElement + "." + row + "." + column );
293 public void selectValue( String locator, String value )
295 getSelenium().select( locator, "label=" + value );
299 public void assertOptionPresent( String selectField, String[] options )
301 assertElementPresent( selectField );
302 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
303 List<String> expected = Arrays.asList( options );
304 List<String> present = Arrays.asList( optionsPresent );
305 Assert.assertTrue( present.containsAll( expected ), "Options expected are not included in present options" );
308 public void assertSelectedValue( String value, String fieldName )
310 assertElementPresent( fieldName );
311 String optionsPresent = getSelenium().getSelectedLabel( value );
312 Assert.assertEquals( optionsPresent, value );
317 clickLinkWithXPath( "//input[@type='submit']" );
320 public void assertButtonWithValuePresent( String text )
322 Assert.assertTrue( isButtonWithValuePresent( text ), "'" + text + "' button isn't present" );
325 public void assertButtonWithIdPresent( String id )
327 Assert.assertTrue( isButtonWithIdPresent( id ), "'Button with id =" + id + "' isn't present" );
330 public void assertButtonWithValueNotPresent( String text )
332 Assert.assertFalse( isButtonWithValuePresent( text ), "'" + text + "' button is present" );
335 public boolean isButtonWithValuePresent( String text )
337 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent(
338 "//input[@value='" + text + "']" );
341 public boolean isButtonWithIdPresent( String text )
343 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
346 public void clickButtonWithName( String text, boolean wait )
348 clickLinkWithXPath( "//input[@name='" + text + "']", wait );
351 public void clickButtonWithValue( String text )
353 clickButtonWithValue( text, true );
356 public void clickButtonWithValue( String text, boolean wait )
358 assertButtonWithValuePresent( text );
360 if ( isElementPresent( "//button[@value='" + text + "']" ) )
362 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
366 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
370 public void clickSubmitWithLocator( String locator )
372 clickLinkWithLocator( locator );
375 public void clickSubmitWithLocator( String locator, boolean wait )
377 clickLinkWithLocator( locator, wait );
380 public void clickImgWithAlt( String alt )
382 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
385 public void clickLinkWithText( String text )
387 clickLinkWithText( text, true );
390 public void clickLinkWithText( String text, boolean wait )
392 clickLinkWithLocator( "link=" + text, wait );
395 public void clickLinkWithXPath( String xpath )
397 clickLinkWithXPath( xpath, true );
400 public void clickLinkWithXPath( String xpath, boolean wait )
402 clickLinkWithLocator( "xpath=" + xpath, wait );
405 public void clickLinkWithLocator( String locator )
407 clickLinkWithLocator( locator, true );
410 public void clickLinkWithLocator( String locator, boolean wait )
412 assertElementPresent( locator );
413 getSelenium().click( locator );
420 public void clickButtonWithLocator( String locator )
422 clickButtonWithLocator( locator, true );
425 public void clickButtonWithLocator( String locator, boolean wait )
427 assertElementPresent( locator );
428 getSelenium().click( locator );
435 public void setFieldValues( Map<String, String> fieldMap )
437 Map.Entry<String, String> entry;
439 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
441 entry = entries.next();
443 getSelenium().type( entry.getKey(), entry.getValue() );
447 public void setFieldValue( String fieldName, String value )
449 getSelenium().type( fieldName, value );
452 public void checkField( String locator )
454 getSelenium().check( locator );
457 public void uncheckField( String locator )
459 getSelenium().uncheck( locator );
462 public boolean isChecked( String locator )
464 return getSelenium().isChecked( locator );
467 public void assertIsChecked( String locator )
469 Assert.assertTrue( getSelenium().isChecked( locator ) );
472 public void assertIsNotChecked( String locator )
474 Assert.assertFalse( getSelenium().isChecked( locator ) );
477 public void assertXpathCount( String locator, int expectedCount )
479 int count = getSelenium().getXpathCount( locator ).intValue();
480 Assert.assertEquals( count, expectedCount );
483 public void assertElementValue( String locator, String expectedValue )
485 Assert.assertEquals( getSelenium().getValue( locator ), expectedValue );