Skip to main content

Maven Tunnel For Locally Hosted Web Applications


Maven is a renowned build automation tool for orchestrating project builds by automatically handling Selenium dependencies, compiling source code to binary & then packaging of the binary. LambdaTest offers an SSH(Secure Shell) tunnel which allows you to test your locally hosted websites over 2000+ browsers for both mobile and desktop through LambdaTest Selenium Grid.

This document will help you configure a secure LambdaTest Tunnel to execute your Selenium test automation scripts through your Maven project on our online Selenium Grid.

Prerequisites#


  • LambdaTest Authentication Credentials: Be aware of your LambdaTest authentication credentials i.e. your LambdaTest username, access key and HubURL. You need to set them up as your environment variables. You can retrieve them from your LambdaTest automation dashboard by clicking on the key icon near the help button.

For Linux/Mac:

$ export LT_USERNAME= {YOUR_LAMBDATEST_USERNAME}$ export LT_ACCESS_KEY= {YOUR_LAMBDATEST_ACCESS_KEY}

For Windows:

$ set LT_USERNAME= {YOUR_LAMBDATEST_USERNAME}$ set LT_ACCESS_KEY= {YOUR_LAMBDATEST_ACCESS_KEY}

Set Up Maven Sample Project & Dependencies#


Note: Make sure you clone our GitHub repository for Maven tunnel.

Navigate to the cloned directory & compile maven dependencies using the command:

$ mvn compile

Ensure that you have the Maven Tunnel dependency configured in your pom.xml.

<dependency>    <groupId>com.github.lambdatest</groupId>    <artifactId>lambdatest-tunnel-binary</artifactId>    <version>1.0.4</version></dependency>

Running Maven Tunnel#


Here is the code that is present in the cloned GitHub repository for Maven Tunnel.

package com.lambdatest.tunnel;
import java.net.URL;import java.util.HashMap;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeTest;import org.testng.annotations.Test;import com.lambdatest.tunnel.Tunnel;
public class MavenSingle {    Tunnel t;
    WebDriver driver = null;    public static String status = "passed";
    String username = System.getenv("LT_USERNAME");    String access_key = System.getenv("LT_ACCESS_KEY");

