continuum-commits mailing list archives

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

URL: http://svn.apache.org/viewvc?rev=794897&view=rev
Log:
Refactor the way the selenium session is stored

Added:
    continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/SeleniumSession.java
  (with props)
Modified:
    continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java
    continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java
    continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/AbstractSeleniumTest.java
    continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java

Modified: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java
URL: http://svn.apache.org/viewvc/continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java?rev=794897&r1=794896&r2=794897&view=diff
==============================================================================
--- continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java
(original)
+++ continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java
Thu Jul 16 22:40:21 2009
@@ -19,8 +19,7 @@
  * under the License.
  */
 
-import static org.apache.continuum.web.test.parent.ThreadSafeSeleniumSession.getBrowser;
-import static org.apache.continuum.web.test.parent.ThreadSafeSeleniumSession.getSelenium;
+import static org.apache.continuum.web.test.parent.ThreadSafeSeleniumSession.getSession;
 
 import java.io.File;
 import java.text.SimpleDateFormat;
@@ -51,7 +50,8 @@
             /* ignore errors related to captureEntirePageScreenshot not implemented in some
browsers */
             if ( !e.getMessage().contains( "captureEntirePageScreenshot is only implemented
for Firefox" ) )
             {
-                System.out.println( "Error when taking screenshot for test " + tr.getName()
+ " [" + getBrowser() + "]" );
+                System.out.println( "Error when taking screenshot for test " + tr.getName()
+ " ["
+                    + getSession().getBrowser() + "]" );
                 e.printStackTrace();
             }
         }
@@ -78,8 +78,8 @@
         String className = cName.substring( cName.lastIndexOf( '.' ) + 1 );
         String fileName =
             targetPath.toString() + fs + methodName + "(" + className + ".java_" + lineNumber
+ ")-" + time + ".png";
-        getSelenium().windowMaximize();
-        getSelenium().captureEntirePageScreenshot( fileName, "" );
+        getSession().getSelenium().windowMaximize();
+        getSession().getSelenium().captureEntirePageScreenshot( fileName, "" );
     }
 
     private int getStackTraceIndexOfCallingClass( String nameOfClass, StackTraceElement stackTrace[]
)

Modified: 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=794897&r1=794896&r2=794897&view=diff
==============================================================================
--- continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java
(original)
+++ continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/listener/SeleniumListener.java
Thu Jul 16 22:40:21 2009
@@ -19,8 +19,13 @@
  * under the License.
  */
 
-import static org.apache.continuum.web.test.parent.ThreadSafeSeleniumSession.*;
+import static org.apache.continuum.web.test.parent.ThreadSafeSeleniumSession.getSession;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.continuum.web.test.parent.SeleniumSession;
 import org.testng.Assert;
 import org.testng.ITestContext;
 import org.testng.ITestResult;
@@ -34,10 +39,15 @@
 public class SeleniumListener
     extends TestListenerAdapter
 {
+    private static final boolean ONE_BROWSER_PER_TEST = false;
+
+    private static final Collection<SeleniumSession> SELENIUM_SESSIONS =
+        Collections.synchronizedCollection( new ArrayList<SeleniumSession>() );
+
     @Override
     public void onTestStart( ITestResult result )
     {
-        if ( !isStarted() && !isInError() )
+        if ( !getSession().isStarted() && !getSession().isInError() )
         {
             /* start selenium */
             String browser = getProperty( "browser" );
@@ -50,24 +60,53 @@
             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() );
+            getSession().start( seleniumHost, Integer.parseInt( seleniumPort ), browser,
baseUrl );
+            SELENIUM_SESSIONS.add( getSession() );
+            System.out.println( "Started Selenium session: " + getSession().configurationString()
);
         }
         super.onTestStart( result );
     }
 
     @Override
