package com.thoughtworks.selenium.thirdparty;

import org.openqa.selenium.server.RemoteControlConfiguration;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleniumException;

public class SessionExtensionJsTest extends SeleneseTestCase {

    private static final String TEST_URL = "http://www.google.com";

    @Override
    public void setUp(String url, String browserString) throws Exception {
       setTestContext();
    }

    @Override
    public void tearDown() throws Exception {    
    }

    public void testLoadSimpleExtensionJs() {
        // expect failure when the extension isn't set
        Selenium selenium = getNewSelenium();
        try {
            runCommands(selenium);
            fail("Expected SeleniumException but none was encountered");
        }
        catch (SeleniumException se) {
            assertTrue(se.getMessage().endsWith("comeGetSome is not defined"));
        }
        finally {
            selenium.stop();
        }

        // everything is peachy when the extension is set
        selenium = getNewSelenium();
        selenium.setExtensionJs("var comeGetSome = 'tn';");
        runCommands(selenium);
        assertEquals("Klaatu barada nikto - Google Search",
            selenium.getTitle());
        selenium.stop();
        
        // reusing the session ... extension should still be available
        runCommands(selenium);
        assertEquals("Klaatu barada nikto - Google Search",
            selenium.getTitle());
        selenium.stop();
    }

    private Selenium getNewSelenium() {
        return new DefaultSelenium("localhost",
            RemoteControlConfiguration.getDefaultPort(), runtimeBrowserString(), TEST_URL);
    }

    private void runCommands(Selenium selenium) {
        selenium.start();
        selenium.open(TEST_URL);
        selenium.type("q", "Klaatu barada nikto");
        selenium.click("javascript{ 'b' + comeGetSome + 'G' }");
        selenium.waitForPageToLoad("5000");
    }

}
@@ -55,57 +55,200 @@
 org.
-openqa.selenium.server.RemoteControlConfiguration
+testng.annotations.AfterMethod;%0Aimport org.testng.annotations.BeforeMethod;%0Aimport org.testng.annotations.Optional;%0Aimport org.testng.annotations.Parameters;%0Aimport org.testng.annotations.Test
 ;%0A%0Ai
@@ -341,20 +341,24 @@
 neseTest
-Case
+NgHelper
 ;%0Aimport
@@ -507,203 +507,518 @@
 Test
-Case %7B%0A%0A    private static final String TEST_URL = %22http://www.google.com%22;%0A%0A    @Override%0A    public void setUp(String url, String browserString) throws Exception %7B%0A       setTestContext
+NgHelper %7B%0A%0A    private Selenium privateSelenium;%0A    private String host, browser;%0A    private int port;%0A    %0A    private static final String TEST_URL = %22http://localhost:4444/selenium-server/tests/html/test_click_page1.html%22;%0A%0A    @BeforeMethod%0A    @Parameters(%7B%22selenium.host%22, %22selenium.port%22, %22selenium.browser%22%7D)%0A    public void privateSetUp(@Optional(%22localhost%22) String host, @Optional(%224444%22) String port, @Optional String browser) %7B%0A        if (browser == null) browser = runtimeBrowserString
 ();%0A    
 %7D%0A%0A 
@@ -1017,228 +1017,359 @@
     
-%7D%0A%0A
     
-@Override%0A    public void tearDown() throws Exception %7B    
+this.host = host;%0A        this.port = Integer.parseInt(port);%0A        this.browser = browser;
 %0A    
-%7D%0A%0A
     p
-ublic void testLoadSimpleExtensionJs() %7B
+rivateSelenium = getNewSelenium();
 %0A    
+%7D%0A
     
-// expect failure when the extension isn't set%0A        Selenium selenium = getNewSelenium();
+%0A    @AfterMethod(alwaysRun=true)%0A    public void privateTearDown()%7B%0A        if (privateSelenium != null) privateSelenium.stop();%0A    %7D%0A%0A    @Test%0A    public void expectFailureWhenExtensionNotSet() %7B
 %0A   
@@ -1395,33 +1395,40 @@
     runCommands(
-s
+privateS
 elenium);%0A      
@@ -1641,61 +1641,66 @@
     
+%7D%0A
     
-finally %7B%0A            selenium.stop();%0A        %7D%0A
+%0A    @Test%0A    public void loadSimpleExtensionJs() %7B
 %0A   
@@ -1766,46 +1766,16 @@
     
-selenium = getNewSelenium();%0A        s
+privateS
 elen
@@ -1813,17 +1813,17 @@
 Some = '
-t
+i
 n';%22);%0A 
@@ -1833,33 +1833,40 @@
     runCommands(
-s
+privateS
 elenium);%0A      
@@ -1885,59 +1885,36 @@
 ls(%22
-Klaatu barada nikto - Google Search%22,%0A            s
+Click Page Target%22, privateS
 elen
@@ -1930,33 +1930,40 @@
 tle());%0A        
-s
+privateS
 elenium.stop();%0A
@@ -2062,17 +2062,24 @@
 ommands(
-s
+privateS
 elenium)
@@ -2106,279 +2106,158 @@
 ls(%22
-Klaatu barada nikto - Google Search%22,%0A            selenium.getTitle());%0A        selenium.stop();%0A    %7D%0A%0A    private Selenium getNewSelenium() %7B%0A        return new DefaultSelenium(%22localhost%22,%0A            RemoteControlConfiguration.getDefaultPort(), runtimeBrowserString()
+Click Page Target%22, privateSelenium.getTitle());%0A    %7D%0A%0A    private Selenium getNewSelenium() %7B%0A        return new DefaultSelenium(host, port, browser
 , TE
@@ -2385,59 +2385,8 @@
 L);%0A
-        selenium.type(%22q%22, %22Klaatu barada nikto%22);%0A
     
@@ -2418,17 +2418,17 @@
 cript%7B '
-b
+l
 ' + come
@@ -2438,17 +2438,17 @@
 Some + '
-G
+k
 ' %7D%22);%0A 

This test doesn't need to be a third party test.