Selenium Automation has been trending over the internet for the past decade and has gained a lot of popularity among the Automation testers. Since selenium tests can be written in many languages like Java, JavaScript, Python, Ruby, Perl. C# and a lot more, this makes it more flexible for the users to be inclined towards automation testing using Selenium. In this article, we’ll show you how you perform live Selenium Automation Testing using Java on LambdaTest Automation testing tool.

About LambdaTest Automation Testing

LambdaTest is a cross-browser testing tool which offers Selenium Automation across 2000+ browsers, browser versions, operating systems, and resolution combinations. You can perform cross-browser testing of your website with the help of LambdaTest Automation testing tool and ensure the browser compatibility of your website with very fewer efforts in a very efficient and effective manner.

LambdaTest provides you Selenium automation in various languages and frameworks in which you can write your automation script which helps you in testing your website across thousands of browsers and devices combinations.

Languages and Frameworks supported in LambdaTest Automation Testing

Languages and Framework

With LambdaTest, you can perform Automation Testing using Python, Java, JavaScript, C-Sharp(C#), Ruby, and PHP using their frameworks.

  • For Python, you can perform testing using Behave Framework.
  • For Java, LambdaTest supports JUnit and TestNG Framework.
  • If you are comfortable with JavaScript, you can write your test script in Protractor, Nightwatch, WD, WebDriverIO, and Cucumber frameworks.
  • For C-sharp, LambdaTest Automation supports NUnit, MSTest, and SpecFlow frameworks.
  • In Ruby, RSpec and Test Unit frameworks are supported by LambdaTest Automation.
  • Behat+Mink and PHPUnit are the PHP Frameworks supported in LambdaTest Automation testing.

Continuous Integration (CI) Tools supported by LambdaTest Automation

CI Tools

There are various CI tools which you can integrate with LambdaTest Automation to ensure faster integration and deployment of your code. LambdaTest Automation integrates with Jenkins, Buildbot, Circle CI, Codeship, Continua, Cruise Control, Bamboo, GOCD, Solano CI,  TeamCity and Travis CI.

Automation Testing using Java

In this article, we’ll be running a Selenium Automation code using Java on LambdaTest Automation platform and see how LambdaTest helps us to perform cross-browser testing using the Selenium Script. We are using Java Specifically since I have an upper hand in it, however, other languages can also be used. You can find the complete documentation to perform step by step Selenium Automation testing using LambdaTest.

How to Perform Automation Testing using Java on LambdaTest platform

Prerequisites for Running Java Selenium test

So before we start writing code and get started with Selenium with Java, there are a few requirements. So, we need to install and download some of the required software:

1.Download and install Java Runtime Environment
2.Download Java Development Kit
3.Download and install all Java Selenium Files (Selenium Server Standalone)
4.Install Browser Specific Drivers ( In this blog, I’ll perform Automation on Chrome, so Chrome Driver for this case)

The first and foremost thing before starting running Automation script on LambdaTest online grid is to learn how to set up desired capabilities of tests. You might have requirements to run your tests across different browser, browser version, resolution, operating system etc, or you will need to define build name, test name or add capability to have network logs, capture browser console logs at various steps. All these things can be very easily set up in a few minutes with LambdaTest Capabilities Generator. It helps to understand more about how you can define running browser environments and leverage advanced LambdaTest capabilities.

The best part about LambdaTest capabilities generator is that you need to select the language and select the desired capabilities, and the code will be generated by the capability generator.

Lambda Test

As you can see on the right-hand side. If you select the language and the details of the Operating system, resolutions, browser, versions, etc you’ll get the code for the selected desired capabilities on the code window in the right-hand side.

Once you generate your capabilities, it’s time to write your code.

Let’s start with Java Selenium Script

In this example, we will be using IntelliJ code editor for writing the test script and Chrome web driver for running tests on Chrome browser.

Sample Code:

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
 
public class DemoTest {
 
	String username = "EnterYourUsername";
	String accesskey = "EnterYourAccessKeyHere";
	static RemoteWebDriver driver = null;
	String gridURL = "@hub.lambdatest.com/wd/hub";
	boolean status = false;
 
	public static void main(String[] args) {
    	new DemoTest().test();
	}
 
	public void test() {
    	setUp();
    	try {
        	driver.get("https://www.lambdatest.com/");
 
        	WebElement live = driver.findElement(By.xpath("//*[@id=\"bs-example-navbar-collapse-1\"]/ul/li[2]/a"));
        	live.click();
        	WebElement pricing = driver.findElement(By.xpath("//*[@id=\"bs-example-navbar-collapse-1\"]/ul/li[4]/a"));
        	pricing.click();
 
        	WebElement login = driver.findElement(By.xpath("//*[@id=\"bs-example-navbar-collapse-1\"]/ul/li[6]/a"));
        	login.click();
        	String windowHandle = driver.getWindowHandle();
        	WebElement TextBox = driver.findElement(By.xpath("//*[@id=\"app\"]/section/form/div/div/input[1]"));
        	TextBox.sendKeys("enteryouremailid");
        	WebElement Password = driver.findElement(By.xpath("//*[@id=\"app\"]/section/form/div/div/input[2]"));
        	Password.sendKeys("Enteryourpassword");
 
        	WebElement proceed = driver.findElement(By.xpath("//*[@id=\"app\"]/section/form/div/div/button"));
        	proceed.click();
 
       	 
 
    	} catch (Exception e) {
        	System.out.println(e.getMessage());
    	} finally {
        	tearDown();
    	}
	}
	private void setUp () {
    	DesiredCapabilities capabilities = new DesiredCapabilities();
    	capabilities.setCapability("build", "JavaAutomation");
    	capabilities.setCapability("name", "Java Test 1");
    	capabilities.setCapability("platform", "Windows 10");
    	capabilities.setCapability("browserName", "Chrome");
    	capabilities.setCapability("version","70.0");
    	capabilities.setCapability("resolution","1280x1024");
    	capabilities.setCapability("selenium_version","3.13.0");
    	capabilities.setCapability("visual",true);
    	capabilities.setCapability("chrome.driver",2.42);
 
    	try {
        	System.out.println("https://" + username + ":" + accesskey + gridURL);
        	driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
        	// System.out.println(driver);
    	} 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();
    	}
	}
 
}

