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 void waitPage()
256 // TODO define a smaller maxWaitTimeJsInMs for wait javascript response for browser side validation
257 getSelenium().waitForPageToLoad( maxWaitTimeInMs );
258 // http://jira.openqa.org/browse/SRC-302
259 // those hack looks to break some tests :-(
260 // getSelenium().waitForCondition( "selenium.isElementPresent('document.body');", maxWaitTimeInMs );
261 //getSelenium().waitForCondition( "selenium.isElementPresent('footer');", maxWaitTimeInMs );
262 //getSelenium().waitForCondition( "selenium.browserbot.getCurrentWindow().document.getElementById('footer')",
263 // maxWaitTimeInMs );
264 // so the only hack is to not use a too small wait time
268 Thread.sleep( 1000 );
270 catch ( InterruptedException e )
272 throw new RuntimeException( "issue on Thread.sleep : " + e.getMessage(), e );
276 public String getFieldValue( String fieldName )
278 return getSelenium().getValue( fieldName );
281 public String getCellValueFromTable( String tableElement, int row, int column )
283 return getSelenium().getTable( tableElement + "." + row + "." + column );
286 public void selectValue( String locator, String value )
288 getSelenium().select( locator, "label=" + value );
292 public void assertOptionPresent( String selectField, String[] options )
294 assertElementPresent( selectField );
295 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
296 List<String> expected = Arrays.asList( options );
297 List<String> present = Arrays.asList( optionsPresent );
298 Assert.assertTrue( present.containsAll( expected ), "Options expected are not included in present options" );
301 public void assertSelectedValue( String value, String fieldName )
303 assertElementPresent( fieldName );
304 String optionsPresent = getSelenium().getSelectedLabel( value );
305 Assert.assertEquals( optionsPresent, value );
310 clickLinkWithXPath( "//input[@type='submit']" );
313 public void assertButtonWithValuePresent( String text )
315 Assert.assertTrue( isButtonWithValuePresent( text ), "'" + text + "' button isn't present" );
318 public void assertButtonWithIdPresent( String id )
320 Assert.assertTrue( isButtonWithIdPresent( id ), "'Button with id =" + id + "' isn't present" );
323 public void assertButtonWithValueNotPresent( String text )
325 Assert.assertFalse( isButtonWithValuePresent( text ), "'" + text + "' button is present" );
328 public boolean isButtonWithValuePresent( String text )
330 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent(
331 "//input[@value='" + text + "']" );
334 public boolean isButtonWithIdPresent( String text )
336 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
339 public void clickButtonWithName( String text, boolean wait )
341 clickLinkWithXPath( "//input[@name='" + text + "']", wait );
344 public void clickButtonWithValue( String text )
346 clickButtonWithValue( text, true );
349 public void clickButtonWithValue( String text, boolean wait )
351 assertButtonWithValuePresent( text );
353 if ( isElementPresent( "//button[@value='" + text + "']" ) )
355 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
359 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
363 public void clickSubmitWithLocator( String locator )
365 clickLinkWithLocator( locator );
368 public void clickSubmitWithLocator( String locator, boolean wait )
370 clickLinkWithLocator( locator, wait );
373 public void clickImgWithAlt( String alt )
375 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
378 public void clickLinkWithText( String text )
380 clickLinkWithText( text, true );
383 public void clickLinkWithText( String text, boolean wait )
385 clickLinkWithLocator( "link=" + text, wait );
388 public void clickLinkWithXPath( String xpath )
390 clickLinkWithXPath( xpath, true );
393 public void clickLinkWithXPath( String xpath, boolean wait )
395 clickLinkWithLocator( "xpath=" + xpath, wait );
398 public void clickLinkWithLocator( String locator )
400 clickLinkWithLocator( locator, true );
403 public void clickLinkWithLocator( String locator, boolean wait )
405 assertElementPresent( locator );
406 getSelenium().click( locator );
413 public void clickButtonWithLocator( String locator )
415 clickButtonWithLocator( locator, true );
418 public void clickButtonWithLocator( String locator, boolean wait )
420 assertElementPresent( locator );
421 getSelenium().click( locator );
428 public void setFieldValues( Map<String, String> fieldMap )
430 Map.Entry<String, String> entry;
432 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
434 entry = entries.next();
436 getSelenium().type( entry.getKey(), entry.getValue() );
440 public void setFieldValue( String fieldName, String value )
442 getSelenium().type( fieldName, value );
445 public void checkField( String locator )
447 getSelenium().check( locator );
450 public void uncheckField( String locator )
452 getSelenium().uncheck( locator );
455 public boolean isChecked( String locator )
457 return getSelenium().isChecked( locator );
460 public void assertIsChecked( String locator )
462 Assert.assertTrue( getSelenium().isChecked( locator ) );
465 public void assertIsNotChecked( String locator )
467 Assert.assertFalse( getSelenium().isChecked( locator ) );
470 public void assertXpathCount( String locator, int expectedCount )
472 int count = getSelenium().getXpathCount( locator ).intValue();
473 Assert.assertEquals( count, expectedCount );
476 public void assertElementValue( String locator, String expectedValue )
478 Assert.assertEquals( getSelenium().getValue( locator ), expectedValue );