Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
Working Saucelabs implementation using SauceTunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
David Goudreau & Sam Pierson committed Jan 8, 2010
1 parent 36ba087 commit 85ddff0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
48 changes: 41 additions & 7 deletions java/source_server/net/jsunit/SeleniumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -26,19 +29,50 @@ public class SeleniumTest extends TestCase {
protected static String SELENIUM_SERVER_PORT = "selenium.server.port";
protected static String SELENIUM_BROWSER_STARTCOMMAND = "selenium.browser.startCommand";
protected static String SELENIUM_BROWSER_URL = "selenium.browser.url";
protected Process tunnel_process;
protected String tunnel_id;
protected String tunnel_endpoint_hostname;

public void setUp() throws Exception {
super.setUp();
startJsUnitServer();
startSauceTunnel();
startSeleniumClient();
}

public void tearDown() throws Exception {
stopJsUnitServer();
stopSeleniumClient();
stopSauceTunnel();
stopJsUnitServer();
super.tearDown();
}

private void startSauceTunnel() throws IOException {
System.out.println("Starting sauce tunnel...");
tunnel_process = Runtime.getRuntime().exec("ruby sauce-tunnel-setup.rb");
String line;
BufferedReader input = new BufferedReader
(new InputStreamReader(tunnel_process.getInputStream()));
Pattern pattern = Pattern.compile("\\[saucelabs-adapter\\] Tunnel ID (.*) for (.*) is up\\.$");

while ((line = input.readLine()) != null) {
System.out.println(line);
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
tunnel_id = matcher.group(1);
tunnel_endpoint_hostname = matcher.group(2);
System.out.println("tunnel_endpoint_hostname=" + tunnel_endpoint_hostname);
break;
}
}
System.out.println("done starting tunnel");
}

private void stopSauceTunnel() throws IOException {
System.out.println("stopping tunnel");
tunnel_process = Runtime.getRuntime().exec("ruby sauce-tunnel-delete.rb " + tunnel_id);
}

public void testStandaloneRun() throws Exception {
selenium.open("/jsunit/testRunner.html?testPage=/jsunit/tests/jsUnitTestSuite.html&autorun=true");
waitForStatus();
Expand Down Expand Up @@ -75,9 +109,9 @@ public void testStandaloneRun() throws Exception {
private HashMap<String, String> loadSeleniumConfig(String configPath) throws Exception {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File(configPath));
Document doc = docBuilder.parse(new File(configPath));
doc.getDocumentElement().normalize();
Element seleniumConfig = (Element)doc.getElementsByTagName("selenium_config").item(0);
Element seleniumConfig = (Element) doc.getElementsByTagName("selenium_config").item(0);
HashMap<String, String> seleniumProperties = new HashMap<String, String>();

String host = System.getProperty(SELENIUM_SERVER_HOST);
Expand All @@ -86,16 +120,16 @@ private HashMap<String, String> loadSeleniumConfig(String configPath) throws Exc
String browser_url = System.getProperty(SELENIUM_BROWSER_URL);

if (host == null) {
host = seleniumConfig.getElementsByTagName("host").item(0).getFirstChild().getNodeValue();
host = seleniumConfig.getElementsByTagName("host").item(0).getFirstChild().getNodeValue();
}
if (port == null) {
port = seleniumConfig.getElementsByTagName("port").item(0).getFirstChild().getNodeValue();
port = seleniumConfig.getElementsByTagName("port").item(0).getFirstChild().getNodeValue();
}
if (start_command == null) {
start_command = seleniumConfig.getElementsByTagName("browser_start_command").item(0).getFirstChild().getNodeValue();
start_command = seleniumConfig.getElementsByTagName("browser_start_command").item(0).getFirstChild().getNodeValue();
}
if (browser_url == null) {
browser_url = seleniumConfig.getElementsByTagName("browser_url").item(0).getFirstChild().getNodeValue();
browser_url = "http://" + tunnel_endpoint_hostname;
}
seleniumProperties.put("host", host);
seleniumProperties.put("port", port);
Expand Down
28 changes: 28 additions & 0 deletions sauce-tunnel-delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'saucelabs-adapter'

STDOUT.sync = true

case ARGV.size
when 1:
@tunnel_id = ARGV[0]
else
puts "Usage: sauce-tunnel-delete.rb <tunnel_id>"
exit 1
end

def connect_to_rest_api
sauce_api_url = "https://#{@selenium_config['username']}:#{@selenium_config['access-key']}@saucelabs.com/rest/#{@selenium_config['username']}/"
# puts "[saucelabs-adapter] Connecting to Sauce API at #{sauce_api_url}"
@sauce_api_endpoint = SauceREST::Client.new sauce_api_url
end

@selenium_config = SeleniumConfig.new('saucelabs', '../selenium.yml', 8080)
connect_to_rest_api
@sauce_api_endpoint.delete :tunnel, @tunnel_id

exit 0


29 changes: 29 additions & 0 deletions sauce-tunnel-setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'saucelabs-adapter'

STDOUT.sync = true

def start_tunnel
puts "[sauce-tunnel] Starting tunnel"
@tunnel = SauceTunnel.new(@selenium_config)
end

def stop_tunnel
puts "[sauce-tunnel] Shutting down tunnel"
@tunnel.shutdown
exit 0
end

Signal.trap('INT') { stop_tunnel }
Signal.trap('TERM') { stop_tunnel }
Signal.trap('HUP') { stop_tunnel }

@selenium_config = SeleniumConfig.new('saucelabs', '../selenium.yml', 8080)

start_tunnel
sleep 3600
stop_tunnel

exit 1

0 comments on commit 85ddff0

Please sign in to comment.