Execute the Test

Once you are done with writing the Java code it’s time to run the script on LambdaTest online Selenium grid by clicking on the Run button.

Lambda Test online Selenium Grid

Once you click on Run, the test will start running on LambdaTest platform. Now you can go to LambdaTest automation tab to view the logs of the performed test.

LambdaTest allows a lot of added provisions and advantages to help you. Let’s check them out one by one.

Timeline

In this section, you can check all the Selenium test data like the number of tests run, test session period and test status. You can also filter out test sessions on the basis of Date, User, Build and status. All these metrics can be viewed in Test view and Build view.

Lambda Test Timeline

Automation Logs

This tab contains all the logs required for a tester for Selenium automation tests. When you click on the Run button, a new test will be created in your LambdaTest account and you can see all the related logs here as per desired capabilities. You will get access to your Command logs, network logs, visual logs, etc. You can also download your test logs, share over email, see the live video and do more actions with LambdaTest logs.

Lambda Test Automation Logs

Summary

You will see comprehensive Selenium test details in this section. This includes Test environment like Browser, browser version, Operating system and screen resolution. Also, you can watch the live test session while the test is being run.

Exception

All the exceptions in your Automation scripts will be reflected in this section.

Command

Screenshots will be generated at every command and stored in this tab. You can also mark as a bug if you found any issue in the screenshot with one click bug logging feature. If integrated with a bug management tool, you can also send your bugs directly over to the other bug management tools like Asana, Trello, Slack, JIRA, Butbucket, Gitlab, Github, VSTS, and a lot more.

Network

This will capture a recording for network packets while the test gets executed.

Logs

This will contain Selenium logs and console of your Selenium tests.

Meta data

Metadata under automation dashboard provides information related to input, meta and browser configuration.

Create Issue

You can directly log bug or issue found in your desired bug tracking or project management tool. LambdaTest platform offers 13 integrations to project bug tracking tools like Jira, Asana, Trello and more.

Analytics

LambdaTest analytics provides you with the data like how many tests you ran, Minutes consumed, Test passed, Test failed etc which can be viewed in both build view and test view. LambdaTest Analytics helps you track your improvements and help to build a better product.

Lambda Test Analytics

Frameworks for Java Selenium Automation

Now, if you want to use LambdaTest Automation Grid with a Java-based framework, then there are two testing frameworks available – JUnit and TestNG.

  • JUnit – It is an open-source testing framework for Java which is specially designed for Java developers to write and run repeatable tests. However, it can only be used for Unit Testing of a small portion of code.
  • TestNG – It is another open-source automated testing framework for Java along with JUnit. However, it has some advanced functionalities which make it more powerful and easy to use. For example, with TestNG, you can easily create a proper report and get to know how many test cases failed, passed, or skipped.

Both JUnit and TestNG frameworks have similar capabilities to Java. However, their usage depends upon your requirements and efficiency.

Conclusion

After running the code very easily and fluently I’d say that LambdaTest is one of the best tools for Selenium Automation. LambdaTest also provides a lot of added features and advantages over the other automation testing tools and helps in easy debugging and running of Selenium Scripts. Cross-browser testing with LambdaTest is a worth try.