Created
May 17, 2011 17:41
-
-
Save freynaud/976944 to your computer and use it in GitHub Desktop.
trying to reproduce Kevin bug.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.openqa.grid.e2e.selenium; | |
import java.io.BufferedWriter; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.io.Reader; | |
import java.io.Writer; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.SocketException; | |
import java.net.SocketTimeoutException; | |
import java.net.URL; | |
import org.openqa.grid.e2e.utils.GridConfigurationMock; | |
import org.openqa.grid.internal.Registry; | |
import org.openqa.grid.selenium.SelfRegisteringRemote; | |
import org.openqa.grid.web.Hub; | |
import org.openqa.selenium.net.PortProber; | |
import org.testng.Assert; | |
import org.testng.annotations.AfterClass; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.Test; | |
import com.thoughtworks.selenium.DefaultSelenium; | |
import com.thoughtworks.selenium.Selenium; | |
/** | |
* A hub controling 1 selenium1 remote can run IE, FF or safari tests. | |
* | |
* | |
*/ | |
public class SmokeTest { | |
private Hub hub = Hub.getNewInstanceForTest(PortProber.findFreePort(), Registry.getNewInstanceForTestOnly()); | |
private URL hubURL = hub.getUrl(); | |
@BeforeClass(alwaysRun = false) | |
public void prepare() throws Exception { | |
hub.start(); | |
SelfRegisteringRemote remote = SelfRegisteringRemote.create(GridConfigurationMock.seleniumConfig(hub.getRegistrationURL())); | |
remote.addFirefoxSupport(); | |
remote.addSafariSupport(); | |
remote.addInternetExplorerSupport(); | |
remote.setTimeout(10000, 1000); | |
remote.launchRemoteServer(); | |
remote.registerToHub(); | |
} | |
@Test(timeOut = 100000) | |
public void sel1firefox() throws InterruptedException { | |
Selenium selenium = new DefaultSelenium(hub.getHost(), hub.getPort(), "*firefox", hubURL + ""); | |
Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 0); | |
selenium.start(); | |
Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 1); | |
selenium.open(hubURL + "/grid/console"); | |
Assert.assertTrue(selenium.getTitle().contains("Grid overview")); | |
selenium.stop(); | |
// cannot assume it will be 0 active session right away. selenium.stop() | |
// will trigger a session.terminate() on the grid, that runs in a | |
// different thread | |
// not to block the test. | |
while (hub.getRegistry().getActiveSessions().size() != 0) { | |
Thread.sleep(250); | |
} | |
} | |
@Test | |
public void clientDisappearAfterSendingNewSession() throws Throwable { | |
// starting a firefox in the hub supporting one browser only, so hub is | |
// busy. | |
Selenium selenium = new DefaultSelenium(hub.getHost(), hub.getPort(), "*firefox", hubURL + ""); | |
Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 0); | |
selenium.start(); | |
// the hub is running the test, so nothing waiting | |
Assert.assertEquals(hub.getRegistry().getNewSessionRequests().size(), 0); | |
// starting a new session request manually to be able to specify a | |
// timeout | |
HttpURLConnection uc = null; | |
Writer wr = null; | |
Reader rdr = null; | |
URL result = new URL(hubURL + "/selenium-server/driver/"); | |
String body = "cmd=getNewBrowserSession&1=*firefox&2=http%3A%2F%2Flocalhost%3A" + hub.getRegistrationURL().getPort() + "&3="; | |
uc = (HttpURLConnection) result.openConnection(); | |
// client dies after 1 sec | |
uc.setReadTimeout(1000); | |
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); | |
uc.setInstanceFollowRedirects(false); | |
uc.setDoOutput(true); | |
wr = new BufferedWriter(new OutputStreamWriter(uc.getOutputStream())); | |
wr.write(body); | |
wr.flush(); | |
try { | |
int responsecode = uc.getResponseCode(); | |
} catch (SocketTimeoutException e) { | |
closeResources(uc, wr, rdr); | |
System.out.println("client times out, resources are closed."); | |
} | |
// the new session request has been recieved, so 1 new session queued. | |
Assert.assertEquals(hub.getRegistry().getNewSessionRequests().size(), 1); | |
// finishing the first test. | |
selenium.stop(); | |
// letting firefox clean up finish | |
Thread.sleep(5000); | |
// the hub can now process the the request from the new session request | |
// waiting list, even if the client has disappeared.No more test queue. | |
Assert.assertEquals(hub.getRegistry().getNewSessionRequests().size(), 0); | |
// and 1 test running | |
Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 1); | |
// the test times out after 11 sec max. | |
Thread.sleep(15000); | |
// no test waiting | |
Assert.assertEquals(hub.getRegistry().getNewSessionRequests().size(), 0); | |
// no test running, firefox should have timed out at this point | |
Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 0); | |
System.out.println("the end"); | |
} | |
private String stringContentsOfInputStream(Reader rdr) throws IOException { | |
StringBuffer sb = new StringBuffer(); | |
int c; | |
try { | |
while ((c = rdr.read()) != -1) { | |
sb.append((char) c); | |
} | |
return sb.toString(); | |
} finally { | |
rdr.close(); | |
} | |
} | |
protected void closeResources(HttpURLConnection conn, Writer wr, Reader rdr) { | |
try { | |
if (null != wr) { | |
wr.close(); | |
} | |
} catch (IOException ioe) { | |
// ignore | |
} | |
try { | |
if (null != rdr) { | |
rdr.close(); | |
} | |
} catch (IOException ioe) { | |
// ignore | |
} | |
if (null != conn) { | |
conn.disconnect(); | |
} | |
} | |
@Test(enabled = false) | |
public void sel1ie() throws InterruptedException { | |
// http://stackoverflow.com/questions/1517623/internet-explorer-8-64bit-and-selenium-not-working | |
Selenium selenium = new DefaultSelenium(hub.getHost(), hub.getPort(), "*iexplore", "http://www.ebay.co.uk"); | |
Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 0); | |
selenium.start(); | |
Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 1); | |
selenium.open("http://www.ebay.co.uk"); | |
Assert.assertTrue(selenium.getTitle().contains("eBay")); | |
selenium.stop(); | |
while (hub.getRegistry().getActiveSessions().size() != 0) { | |
Thread.sleep(250); | |
} | |
} | |
@Test(enabled = false) | |
public void selsafari() { | |
// http://discussions.apple.com/thread.jspa?messageID=12564201 ? | |
Selenium selenium = new DefaultSelenium(hub.getHost(), hub.getPort(), "*safari", "http://www.ebay.co.uk"); | |
Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 0); | |
selenium.start(); | |
Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 1); | |
selenium.open("http://www.ebay.co.uk"); | |
Assert.assertTrue(selenium.getTitle().contains("eBay")); | |
selenium.stop(); | |
Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 0); | |
} | |
@AfterClass(alwaysRun = true) | |
public void stop() throws Exception { | |
hub.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment