LambdaTest Selenium Automation Grid is a cloud-based, scalable Selenium testing platform which enables you to run your automation scripts on 2000+ different browsers and operating systems. You can now run your automation test cases using Java with Selenium on a scalable Selenium infrastructure that is running real browsers and real operating systems.
This post will help you in getting started with configuring and running your Java automation testing scripts on LambdaTest Selenium cloud platform. In this post we would be exploring:
The first step in using LambdaTest platform is to understand LambdaTest’s Selenium Grid capabilities. Our Selenium Grid uses remote webdriver instead of normal Selenium client browser drivers so if you are migrating from locally run Selenium, you would have to invoke LambdaTest Selenium remote WebDriver. Next, you need to specify in your code, which browser, browser versions, operating systems, and resolution you wish to run your test on, along with defining LambdaTest specific capabilities. You can check out LambdaTest Capabilities Generator tool to understand more about how you can define running browser environments and leverage advanced LambdaTest capabilities.
Let’s start with a simple Selenium Remote Webdriver test first. The Java script below tests a simple to-do application with basic functionalities like mark items as done, add items in a list, calculate total pending items etc. You can also find this at GitHub repository.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class JavaTodo { String username = "YOUR_USERNAME"; String accesskey = "YOUR_ACCESS_KEY"; static RemoteWebDriver driver = null; String gridURL = "@hub.lambdatest.com/wd/hub"; boolean status = false; public static void main(String[] args) { new JavaTodo().test(); } public void test() { // To Setup driver setUp(); try { //Change it to production page driver.get("https://lambdatest.github.io/sample-todo-app/"); //Let's mark done first two items in the list. driver.findElement(By.name("li1")).click(); driver.findElement(By.name("li2")).click(); // Let's add an item in the list. driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list"); driver.findElement(By.id("addbutton")).click(); // Let's check that the item we added is added in the list. String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText(); if (enteredText.equals("Yey, Let's add it to list")) { status = true; } } catch (Exception e) { System.out.println(e.getMessage()); } finally { tearDown(); } } private void setUp() { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("browserName", "chrome"); capabilities.setCapability("version", "70.0"); capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get any available one. capabilities.setCapability("build", "LambdaTestSampleApp"); capabilities.setCapability("name", "LambdaTestJavaSample"); capabilities.setCapability("network", true); // To enable network logs capabilities.setCapability("visual", true); // To enable step by step screenshot capabilities.setCapability("video", true); // To enable video recording capabilities.setCapability("console", true); // To capture console logs try { driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities); } catch (MalformedURLException e) { System.out.println("Invalid grid URL"); } catch (Exception e) { System.out.println(e.getMessage()); } } private void tearDown() { if (driver != null) { ((JavascriptExecutor) driver).executeScript("lambda-status=" + status); driver.quit(); //really important statement for preventing your test execution from a timeout. } } } |
The Selenium Webdriver test would open a URL, mark the first two items in the list as done, add an item in the list, and return the total number of pending items. Your results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest dashboard. LambdaTest Dashboard will help you view all your text logs, screenshots and video recording for your entire Selenium tests.
You would need to execute the below command in your terminal/cmd.
1 2 3 4 5 6 7 8 9 |
cd to/file/location compile the test file: javac -classpath ".:/path/to/selenium/jarfile:" JavaTodo.java Run the test: java -classpath ".:/path/to/selenium/jarfile:" JavaTodo Example: cd /home/admin1/eclipseworkspace/Demo javac -classpath ".:/home/admin1/eclipseworkspace/Demo/src/selenium-server-standalone-3.14.0.jar:" JavaTodo.java java -classpath ".:/home/admin1/eclipseworkspace/Demo/src/selenium-server-standalone-3.14.0.jar:" JavaTodo |
As we said earlier, the first step is to configure your test scripts to connect with LambdaTest Selenium automation gird. In the above mentioned Java code, the first thing you would notice is the invoking of remote WebDriver instead of the native browser WebDrivers. So for example if you are planning to run on Firefox browser in your local machine, you would be using Firefox browser driver like this:
Local Driver
1 |
FirefoxDriver driver = new FirefoxDriver(); |
However, to run on LambdaTest Selenium grid, you would have to change it remote WebDriver and at the same time pass capabilities related to browser, browser versions etc. In simple terms, it would look something like this:
Remote Web Driver
1 |
WebDriver driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities); |
In this code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object. The capabilities object in the above code is defined as:
1 2 3 4 5 6 7 8 9 10 |
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("browserName", "chrome"); capabilities.setCapability("version", "70.0"); capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one capabilities.setCapability("build", "LambdaTestSampleApp"); capabilities.setCapability("name", "LambdaTestJavaSample"); capabilities.setCapability("network", true); // To enable network logs capabilities.setCapability("visual", true); // To enable step by step screenshot capabilities.setCapability("video", true); // To enable video recording capabilities.setCapability("console", true); // To capture console logs |
The most important capabilities to understand here are ‘browserName’, ‘version’, and ‘platform’. They define which browser environment you wish to run the test on. Rest of the capabilities are important in test management and debugging. We have an inbuilt Capabilities Generator tool as well that you use to generate capabilities code for your test suite.
In addition to default Selenium Grid capabilities, LambdaTest also has platform specific capabilities like video recording of test runs, console logs of each test run, network logs of each test run, custom profiles for specific browsers, etc. Do checkout our documentation on LambdaTest Advanced capabilities as well.
You can test your locally hosted or privately hosted projects with LambdaTest Selenium grid cloud using Lambda Tunnel app. All you would have to do is set up an SSH tunnel using Lambda Tunnel app, and pass toggle tunnel = True via desired capabilities. Lambda Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are made live.
Here’s how you can establish Lambda Tunnel.
Download latest LambdaTest Tunnel binary file and extract it.
Open command prompt and navigate to the binary folder
Run the following command
LT -user {user’s login email} -key {user’s access key}
So if your user name is lambdatest@example.com
and key is 123456
, the command would be
LT -user lambdatest@example.com -key 123456
Once you are able to connect Lambda Tunnel successfully, you would just have to pass on tunnel capabilities
Add the below code snippet into your desired capability.
Tunnel Capability
1 2 |
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("tunnel", true); |
By default, LambdaTest platform do not know whether the test that has been executed passes the tests or fails the test. For that, the users have to define pass or fail conditions as assertions in their test execution code and pass on the result to LambdaTest platform via our APIs. This will help you manage your pass and fail results of your test suite at LambdaTest platform.
1 2 3 |
//LambdaTest Annotation ((JavascriptExecutor) driver).executeScript("lambda-status=passed"); |
One of the most important features of LambdaTest Selenium grid is the ability to run your test cases in parallel. What that means is that if you have more than one concurrent session, you can run your test cases on more than one machine at a time, which greatly cuts down your test times. To put it in perspective, if you have 100 test cases each with an average run time of 1 minute, without parallel testing it would take 100 minutes to execute. However, with 2 concurrent sessions, you can run 2 test cases in parallel at a time and can cut down the build’s test time to 50 minutes. With four concurrent sessions, it would cut down to 25 minutes. With eight, well you got the picture (smile)
This reduced test times now open up possibilities of increasing your test coverage over more browsers and more operating systems.
Test automation frameworks like TestNG and JUnit are really useful in running parallel tests. For instance, in our above example, you may notice that we are running our test in a single environment. If you want to cover multiple environments you would have to change the hard code every time. Or you would have to use arrays, or multi-threading or something similar.
With TestNG however, it’s very easy. You can define which browsers you want to run on in parameters of your test suite file testng.xml and while running test code just use parameter annotations @org.testng.annotations.Parameters(value={“browser”, “version”, “platform”})
Checkout the same code below to understand it better. You can also find it at our GitHub repository.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import org.openqa.selenium.By; import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.net.MalformedURLException; import java.net.URL; public class TestNGTodo { public String username = "YOUR_USERNAME"; public String accesskey = "YOUR_ACCESS_KEY"; public static RemoteWebDriver driver = null; public String gridURL = "@hub.lambdatest.com/wd/hub"; boolean status = false; @BeforeClass @org.testng.annotations.Parameters(value={"browser","version","platform"}) public void setUp() throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("browserName", "chrome"); capabilities.setCapability("version", "70.0"); capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one capabilities.setCapability("build", "LambdaTestSampleApp"); capabilities.setCapability("name", "LambdaTestJavaSample"); capabilities.setCapability("network", true); // To enable network logs capabilities.setCapability("visual", true); // To enable step by step screenshot capabilities.setCapability("video", true); // To enable video recording capabilities.setCapability("console", true); // To capture console logs try { driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities); } catch (MalformedURLException e) { System.out.println("Invalid grid URL"); } catch (Exception e) { System.out.println(e.getMessage()); } } @Test public void testSimple() throws Exception { try { //Change it to production page driver.get("https://lambdatest.github.io/sample-todo-app/"); //Let's mark done first two items in the list. driver.findElement(By.name("li1")).click(); driver.findElement(By.name("li2")).click(); // Let's add an item in the list. driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list"); driver.findElement(By.id("addbutton")).click(); // Let's check that the item we added is added in the list. String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText(); if (enteredText.equals("Yey, Let's add it to list")) { status = true; } } catch (Exception e) { System.out.println(e.getMessage()); } } @AfterClass public void tearDown() throws Exception { if (driver != null) { ((JavascriptExecutor) driver).executeScript("lambda-status=" + status); driver.quit(); } } } |
Here’s the associate testng.xml suite file for performing Java automation testing.
TestNG.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite thread-count="3" name="LambaTestSuite" parallel="tests"> <test name="WIN8TEST"> <parameter name="browser" value="firefox"/> <parameter name="version" value="62.0"/> <parameter name="platform" value="WIN8"/> <classes> <class name="lambdatest.TestNGTodo "/> </classes> </test> <!-- Test --> <test name="WIN10TEST"> <parameter name="browser" value="chrome"/> <parameter name="version" value="70.0"/> <parameter name="platform" value="WIN10"/> <classes> <class name="lambdatest.TestNGTodo "/> </classes> </test> <!-- Test --> <test name="MACTEST"> <parameter name="browser" value="safari"/> <parameter name="version" value="11.0"/> <parameter name="platform" value="macos 10.13"/> <classes> <class name="lambdatest.TestNGTodo"/> </classes> </test> <!-- Test --> </suite> |
If you notice, with simple annotations and parameters, you can now run your test suite on multiple browsers every time, without changing the browsers parameters in code files every time.
To prevent abuse of the platform we have added limitation on the number of tests you can queue at our platform and have added a limitation on total time a queued item will remain in queue before it’s is timeout. Number of test session that you can add to the queue is defined by your concurrency plan. This formula will help you understand how many you can plan your test case queues.
Maximum Queue Capacity for Java Automation Testing
Maximum number of test cases that can be queued = n + 150
Here, n = number of concurrent sessions.
That means, that if you are on a 10 concurrent session plan, you can add 10+150 i.e. 160 java automation tests in queue at LambdaTest platform.
Here you can get more information about LambdaTest Queuing Policy.
Queuing Timeout
There is also a limit on how long a test case can stay in the queue. If your test case stays in the queue for more than 15 minutes, the test case would be timed out and would not be executed.