CAPABILITY GENERATOR

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

JUnit with Selenium – Running JUnit 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 JUnit automation test scripts on LambdaTest Selenium cloud platform. In this post we would be exploring:

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

Prerequisites For Running JUnit and Selenium


To run your Selenium and JUnit script, first you need to setup the environment.

  • You would JDK (Java Development Environment) 1.6 or higher. We recommend using the latest version.
  • You would also need latest Selenium client and it’s WebDriver Bindings. Selenium can be downloaded from here. You should always run latest version of Selenium Client and WeDriver on LambdaTest Selenium cloud grid to avoid any errors.
  • To test your locally or privately hosted files, you need LambdaTest Tunnel binary file.
  • It would be very beneficial if you use code project management options like Maven or Ant.
  • Maven supports JUnit out of the box. You would just have to define Selenium dependencies in it’s project object model file or pom.xml file.

    Maven Dependancy

First Test with JUnit


Now let’s start with a simple Selenium WebDriver test. Below is a JUnit Selenium script that will open a sample to-do application which will do following task:

  • Mark first two items as mark done.
  • Add a new item in the list.
  • Return the added item.

To run your first test with JUnit, follow the simple example below. Same code can be downloaded from our repository: Java-Junit-Selenium

JUnit Todo : Sample App

Execute The Test


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

Running First Test

Going Into Details – What is JUnit?


One of the most used Java Based Automation framework, JUnit, is an open source framework. JUnit is widely preferred by developers across the globe due to its data-driven testing support, flexible test configuration abilities, annotations and ability to execute and manage test cases. JUnit, most importantly, supports Selenium based automation.

If JUnit is your preferred framework then it is very easy to get started with LambdaTest Automation grid. JUnit + LambdaTest integration will instantly give you access to 2000+ browsers by giving automation build capabilities and enabling wider test coverage. JUnit has inherent capabilities to manage tests, coupled with this, you can boost your build speed by multiple times leveraging the flexibility of running multiple tests.

We are going to learn deeply that how to integrate JUnit Selenium testing suite with LambdaTest cloud grid. This will include, testing your locally hosted or privately hosted pages using tunnel feature and speeding up your test times by LambdaTest concurrent testing capabilities.

Understanding and Configuring JUnit 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 JUnit automation 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 the browser, browser versions etc. In simple terms, it would look something like this:

Remote Web Driver

In the JUnit automation code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object.

Our Capabilities Generator will automatically generate the program, based on your provided input. For Instance, if you select the below configurations:

Fields Selected Values
Operating System MacOS High Sierra
Browser Mozilla Firefox
Browser Version 64.0
Resolution 1280×1024
Selenium Version 3.13.0

Then Capabilities Generator at LambdaTest will provide you with the below program:

The most important capabilities to understand here are ‘browserName’, ‘versions’, 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.

You can also check out our documentation on capabilities generator to understand more about individual capabilities.

Advanced LambdaTest Capabilities

In addition to default Selenium Grid capabilities, LambdaTest also has platform specific capabilities like a video recording of test runs, console logs of each test run, network logs of each test run, custom profiles for specific browsers, etc. Do check out 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.

To perform cross browser testing for your locally stored web pages or web apps. You would need to download a binary zip file of Lambda Tunnel with respect to your device’s operating system.

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.

Syntax To Start Lambda Tunnel

So for example, if your user login email is example@lambdatest.com and your user key is 123asd123, then the command would be:

Once, the tunnel is successfully set up. You can add the below code to your capabilities for testing internal servers on your network.

Tunnel Capability

You can also add the capability using LambdaTest Capabilities Generator.

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

Mark Tests As Pass Or Fail


While performing automation testing using Selenium with Java on LambdaTest selenium grid, it is imperative to flag an automation test as either pass or fails. This is completely dependent on your testing requirement with respect to the validation of expected behaviour. You can mark whether an automation test passed or failed by adding the below code fragment in your automation script:

LambdaTest Annotation

Parallel Testing


Parallel Testing is one of the most demanding features 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 sessions 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 taken 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 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 LambdaTest, you can pace up your test build activities by performing parallel automation testing using Selenium with Java using JUnit. Boost quality assurance of your product by performing extensive and fast-paced automated cross browser testing process with LambdaTest parallel testing.

Check out the same code below to understand it better. You can also find it at our GitHub page here.

Parallel Testing Example


JUnit: Parallelized Class

Let us take a look at a reference of the above helper class for executing parallel test using JUnit.

JUnit Concurrent Todo : Sample App

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

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.