+    public void onFinish( ITestContext testContext )
+    {
+        /* ensure all browsers are killed */
+        for ( SeleniumSession session : SELENIUM_SESSIONS )
+        {
+            try
+            {
+                if ( session.isStarted() )
+                {
+                    session.stop();
+                }
+            }
+            catch ( RuntimeException e )
+            {
+                System.err.println( "Error stoping selenium server: " + session.configurationString()
);
+                e.printStackTrace();
+            }
+        }
+        super.onFinish( testContext );
+    }
+
+    @Override
     public void onTestSuccess( ITestResult tr )
     {
-        stop();
+        if ( ONE_BROWSER_PER_TEST )
+        {
+            getSession().stop();
+        }
         super.onTestSuccess( tr );
     }
 
     @Override
     public void onTestFailure( ITestResult tr )
     {
-        stop();
-        System.out.println( "Test " + tr.getName() + configurationString() + " -> Failed"
);
+        if ( ONE_BROWSER_PER_TEST )
+        {
+            getSession().stop();
+        }
+        System.out.println( "Test " + tr.getName() + " " + getSession().configurationString()
+ " -> Failed" );
         super.onTestFailure( tr );
     }
 

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=794897&r1=794896&r2=794897&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 22:40:21 2009
@@ -76,7 +76,7 @@
 
     public Selenium getSelenium()
     {
-        return ThreadSafeSeleniumSession.getSelenium();
+        return ThreadSafeSeleniumSession.getSession().getSelenium();
     }
 
     /**
@@ -126,7 +126,7 @@
     public void close()
         throws Exception
     {
-        ThreadSafeSeleniumSession.stop();
+        ThreadSafeSeleniumSession.getSession().stop();
     }
 
     // *******************************************************

Added: continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/SeleniumSession.java
URL: http://svn.apache.org/viewvc/continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/SeleniumSession.java?rev=794897&view=auto
==============================================================================
--- continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/SeleniumSession.java
(added)
+++ continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/SeleniumSession.java
Thu Jul 16 22:40:21 2009
@@ -0,0 +1,143 @@
+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;
+
+/**
+ * Selenium configuration.
+ * 
+ * @author Carlos Sanchez <a href="mailto:carlos@apache.org">
+ */
+public class SeleniumSession
+{
+    private Selenium selenium;
+
+    private Exception error;
+
+    private String host, browser, baseUrl;
+
+    private int port;
+
+    public void start( String seleniumHost, int seleniumPort, String browser, String baseUrl
)
+    {
+        try
+        {
+            DefaultSelenium s = new DefaultSelenium( seleniumHost, seleniumPort, browser,
baseUrl );
+            s.start();
+            this.selenium = s;
+            this.host = seleniumHost;
+            this.port = seleniumPort;
+            this.browser = browser;
+            this.baseUrl = baseUrl;
+            afterStart();
+        }
+        catch ( RuntimeException e )
+        {
+            error = e;
+            throw e;
+        }
+    }
+
+    private void afterStart()
+    {
+        AbstractSeleniumTest.baseUrl = getBaseUrl();
+        SetupContinuum setup = new SetupContinuum();
+        try
+        {
+            setup.initializeContinuumIfNeeded();
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+    public void stop()
+    {
+        if ( getSelenium() != null )
+        {
+            getSelenium().stop();
+        }
+        clear();
+    }
+
+    private void clear()
+    {
+        selenium = null;
+        error = null;
+        host = null;
+        port = 0;
+        browser = null;
+        baseUrl = null;
+    }
+
+    public boolean isStarted()
+    {
+        return selenium != null;
+    }
+
+    public boolean isInError()
+    {
+        return error != null;
+    }
+
+    public Selenium getSelenium()
+    {
+        Selenium s = selenium;
+        if ( s != null )
+        {
+            return s;
+        }
+        else
+        {
+            throw new RuntimeException( "Selenium session has not been started." );
+        }
+    }
+
+    public String getHost()
+    {
+        return host;
+    }
+
+    public String getBrowser()
+    {
+        return browser;
+    }
+
+    public String getBaseUrl()
+    {
+        return baseUrl;
+    }
+
+    public int getPort()
+    {
+        return port;
+    }
+
+    public String configurationString()
+    {
+        return "[" + getHost() + ", " + getPort() + ", " + getBaseUrl() + ", " + getBrowser()
+ "]";
+    }
+
+}

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

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

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

Modified: 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=794897&r1=794896&r2=794897&view=diff
==============================================================================
--- continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java
(original)
+++ continuum/branches/continuum-selenium-parallel-carlos/src/test/testng/org/apache/continuum/web/test/parent/ThreadSafeSeleniumSession.java
Thu Jul 16 22:40:21 2009
@@ -19,11 +19,6 @@
  * 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.
  * 
@@ -31,126 +26,17 @@
  */
 public class ThreadSafeSeleniumSession
 {
-    private static ThreadLocal<ThreadSafeSeleniumSession> instance = new ThreadLocal<ThreadSafeSeleniumSession>();
-
-    private Selenium selenium;
-
-    private Exception error;
+    private static ThreadLocal<SeleniumSession> instance = new ThreadLocal<SeleniumSession>();
 
-    private String host, browser, baseUrl;
-
-    private int port;
-
-    private static ThreadSafeSeleniumSession getInstance()
+    public static SeleniumSession getSession()
     {
-        ThreadSafeSeleniumSession session = instance.get();
+        SeleniumSession session = instance.get();
         if ( session == null )
         {
-            session = new ThreadSafeSeleniumSession();
+            session = new SeleniumSession();
             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.initializeContinuumIfNeeded();
-        }
-        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()
+ "]";
-    }
-
 }



Mime
View raw message