continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From car...@apache.org
Subject svn commit: r794477 - in /continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test: ./ listener/ parent/
Date Thu, 16 Jul 2009 00:56:02 GMT
Author: carlos
Date: Thu Jul 16 00:56:02 2009
New Revision: 794477

URL: http://svn.apache.org/viewvc?rev=794477&view=rev
Log:
Extract general selenium utils into their own classes

Added:
    continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupContinuum.java
  (contents, props changed)
      - copied, changed from r794055, continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupSelenium.java
    continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java
  (with props)
    continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java
  (with props)
Removed:
    continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupSelenium.java
Modified:
    continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/AbstractSeleniumTest.java

Copied: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupContinuum.java
(from r794055, continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupSelenium.java)
URL: http://svn.apache.org/viewvc/continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupContinuum.java?p2=continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupContinuum.java&p1=continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupSelenium.java&r1=794055&r2=794477&rev=794477&view=diff
==============================================================================
--- continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupSelenium.java
(original)
+++ continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupContinuum.java
Thu Jul 16 00:56:02 2009
@@ -20,48 +20,20 @@
  */
 
 import org.apache.continuum.web.test.parent.AbstractContinuumTest;
-import org.testng.Assert;
-import org.testng.annotations.AfterTest;
-import org.testng.annotations.BeforeSuite;
-import org.testng.annotations.BeforeTest;
-import org.testng.annotations.Optional;
-import org.testng.annotations.Parameters;
-import org.testng.annotations.Test;
 
 /**
- * Based on SetupSelenium of Wendy Smoak test.
+ * Based on SetupContinuum of Wendy Smoak test.
  * 
  * @author José Morales Martínez
  * @version $Id$
  */
