CAPABILITY GENERATOR

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

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


LambdaTest offers support with Selenium WebDriver to help you instantly execute your automation test scripts. LambdaTest is a cloud-based, cross browser testing tool offering a Selenium Grid consisting of 2000+ browsers and browser versions running on real operating systems to pace up automation testing of your web-app or website. In this topic, you will learn how to automate your website testing using Selenium with TestNG on LambdaTest. In this post we would be exploring:

  • Running a sample test on LambdaTest Automation using Selenium WebDriver.
  • Setting up environment to test for the URLs in the internal network.
  • Core Capabilities: understanding and configuring them in your Selenium test suite.
  • Advanced features.

Getting Started With TestNG and LambdaTest


The journey starts with downloading TestNG. There are multiple ways you can download TestNG. For example, you can download latest TestNG packages here. Or if you are using Eclipse you can directly download it from Eclipse marketplace.

Checkout our blog on how to setup and run TestNG in an Eclipse Environment.

Prerequisites For Running TestNG and Selenium


Before running your first Selenium and TestNG script, you would need to setup the environment.

  • You would need java development environment i.e. JDK 1.6 or higher. We recommend using the latest version.
  • 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.
  • If you wish to test locally hosted or privately hosted projects, you would also need Lambda Tunnel binary file.
  • Though it is not a necessity, but it’s always advisable to use code project management options such as Maven or Ant.

Maven supports TestNG out of the box. You would just have to define Selenium dependencies in it’s project object model file or pom.xml file.

First Test with TestNG


Running first script of TestNG on LambdaTest Selenium grid is as easy as changing a few lines of code. To start with, you would have to invoke Selenium remote WebDriver instead of local browser WebDriver. In addition, since we are using remote WebDriver, we have to define which browser environment we want to run the test. We do that by passing browser environment details to LambdaTest Selenium grid via desired capabilities class.

Let’s checkout sample TestNG code running LambdaTest Selenium grid. This is a simple TestNG automation script that test a sample to-do list app by marking couple items as done, adding a new item to the list and finally displaying the count of pending items as output.

You can also find this code at our TestNG GitHub repository.

Things to notice in the code are values sent via DesiredCapabilities class.

Execute the test


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

Running First Script

Going Into Details – What is TestNG


TestNG is one of the most used Java based automation framework along with JUnit. It is an open source framework preferred by developers globally for it’s flexible test configuration abilities, support for data driven testing, annotations, and very impressive ability to manage and execute test cases. Most importantly, TestNG supports Selenium based automation.

If you are using TestNG, or planning to use TestNG, you can get started with LambdaTest Selenium grid quite easily. Integrating LambdaTest with TestNG will give your automation build capabilities to run on 2000+ different browsers, enabling you a wider test coverage. Coupled with TestNG’s inherent capabilities to manage tests, you get greater flexibility in running parallel test threads boosting your build speed by multiple times.

In this post, we are going to dive into how you can integrate LambdaTest Selenium cloud grid in your TestNG Selenium testing suite. We are also going to touch upon how you can take advantage of LambdaTest parallel testing capabilities to speed up your test times and how you can leverage Lambda Tunnel feature to test your locally hosted or privately hosted pages even before launch.

Understanding and Configuring TestNG Desired Capabilities


The first step in using LambdaTest Selenium gird is to invoke LambdaTest’s remote WebDriver. So if you are running a local Selenium test on for suppose in firefox, you would be using a code line something like this

Local Driver

When using LambdaTest Selenium grid, you would be invoking the Selenium remote webdriver like this

Remote Web Driver

In this example ‘username’ and ‘accesskey’ are your account’s identifiers and you can find them in your profile section. Checkout out this post to know more about it.

Running a remote grid also requires you to instruct the grid on which browser, browser versions, operating system, and resolution you wish to run the test on. This is done by passing these values in desiredCapabilites class. LambdaTest Selenium Grid follows a specific syntax in this class and you can get an idea about it in the code above. So for example, if you want to run a test on Chrome 70 in Windows 10 operating system, your DesiredCapabilities class’s parameters would look something like this

