Search code examples
selenium-webdriversaucelabs

Programatic SauceLabs Tunnel control


Does anyone have any experience controlling SauceLabs SauceConnect tunnels programmatically for Selenium WebDriver testing? Specifically from within Java code. The examples in the SauceLabs documentation assume that a tunnel is created manually before a test is executed or there is a permanent tunnel somewhere used by all tests.

The tunnels would need to be unique for each test, and support having multiple tunnels/tests running simultaneously on the same box. Anyone using the project should be able to execute tests using a tunnel without manual steps to create tunnels or perform special install and configuration of tunnel software.


Solution

  • I did some research and have come up with a means to control my SauceConnect tunnels from in code. Here is a brief summary of how to get it working in case someone else is looking to do this. I am using this setup to run multiple tunnels on the same server and tie each to on site proxy servers. These examples assume you are running your code on a JVM.

    I have put together a more detailed writeup in a blog post Controlling Sauce Connect.

    Dependency

    Add the following dependency to your project. com.saucelabs.ci-sauce

    Then you can call into the library to start and stop a tunnel using the code below.

    Start Tunnel

    Process tunnel = sauceTunnelManager.openConnection( 
            sauceUser,      // username 
            sauceKey,       // apiKey 
            port,           // port 
            null,           // sauceConnectJar 
            tunnelOptions,  // Tunnel options 
            null,           // printStream 
            null,           // verboseLogging 
            null            // sauceConnectPath );
    

    Close Tunnel

    sauceTunnelManager.closeTunnelsForPlan(
         sauceUser,      // username (same as start tunnel)
         tunnelOptions,  // tunnelOptions (same as start tunnel)
         null);