CAPABILITY GENERATOR

LambdaTest DocumentationAutomation Testing ● Java with Selenium – Running Java Automation Scripts on LambdaTest Selenium Grid

Java with Selenium – Running Java Automation Scripts on LambdaTest Selenium Grid


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 Java Selenium automated test cases 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 based automation test scripts on LambdaTest Selenium cloud platform. In this post we would be exploring:

  • How to run a Java and Selenium script on LambdaTest Selenium Cloud
  • How to specify which browsers to run the test on
  • How to run your test cases in parallel to reduce build times
  • How to test your locally hosted pages
  • How to leverage LambdaTest’s Advanced capabilities.

Prerequisites For Running Java With Selenium


Before you can start running your Selenium tests with Java:

  • You would need Java development environment i.e. JDK 1.6 or higher. We recommend using the latest version.
  • You would need latest Selenium Client and it’s WebDriver bindings. You can download latest Selenium client here https://docs.seleniumhq.org/download/. Latest versions of Selenium Client and WebDriver are ideal for running your automation script on LambdaTest Selenium cloud grid.
  • You would need Lambda Tunnel binary file for testing your locally or privately hosted projects.

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.

Getting Started With Java & Selenium


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.

Java Todo : Sample App

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.

Execute The Test


You would need to execute the below command in your terminal/cmd.

Understanding & Configuring Java Desired Capabilities


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

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

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:

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.

Advanced LambdaTest Capabilities


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.

Testing Locally Hosted or Privately Hosted Projects


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.

Refer our LambdaTest Tunnel documentation for more information.

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

Mark Tests As Pass or Fail


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.

Parallel Testing


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.

TestNG Todo : Sample App

Here’s the associate testng.xml suite file for it.

TestNG.xml

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.

Queuing


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

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 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.