We have a nifty capabilities generator tool as well that can you can leverage to create these code blocks. The tool can also be used to configure LambdaTest platform’s advanced capabilities such as enabling/disabling video recording of the test run, console logs, etc.

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


To help you perform cross browser testing of your locally stored web pages, LambdaTest provides an SSH(Secure Shell) tunnel connection with the name Lambda Tunnel. With Lambda Tunnel, you can test your locally hosted files before you make them live over the internet. You could even perform cross browser testing from different IP addresses belonging to various geographic locations. You can also use LambdaTest Tunnel to test web-apps and websites that are permissible inside your corporate firewall. The SSH tunnel provided by LambdaTest acts as a proxy server for hosting your web pages from your local machine to Virtual machines running on LambdaTest cloud servers. Curious to know more about Lambda Tunnel?

Follow our documentation on Lambda Tunnel to know it all.

You can start tunnel by executing the below command in your cmd(Command Prompt) / terminal. Make sure that your pointer is routed in the same directory where you extract the downloaded Lambda Tunnel zip file.

So if your user name is lambdatest@example.com and key is 123456, the command would be

Once you are able to connect Lambda Tunnel successfully, you would just have to inform the gird to test via tunnel by simply adding one more line to DesiredCapabilities class.

Tunnel Reference: For a full reference of all tunnel features we support, visit our tunnel page.

Mark Test Status


The LambdaTest Selenium grid doesn’t identify if you test ‘passed’ or ‘failed’. It only identifies whether a test has executed to it’s last command or whether it has stopped/timed-out before it. The user would have to pass on these parameters to LambdaTest platform to make it possible for tracking these values at LambdaTest platform.

Make use of LambdaTest API or LambdaTest Annotations, for pushing additional meta data to LambdaTest or to track the pass/fail status of a test.

LambdaTest Annotation

Parallel Testing


Parallel Testing is one of the most demanding feature of LambdaTest Selenium Grid. By parallel testing, you can run more than one test case, simultaneously. This means that, Parallel testing would allow you to execute numerous automation test cases altogether. So you execute a single test scenario across different browsers or could run different test scenarios across the same browser but with different browser versions. Wondering how many parallel test cases can you run? That depends entirely on the number of concurrent session under your opted plan.

For instance, if you have a bucket of 100 automated test cases, and the average time for execution of a single test is around 6 minutes. The time taken normally to completely execute all your test cases sequentially would be 600 minutes i.e. 10 hours. However, if you are opting for a plan which offers 2 concurrent sessions then you can perform 2 parallel test cases. This would divide your time in half as the total time taken through parallel testing with 2 concurrent session for the above scenario would be 300 minutes i.e. 5 hours. Similarly, if you have got 4 concurrent sessions with you then the time take would be quarter with respect to sequential testing. This way you could fasten your release cycles as much as you want.

Test automation frameworks like TestNG are really useful in running parallel tests. For instance, in our above example, you may notice that we are running our test on 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 LambdaTest, you can pace up your test build activities by performing parallel automation testing using Selenium with Java using TestNG. Boost quality assurance of your product by performing extensive and fast-paced automated cross browser testing process with LambdaTest parallel testing.

Checkout the same code below to understand it better. You can also find it at our github page here.

Parallel Testing Example

Here is an example testng.xml file which would help you to run a single test on various browsers at the same time, you would also need to generate a testcase which make use of TestNG parameters (org.testng.annotations.Parameters).

Now you need to create a testng.xml file which will provide the test cases with parameters (browser combinations):

Queuing


To prevent over-exploitation of the platform we have added a capacity constraint on the number of tests that can be queued at our platform. Your maximum queue capacity will be dependent upon the number of concurrent sessions your LambdaTest account is eligible for. Below formula will help you calculate the maximum capacity for your queue.

Maximum Queuing Capacity

Maximum number of test cases that can be queued = n + 150

Here, n = number of concurrent sessions.

For instance, if you are opting for pricing where you can avail up to 10 concurrent sessions. Then the maximum number of test cases that can be queued will be equal to 10 + 150 i.e. 160 queued test cases. LambdaTest would automatically schedule and execute test cases for you.

LambdaTest Queuing Policy: Here you can get more information about LambdaTest Queuing Policy.

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