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.archiva.web.test.tools.ArchivaSeleniumExecutionRule;
25 import org.junit.Assert;
26 import org.junit.Rule;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.text.SimpleDateFormat;
32 import java.util.Arrays;
33 import java.util.Date;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.Map.Entry;
38 import java.util.Properties;
39 import org.apache.commons.io.FileUtils;
42 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
46 public abstract class AbstractSeleniumTest
50 public ArchivaSeleniumExecutionRule archivaSeleniumExecutionRule = new ArchivaSeleniumExecutionRule();
52 public String browser = System.getProperty( "browser" );
54 public String baseUrl =
55 "http://localhost:" + System.getProperty( "tomcat.maven.http.port" ) + "/archiva/index.html?request_lang=en";
57 public int maxWaitTimeInMs = Integer.getInteger( "maxWaitTimeInMs" );
59 public String seleniumHost = System.getProperty( "seleniumHost", "localhost" );
61 public int seleniumPort = Integer.getInteger( "seleniumPort", 4444 );
63 private Selenium selenium = null;
68 * this method is called by the Rule before executing a test
76 p.load( this.getClass().getClassLoader().getResourceAsStream( "test.properties" ) );
78 Properties tomcatPortProperties = new Properties();
79 tomcatPortProperties.load(
80 new FileInputStream( new File( System.getProperty( "tomcat.propertiesPortFilePath" ) ) ) );
82 int tomcatPort = Integer.parseInt( tomcatPortProperties.getProperty( "tomcat.maven.http.port" ) );
84 baseUrl = "http://localhost:" + tomcatPort + "/archiva/index.html?request_lang=en";
86 open( baseUrl, browser, seleniumHost, seleniumPort, maxWaitTimeInMs );
87 archivaSeleniumExecutionRule.selenium = selenium;
92 * this method is called by the Rule after executing a tests
96 if ( getSelenium() != null )
103 * Initialize selenium
105 public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort, int maxWaitTimeInMs )
110 if ( getSelenium() == null )
112 selenium = new DefaultSelenium( seleniumHost, seleniumPort, browser, baseUrl );
114 selenium.setTimeout( Integer.toString( maxWaitTimeInMs ) );
117 catch ( Exception e )
120 System.out.print( e.getMessage() );
125 public void assertAdminCreated()
128 initializeArchiva( baseUrl, browser, maxWaitTimeInMs, seleniumHost, seleniumPort );
131 public void initializeArchiva( String baseUrl, String browser, int maxWaitTimeInMs, String seleniumHost,
136 open( baseUrl, browser, seleniumHost, seleniumPort, maxWaitTimeInMs );
138 getSelenium().open( baseUrl );
142 // if not admin user created create one
143 if ( isElementVisible( "create-admin-link" ) )
145 Assert.assertFalse( getSelenium().isVisible( "login-link-a" ) );
146 Assert.assertFalse( getSelenium().isVisible( "register-link-a" ) );
147 // skygo need to set to true for passing is that work as expected ?
148 clickLinkWithLocator( "create-admin-link-a", true );
150 String fullname = getProperty( "ADMIN_FULLNAME" );
151 String username = getAdminUsername();
152 String mail = getProperty( "ADMIN_EMAIL" );
153 String password = getProperty( "ADMIN_PASSWORD" );
154 submitAdminData( fullname, mail, password );
155 assertUserLoggedIn( username );
156 clickLinkWithLocator( "logout-link-a" );
160 Assert.assertTrue( getSelenium().isVisible( "login-link-a" ) );
161 Assert.assertTrue( getSelenium().isVisible( "register-link-a" ) );
162 login( getAdminUsername(), getAdminPassword() );
167 public Selenium getSelenium()
172 protected String getProperty( String key )
174 return p.getProperty( key );
177 public String getAdminUsername()
179 String adminUsername = getProperty( "ADMIN_USERNAME" );
180 return adminUsername;
183 public String getAdminPassword()
185 String adminPassword = getProperty( "ADMIN_PASSWORD" );
186 return adminPassword;
189 public void submitAdminData( String fullname, String email, String password )
191 setFieldValue( "fullname", fullname );
192 setFieldValue( "email", email );
193 setFieldValue( "password", password );
194 setFieldValue( "confirmPassword", password );
195 clickButtonWithLocator( "user-create-form-register-button" );
198 public void login( String username, String password )
200 login( username, password, true, "Login Page" );
203 public void login( String username, String password, boolean valid, String assertReturnPage )
205 if ( isElementVisible( "login-link-a" ) )//isElementPresent( "loginLink" ) )
209 submitLoginPage( username, password, false, valid, assertReturnPage );
213 assertUserLoggedIn( username );
218 public void goToLoginPage()
220 getSelenium().open( baseUrl );
222 // are we already logged in ?
223 if ( isElementVisible( "logout-link" ) ) //isElementPresent( "logoutLink" ) )
226 clickLinkWithLocator( "logout-link-a", false );
227 clickLinkWithLocator( "login-link-a" );
229 else if ( isElementVisible( "login-link-a" ) )
231 clickLinkWithLocator( "login-link-a" );
237 public void assertLoginModal()
239 assertElementPresent( "user-login-form" );
240 Assert.assertTrue( isElementVisible( "register-link" ) );
241 assertElementPresent( "user-login-form-username" );
242 assertElementPresent( "user-login-form-password" );
243 assertButtonWithIdPresent( "modal-login-ok" );
247 public void submitLoginPage( String username, String password )
249 submitLoginPage( username, password, false, true, "Login Page" );
252 public void submitLoginPage( String username, String password, boolean validUsernamePassword )
254 submitLoginPage( username, password, false, validUsernamePassword, "Login Page" );
257 public void submitLoginPage( String username, String password, boolean rememberMe, boolean validUsernamePassword,
258 String assertReturnPage )
260 clickLinkWithLocator( "login-link-a", false );
261 setFieldValue( "user-login-form-username", username );
262 setFieldValue( "user-login-form-password", password );
266 checkField( "rememberMe" );
269 clickButtonWithLocator( "modal-login-ok" );
270 if ( validUsernamePassword )
272 assertUserLoggedIn( username );
277 if ( "Login Page".equals( assertReturnPage ) )
283 assertPage( assertReturnPage );
288 // *******************************************************
289 // Auxiliar methods. This method help us and simplify test.
290 // *******************************************************
292 protected void assertUserLoggedIn( String username )
294 Assert.assertFalse( isElementVisible( "login-link" ) );
295 Assert.assertTrue( isElementVisible( "logout-link" ) );
296 Assert.assertFalse( isElementVisible( "register-link" ) );
297 Assert.assertFalse( isElementVisible( "create-admin-link" ) );
300 public void assertCreateAdmin()
302 assertElementPresent( "user-create" );
303 assertFieldValue( "admin", "username" );
304 assertElementPresent( "fullname" );
305 assertElementPresent( "password" );
306 assertElementPresent( "confirmPassword" );
307 assertElementPresent( "email" );
310 public void assertFieldValue( String fieldValue, String fieldName )
312 assertElementPresent( fieldName );
313 Assert.assertEquals( fieldValue, getSelenium().getValue( fieldName ) );
316 public void assertPage( String title )
318 Assert.assertEquals( getTitle(), title );
321 public String getTitle()
324 return getSelenium().getTitle().replaceAll( "[ \n\r]+", " " );
327 public String getHtmlContent()
329 return getSelenium().getHtmlSource();
332 public String getText( String locator )
334 return getSelenium().getText( locator );
337 public void assertTextPresent( String text )
339 Assert.assertTrue( "'" + text + "' isn't present.", getSelenium().isTextPresent( text ) );
343 * one of text args must be in the page so use en and fr text (olamy use en locale :-) )
347 public void assertTextPresent( String... texts )
349 boolean present = false;
350 StringBuilder sb = new StringBuilder();
351 for ( String text : texts )
353 present = present || getSelenium().isTextPresent( text );
354 sb.append( " " + text + " " );
356 Assert.assertTrue( "'one of the following test " + sb.toString() + "' isn't present.", present );
359 public void assertTextNotPresent( String text )
361 Assert.assertFalse( "'" + text + "' is present.", getSelenium().isTextPresent( text ) );
364 public void assertElementPresent( String elementLocator )
366 Assert.assertTrue( "'" + elementLocator + "' isn't present.", isElementPresent( elementLocator ) );
369 public void assertElementNotPresent( String elementLocator )
371 Assert.assertFalse( "'" + elementLocator + "' is present.", isElementPresent( elementLocator ) );
374 public void assertLinkPresent( String text )
376 Assert.assertTrue( "The link '" + text + "' isn't present.", isElementPresent( "link=" + text ) );
379 public void assertLinkNotPresent( String text )
381 Assert.assertFalse( "The link('" + text + "' is present.", isElementPresent( "link=" + text ) );
384 public void assertLinkNotVisible( String text )
386 Assert.assertFalse( "The link('" + text + "' is visible.", isElementVisible( "link=" + text ) );
389 public void assertLinkVisible( String text )
391 Assert.assertTrue( "The link('" + text + "' is not visible.", isElementVisible( "link=" + text ) );
394 public void assertImgWithAlt( String alt )
396 assertElementPresent( "/¯img[@alt='" + alt + "']" );
399 public void assertImgWithAltAtRowCol( boolean isALink, String alt, int row, int column )
401 String locator = "//tr[" + row + "]/td[" + column + "]/";
402 locator += isALink ? "a/" : "";
403 locator += "img[@alt='" + alt + "']";
405 assertElementPresent( locator );
408 public void assertImgWithAltNotPresent( String alt )
410 assertElementNotPresent( "/¯img[@alt='" + alt + "']" );
413 public void assertCellValueFromTable( String expected, String tableElement, int row, int column )
415 Assert.assertEquals( expected, getCellValueFromTable( tableElement, row, column ) );
418 public boolean isTextPresent( String text )
420 return getSelenium().isTextPresent( text );
423 public boolean isLinkPresent( String text )
425 return isElementPresent( "link=" + text );
428 public boolean isElementPresent( String locator )
430 return getSelenium().isElementPresent( locator );
433 public boolean isElementVisible( String locator )
435 return getSelenium().isVisible( locator );
439 public void waitPage()
441 // TODO define a smaller maxWaitTimeJsInMs for wait javascript response for browser side validation
442 //getSelenium().w .wait( Long.parseLong( maxWaitTimeInMs ) );
443 //getSelenium().waitForPageToLoad( maxWaitTimeInMs );
444 // http://jira.openqa.org/browse/SRC-302
445 // those hack looks to break some tests :-(
446 // getSelenium().waitForCondition( "selenium.isElementPresent('document.body');", maxWaitTimeInMs );
447 //getSelenium().waitForCondition( "selenium.isElementPresent('footer');", maxWaitTimeInMs );
448 //getSelenium().waitForCondition( "selenium.browserbot.getCurrentWindow().document.getElementById('footer')",
449 // maxWaitTimeInMs );
450 // so the only hack is to not use a too small wait time
454 Thread.sleep( maxWaitTimeInMs );
456 catch ( InterruptedException e )
458 throw new RuntimeException( "issue on Thread.sleep : " + e.getMessage(), e );
462 public String getFieldValue( String fieldName )
464 return getSelenium().getValue( fieldName );
467 public String getCellValueFromTable( String tableElement, int row, int column )
469 return getSelenium().getTable( tableElement + "." + row + "." + column );
472 public void selectValue( String locator, String value )
474 getSelenium().select( locator, "label=" + value );
478 public void assertOptionPresent( String selectField, String[] options )
480 assertElementPresent( selectField );
481 String[] optionsPresent = getSelenium().getSelectOptions( selectField );
482 List<String> expected = Arrays.asList( options );
483 List<String> present = Arrays.asList( optionsPresent );
484 Assert.assertTrue( "Options expected are not included in present options", present.containsAll( expected ) );
487 public void assertSelectedValue( String value, String fieldName )
489 assertElementPresent( fieldName );
490 String optionsPresent = getSelenium().getSelectedLabel( value );
491 Assert.assertEquals( optionsPresent, value );
496 clickLinkWithXPath( "//input[@type='submit']" );
499 public void assertButtonWithValuePresent( String text )
501 Assert.assertTrue( "'" + text + "' button isn't present", isButtonWithValuePresent( text ) );
504 public void assertButtonWithIdPresent( String id )
506 Assert.assertTrue( "'Button with id =" + id + "' isn't present", isButtonWithIdPresent( id ) );
509 public void assertButtonWithValueNotPresent( String text )
511 Assert.assertFalse( "'" + text + "' button is present", isButtonWithValuePresent( text ) );
514 public boolean isButtonWithValuePresent( String text )
516 return isElementPresent( "//button[@value='" + text + "']" ) || isElementPresent(
517 "//input[@value='" + text + "']" );
520 public boolean isButtonWithIdPresent( String text )
522 return isElementPresent( "//button[@id='" + text + "']" ) || isElementPresent( "//input[@id='" + text + "']" );
525 public void clickButtonWithName( String text, boolean wait )
527 clickLinkWithXPath( "//input[@name='" + text + "']", wait );
530 public void clickButtonWithValue( String text )
532 clickButtonWithValue( text, true );
535 public void clickButtonWithValue( String text, boolean wait )
537 assertButtonWithValuePresent( text );
539 if ( isElementPresent( "//button[@value='" + text + "']" ) )
541 clickLinkWithXPath( "//button[@value='" + text + "']", wait );
545 clickLinkWithXPath( "//input[@value='" + text + "']", wait );
549 public void clickSubmitWithLocator( String locator )
551 clickLinkWithLocator( locator );
554 public void clickSubmitWithLocator( String locator, boolean wait )
556 clickLinkWithLocator( locator, wait );
559 public void clickImgWithAlt( String alt )
561 clickLinkWithLocator( "//img[@alt='" + alt + "']" );
564 public void clickLinkWithText( String text )
566 clickLinkWithText( text, true );
569 public void clickLinkWithText( String text, boolean wait )
571 clickLinkWithLocator( "link=" + text, wait );
574 public void clickLinkWithXPath( String xpath )
576 clickLinkWithXPath( xpath, true );
579 public void clickLinkWithXPath( String xpath, boolean wait )
581 clickLinkWithLocator( "xpath=" + xpath, wait );
584 public void clickLinkWithLocator( String locator )
586 clickLinkWithLocator( locator, true );
589 public void clickLinkWithLocator( String locator, boolean wait )
591 assertElementPresent( locator );
592 getSelenium().click( locator );
599 public void clickButtonWithLocator( String locator )
601 clickButtonWithLocator( locator, true );
604 public void clickButtonWithLocator( String locator, boolean wait )
606 assertElementPresent( locator );
607 getSelenium().click( locator );
614 public void setFieldValues( Map<String, String> fieldMap )
616 Map.Entry<String, String> entry;
618 for ( Iterator<Entry<String, String>> entries = fieldMap.entrySet().iterator(); entries.hasNext(); )
620 entry = entries.next();
622 getSelenium().type( entry.getKey(), entry.getValue() );
626 public void setFieldValue( String fieldName, String value )
628 getSelenium().type( fieldName, value );
631 public void checkField( String locator )
633 getSelenium().check( locator );
636 public void uncheckField( String locator )
638 getSelenium().uncheck( locator );
641 public boolean isChecked( String locator )
643 return getSelenium().isChecked( locator );
646 public void assertIsChecked( String locator )
648 Assert.assertTrue( getSelenium().isChecked( locator ) );
651 public void assertIsNotChecked( String locator )
653 Assert.assertFalse( getSelenium().isChecked( locator ) );
656 public void assertXpathCount( String locator, int expectedCount )
658 int count = getSelenium().getXpathCount( locator ).intValue();
659 Assert.assertEquals( count, expectedCount );
662 public void assertElementValue( String locator, String expectedValue )
664 Assert.assertEquals( getSelenium().getValue( locator ), expectedValue );
667 public String captureScreenShotOnFailure( Throwable failure, String methodName, String className )
669 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" );
670 String time = sdf.format( new Date() );
671 File targetPath = new File( "target", "screenshots" );
675 for ( StackTraceElement stackTrace : failure.getStackTrace() )
677 if ( stackTrace.getClassName().equals( this.getClass().getName() ) )
679 lineNumber = stackTrace.getLineNumber();
685 Selenium selenium = getSelenium();
686 String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;
688 selenium.windowMaximize();
692 // save html to have a minimum feedback if jenkins firefox not up
693 File fileNameHTML = new File( new File( "target", "errorshtmlsnap" ) , fileBaseName + ".html" );
694 FileUtils.writeStringToFile( fileNameHTML, selenium.getHtmlSource() );
696 catch ( IOException e )
698 System.out.print( e.getMessage() );
702 File fileName = new File( targetPath, fileBaseName + ".png" );
704 selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "background=#FFFFFF" );
706 return fileName.getAbsolutePath();