-@Test( groups = { "setup" }, alwaysRun = true )
-public class SetupSelenium
+public class SetupContinuum
     extends AbstractContinuumTest
 {
-    @Override
-    @AfterTest
-    public void close()
-        throws Exception
-    {
-        super.close();
-    }
-
-    @Override
-    @BeforeSuite
-    public void open()
-        throws Exception
-    {
-        super.open();
-    }
 
-    @BeforeTest
-    @Parameters( { "baseUrl", "browser", "seleniumHost", "seleniumPort" } )
-    public void initializeContinuum( String baseUrl, String browser, @Optional( "localhost"
) String seleniumHost,
-                                     @Optional( "4444" ) int seleniumPort )
+     public void initializeContinuum()
         throws Exception
     {
-        super.open( baseUrl, browser, seleniumHost, seleniumPort );
-        Assert.assertNotNull( getSelenium(), "Selenium is not initialized" );
         getSelenium().open( baseUrl );
         String title = getSelenium().getTitle();
         if ( title.equals( "Create Admin User" ) )

Propchange: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupContinuum.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupContinuum.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/SetupContinuum.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java
URL: http://svn.apache.org/viewvc/continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java?rev=794477&view=auto
==============================================================================
--- continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java
(added)
+++ continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java
Thu Jul 16 00:56:02 2009
@@ -0,0 +1,86 @@
+package org.apache.continuum.web.test.listener;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.apache.continuum.web.test.parent.ThreadSafeSeleniumSession.*;
+
+import org.testng.Assert;
+import org.testng.ITestContext;
+import org.testng.ITestResult;
+import org.testng.TestListenerAdapter;
+
+/**
+ * Configures Selenium before each test if needed.
+ * 
+ * @author Carlos Sanchez <a href="mailto:carlos@apache.org">
+ */
+public class SeleniumListener
+    extends TestListenerAdapter
+{
+    @Override
+    public void onTestStart( ITestResult result )
+    {
+        if ( !isStarted() && !isInError() )
+        {
+            /* start selenium */
+            String browser = getProperty( "browser" );
+            String seleniumHost = getProperty( "seleniumHost" );
+            String seleniumPort = getProperty( "seleniumPort" );
+            String baseUrl = getProperty( "baseUrl" );
+            Assert.assertNotNull( browser, "browser parameter is not defined" );
+            Assert.assertNotNull( seleniumHost, "seleniumHost parameter is not defined" );
+            Assert.assertNotNull( seleniumPort, "seleniumPort parameter is not defined" );
+            Assert.assertNotNull( baseUrl, "baseUrl parameter is not defined" );
+            System.out.println( "Starting Selenium session: " + "[" + seleniumHost + ", "
+ seleniumPort + ", "
+                + baseUrl + ", " + browser + "]" );
+            start( seleniumHost, Integer.parseInt( seleniumPort ), browser, baseUrl );
+            System.out.println( "Started Selenium session: " + configurationString() );
+        }
+        super.onTestStart( result );
+    }
+
+    @Override
+    public void onTestSuccess( ITestResult tr )
+    {
+        stop();
+        super.onTestSuccess( tr );
+    }
+
+    @Override
+    public void onTestFailure( ITestResult tr )
+    {
+        stop();
+        System.out.println( "Test " + tr.getName() + configurationString() + " -> Failed"
);
+        super.onTestFailure( tr );
+    }
+
+    private String getProperty( String name )
+    {
+        for ( ITestContext context : getTestContexts() )
+        {
+            String p = context.getSuite().getParameter( "browser" );
+            if ( p != null )
+            {
+                return p;
+            }
+        }
+        return System.getProperty( name );
+    }
+}

Propchange: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/AbstractSeleniumTest.java
URL: http://svn.apache.org/viewvc/continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/AbstractSeleniumTest.java?rev=794477&r1=794476&r2=794477&view=diff
==============================================================================
--- continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/AbstractSeleniumTest.java
(original)
+++ continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/AbstractSeleniumTest.java
Thu Jul 16 00:56:02 2009
@@ -32,7 +32,6 @@
 import org.apache.commons.io.IOUtils;
 import org.testng.Assert;
 
-import com.thoughtworks.selenium.DefaultSelenium;
 import com.thoughtworks.selenium.Selenium;
 
 /**
@@ -47,52 +46,37 @@
 
     public static String maxWaitTimeInMs;
 
-    private static ThreadLocal<Selenium> selenium = new ThreadLocal<Selenium>();
-
-    private static ThreadLocal<String> seleniumBrowser = new ThreadLocal<String>();
-
-    private static Properties p;
+    private static Properties properties;
 
     private final static String PROPERTIES_SEPARATOR = "=";
 
-    /**
-     * Initialize properties.
-     */
-    public void open()
-        throws Exception
+    static
     {
-        InputStream input = this.getClass().getClassLoader().getResourceAsStream( "testng.properties"
);
-        p = new Properties();
-        p.load( input );
-
-        maxWaitTimeInMs = getProperty( "MAX_WAIT_TIME_IN_MS" );
+        initialize();
     }
 
     /**
-     * Initialize selenium
+     * Initialize properties.
      */
-    public void open( String baseUrl, String browser, String seleniumHost, int seleniumPort
)
-        throws Exception
+    private static void initialize()
     {
-        this.baseUrl = baseUrl;
-
-        if ( getSelenium() == null )
+        InputStream input = AbstractSeleniumTest.class.getClassLoader().getResourceAsStream(
"testng.properties" );
+        properties = new Properties();
+        try
         {
-            DefaultSelenium s = new DefaultSelenium( seleniumHost, seleniumPort, browser,
baseUrl );
-            s.start();
-            selenium.set( s );
-            seleniumBrowser.set( browser );
+            properties.load( input );
+        }
+        catch ( IOException e )
+        {
+            throw new RuntimeException( e );
         }
-    }
 
-    public static Selenium getSelenium()
-    {
-        return selenium == null ? null : selenium.get();
+        maxWaitTimeInMs = properties.getProperty( "MAX_WAIT_TIME_IN_MS" );
     }
 
-    public static String getSeleniumBrowser()
+    public Selenium getSelenium()
     {
-        return seleniumBrowser == null ? null : seleniumBrowser.get();
+        return ThreadSafeSeleniumSession.getSelenium();
     }
 
     /**
@@ -108,7 +92,7 @@
 
     protected String getProperty( String key )
     {
-        return p.getProperty( key );
+        return properties.getProperty( key );
     }
 
     protected String getEscapeProperty( String key )
@@ -142,11 +126,7 @@
     public void close()
         throws Exception
     {
-        if ( getSelenium() != null )
-        {
-            getSelenium().stop();
-            selenium.set( null );
-        }
+        ThreadSafeSeleniumSession.stop();
     }
 
     // *******************************************************
@@ -201,7 +181,7 @@
 
     public void assertLinkNotPresent( String text )
     {
-            Assert.assertFalse( isElementPresent( "link=" + text ), "The link '" + text +
"' is present." );
+        Assert.assertFalse( isElementPresent( "link=" + text ), "The link '" + text + "'
is present." );
     }
 
     public void assertImgWithAlt( String alt )

Added: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java
URL: http://svn.apache.org/viewvc/continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java?rev=794477&view=auto
==============================================================================
--- continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java
(added)
+++ continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java
Thu Jul 16 00:56:02 2009
@@ -0,0 +1,156 @@
+package org.apache.continuum.web.test.parent;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.continuum.web.test.SetupContinuum;
+
+import com.thoughtworks.selenium.DefaultSelenium;
+import com.thoughtworks.selenium.Selenium;
+
+/**
+ * Storage for selenium related objects needed for each thread when running in parallel.
+ * 
+ * @author Carlos Sanchez <a href="mailto:carlos@apache.org">
+ */
+public class ThreadSafeSeleniumSession
+{
+    private static ThreadLocal<ThreadSafeSeleniumSession> instance = new ThreadLocal<ThreadSafeSeleniumSession>();
+
+    private Selenium selenium;
+
+    private Exception error;
+
+    private String host, browser, baseUrl;
+
+    private int port;
+
+    private static ThreadSafeSeleniumSession getInstance()
+    {
+        ThreadSafeSeleniumSession session = instance.get();
+        if ( session == null )
+        {
+            session = new ThreadSafeSeleniumSession();
+            instance.set( session );
+        }
+        return session;
+    }
+
+    public static void start( String seleniumHost, int seleniumPort, String browser, String
baseUrl )
+    {
+        try
+        {
+            DefaultSelenium s = new DefaultSelenium( seleniumHost, seleniumPort, browser,
baseUrl );
+            s.start();
+            getInstance().selenium = s;
+            getInstance().host = seleniumHost;
+            getInstance().port = seleniumPort;
+            getInstance().browser = browser;
+            getInstance().baseUrl = baseUrl;
+            afterStart();
+        }
+        catch ( RuntimeException e )
+        {
+            getInstance().error = e;
+            throw e;
+        }
+    }
+
+    private static void afterStart()
+    {
+        AbstractSeleniumTest.baseUrl = getBaseUrl();
+        SetupContinuum setup = new SetupContinuum();
+        try
+        {
+            setup.initializeContinuum();
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+    public static void stop()
+    {
+        if ( getSelenium() != null )
+        {
+            getSelenium().stop();
+        }
+        getInstance().clear();
+    }
+
+    private void clear()
+    {
+        selenium = null;
+        error = null;
+        host = null;
+        port = 0;
+        browser = null;
+        baseUrl = null;
+    }
+
+    public static boolean isStarted()
+    {
+        return getInstance().selenium != null;
+    }
+
+    public static boolean isInError()
+    {
+        return getInstance().error != null;
+    }
+
+    public static Selenium getSelenium()
+    {
+        Selenium s = getInstance().selenium;
+        if ( s != null )
+        {
+            return s;
+        }
+        else
+        {
+            throw new RuntimeException( "Selenium session has not been started." );
+        }
+    }
+
+    public static String getHost()
+    {
+        return getInstance().host;
+    }
+
+    public static String getBrowser()
+    {
+        return getInstance().browser;
+    }
+
+    public static String getBaseUrl()
+    {
+        return getInstance().baseUrl;
+    }
+
+    public static int getPort()
+    {
+        return getInstance().port;
+    }
+
+    public static String configurationString()
+    {
+        return "[" + getHost() + ", " + getPort() + ", " + getBaseUrl() + ", " + getBrowser()
+ "]";
+    }
+
+}

Propchange: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message