    @BeforeTest    public void setUp() throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();        capabilities.setCapability("build", "Single Maven Tunnel");        capabilities.setCapability("name", "Maven Tunnel");        capabilities.setCapability("platform", "Windows 10");        capabilities.setCapability("browserName", "Chrome");        capabilities.setCapability("version","latest");        capabilities.setCapability("tunnel",true);        capabilities.setCapability("network",true);        capabilities.setCapability("console",true);        capabilities.setCapability("visual",true);
        //create tunnel instance        t = new Tunnel();        HashMap<String, String> options = new HashMap<String, String>();        options.put("user", username);        options.put("key", access_key);
        //start tunnel        t.start(options);        driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"), capabilities);        System.out.println("Started session");    }
    @Test()    public void testTunnel() throws Exception {            //Check LocalHost on XAMPP            driver.get("http://localhost.lambdatest.com");            // Let's check that the item we added is added in the list.            driver.get("https://google.com");    } 
    @AfterTest    public void tearDown() throws Exception {        ((JavascriptExecutor) driver).executeScript("lambda-status=" + status);        driver.quit();        //close tunnel        t.stop();    }}

Parallel Testing With Maven Tunnel#


In order to trigger parallel test execution you will need to specify the parameters inside an XML file, in our GitHub repository you will find the XML file named as MavenParallel.xml file.

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"><suite thread-count="40" name="LambaTestSuite" parallel="tests">
    <listeners>        <listener class-name="com.lambdatest.tunnel.MavenParallel"/>    </listeners>       <test name="1 Chrome 12">   <parameter name="browser" value="Chrome"/>      <parameter name="version" value="latest"/>        <parameter name="platform" value="Windows 10"/>        <parameter name="resolution" value="1600x1200"/>        <classes>            <class name="com.lambdatest.tunnel.MavenParallel"/>        </classes>    </test>        <test name="2 Chrome 12">   <parameter name="browser" value="Chrome"/>      <parameter name="version" value="latest-1"/>        <parameter name="platform" value="Windows 8.1"/>        <parameter name="resolution" value="1920x1080"/>        <classes>            <class name="com.lambdatest.tunnel.MavenParallel"/>        </classes>    </test>        <test name="3 Chrome 12">   <parameter name="browser" value="Chrome"/>      <parameter name="version" value="latest-2"/>        <parameter name="platform" value="Windows 8"/>        <parameter name="resolution" value="2048x1536"/>        <classes>            <class name="com.lambdatest.tunnel.MavenParallel"/>        </classes>    </test>        <test name="4 Chrome 12">   <parameter name="browser" value="Chrome"/>      <parameter name="version" value="latest-3"/>        <parameter name="platform" value="Windows 7"/>        <parameter name="resolution" value="2560x1440"/>        <classes>            <class name="com.lambdatest.tunnel.MavenParallel"/>        </classes>    </test>     </suite>

Here is the final code to execute parallel testing with Maven tunnel.

package com.lambdatest.tunnel;
import java.net.MalformedURLException;import java.net.URL;import java.util.HashMap;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;import org.testng.IExecutionListener;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeTest;import org.testng.annotations.Test;import com.lambdatest.tunnel.Tunnel;

public class MavenParallel implements IExecutionListener{
    public RemoteWebDriver driver = null;    String status = "passed";    String username = System.getenv("LT_USERNAME");    String accessKey = System.getenv("LT_ACCESS_KEY");    Tunnel t;        @Override    public void onExecutionStart() {    try {          //start the tunnel          t = new Tunnel();          HashMap<String, String> options = new HashMap<String, String>();          options.put("user", username);          options.put("key", accessKey);          t.start(options);    } catch (Exception e) {          e.printStackTrace();    }    }
    @BeforeTest    @org.testng.annotations.Parameters(value={"browser","version","platform", "resolution"})    public void setUp(String browser, String version, String platform, String resolution) throws Exception {        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("build", "Parallel Maven Tunnel");        capabilities.setCapability("name", "Maven Tunnel");        capabilities.setCapability("browserName", browser);        capabilities.setCapability("version", version);        capabilities.setCapability("platform", platform);        capabilities.setCapability("tunnel",true);        capabilities.setCapability("network",true);        capabilities.setCapability("console",true);        capabilities.setCapability("visual",true);
        try {            driver= new RemoteWebDriver(new URL("https://"+username+":"+accessKey+"@hub.lambdatest.com/wd/hub"), capabilities);        } catch (MalformedURLException e) {            System.out.println("Invalid grid URL");        }    }       @Test()    public void testTunnel() throws Exception {            //Check LocalHost on XAMPP            driver.get("http://localhost.lambdatest.com");            // Let's check that the item we added is added in the list.            driver.get("https://google.com");    } 
    @AfterTest    public void tearDown() throws Exception {        if (driver != null) {            ((JavascriptExecutor) driver).executeScript("lambda-status=" + status);            driver.quit();        }    }        @Override       public void onExecutionFinish() {    try {          //stop the Tunnel;          t.stop();        } catch (Exception e) {        e.printStackTrace();        }           }}

Optional Arguments For LambdaTest Tunnel#


ARGUMENTSYNTAXDESCRIPTION
tunnelNametunnelArgs.put("tunnelName","YourName");To change the name of the LambdaTest Tunnel.
pidFiletunnelArgs.put("pidFile","Your/pid/path");To change the PID path.
dirtunnelArgs.put("dir","give/lambda/directory/path");To change the directory path.
logFiletunnelArgs.put("logFile","give/tunnel/log/directory/path");To change the path for Tunnel logs.

For more arguments, refer to our document of LambdaTest Tunnel Modifiers.

Last updated on