Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Selenium Automation Testing With LambdaTest

DZone 's Guide to

Selenium Automation Testing With LambdaTest

Automate testing with Selenium and LambdaTest

· Web Dev Zone ·
Free Resource

clear-test-tubes-in-pile


Automation testing has become a basic need for organizations that run their businesses online. It is a testing technique that is used to ensure that a web application works fine across all browsers, operating systems, and devices in terms of functionality, speed, and accuracy. There are multiple testing platforms and tools that can be used for automation testing like Robotium, TestComplete, SoapUI, and Ranorex.

But, there is no match for Selenium. It is the most recommended tool for automation testing by large as well as small organizations. So, if you’re starting out with automation testing and don’t know what selenium is and how it is beneficial for automation testing?

Then don’t worry! We’re here to help.

You may also like: Top Tips for Better Cross-Browser Testing.

In this article, we will discuss every aspect of selenium automation and how LambdaTest Selenium Grid can be used to perform selenium automation.

What Is Selenium?

Selenium is an automated testing tool that is specially designed to automate web application testing. With Selenium automation, test scripts automatically perform the same interaction over different browsers for a web application so that testers don’t have to perform it manually.

In usual terms, it is known as Selenium Suite because it is not an individual tool. Selenium is a collection of four major tools:

These four tools make Selenium's suite the most used and recommended tool for automation testing. Selenium can be used to perform any type of automated testing, but it is primarily used for automated cross-browser testing. Apart from that, it is an open-source tool, so there is no licensing cost involved, which is a huge advantage over other automation testing tools.

Now, let’s have a look at the other advantages that selenium has to offer for automation testing.

Benefits of Selenium Automation

The benefits of Selenium automation are endless. However, there are some unique benefits that make it the first choice of organizations for website testing. Let’s have a look at these benefits:

  • Open-source: As mentioned earlier, Selenium is an open source automated testing framework, which makes it a free and portable testing tool. Also, being an open source framework, it doesn’t involve any licensing cost, which is a significant advantage over other tools.

  • Uses Common Script for Cross Browser Testing: With Selenium, you can perform cross-browser testing across Chrome, Firefox, Safari, Internet Explorer, and Opera with only one test script on almost every platform like Windows, Linux, and Mac. Customers use different devices and platforms to access the internet; Selenium allows testers to improve their test coverage with just one test script over all the devices and platforms.

  • Earlier Detection of Defects: As a tester, you might know the importance of early defect detection, which leads to cost-effective code rework. Selenium not only detects defects earlier, but it also helps in maintaining the overall velocity of code development while ensuring that the functionality of the website is not compromised.

  • Testing Efficiency Improvement: Usually, automation testing takes less time to set up as compared to manual testing. But, selenium automation is even faster than other automation testing techniques. The time required to set up the selenium framework is much less than other tools, which make it the most efficient tool for website testing.

  • Multiple Language Support: One of the biggest challenges in automation testing is the incorporation of automation tools with the development environment for CI. Selenium support multiple programming languages like Java, Python, Perl, C#, Ruby, JavaScript, and many more, which makes it is easy to integrate with the development environment. Although it has its own script, it doesn’t limit to that specific language. Testers and developers can use it with any language that they are comfortable with.

  • Easy to Implement: The user-friendly interface of selenium makes it easy for the testers and developers to execute tests across various platforms. The open source features allow testers to write the script with their own extensions so that it becomes easy to develop customized actions. Also, the reporting capability of Selenium is one of the main reasons to choose it because it helps testers extract test results and take necessary action.

  • It’s not a Utility, it’s a Suite: As mentioned earlier, Selenium is not a single utility. It is a package of multiple testing tools, and that’s why it is referred to as a suite. Each of the selenium testing tools is designed for different testing requirements. For instance, Selenium has the capability to support Selenium IDE, Selenium Grid, Selenium WebDriver, and Selenium Remote Control.

  • Integrates Easily with Testing Tools: Selenium has its own set of standards, which allow it to be used with popular software development and testing tools easily. For example, it easily integrates SC compilers, such as Maven and ANT, cloud-grids like LambdaTest, CI systems like TeamCity and Jenkins, and many other test management tools.

These benefits clearly prove why Selenium is the best automation testing tool. After knowing these benefits, you definitely wanted to see how you can perform selenium automation with multiple languages for your web application.

We have got you covered for that too!

To perform Selenium automation, you need a grid that allows you to test web application with different programming languages across various browsers and devices. And LambdaTest proved to be a genuine and quality online selenium grid for performing cross browser testing as well as other web application testing.

Performing Selenium Automation With LambdaTest Selenium Grid

With LambdaTest Selenium Grid, you can perform automated cross browser testing across more than 2000 browsers and operating systems, which gives you higher test coverage. It helps you analyze how your web application renders across different browsers. Also, using Lambda Tunnel, you can perform automated cross browser testing of your locally hosted web pages. In addition, it supports all the languages and frameworks that are compatible with selenium automation like Java, PHP, Python, C#, Ruby, and JavaScript.

Here, we will discuss how LambdaTest can be used for selenium automation with different programming languages and frameworks. But, before that, it’s necessary to understand the capabilities of the LambdaTest Selenium Automation Grid.

Understanding the Capabilities of the LambdaTest Selenium Grid

LambdaTest Selenium Grid uses remote Webdriver instead of normal Selenium client browser drivers. So, if you want to migrate from locally run Selenium, then you will have to invoke LambdaTest Selenium remote WebDriver.

After that, you’ll have to specify the browser, browser versions, operating systems, and resolution that you want to run your test on, along with defining LambdaTest specific capabilities. You can check out the LambdaTest Capability Generator tool to know more about how you can define running browser environments and leverage advanced LambdaTest capabilities.

Now, let’s get back to perform selenium automation with different programming languages.

Getting Started with LambdaTest Selenium Grid

Java on LambdaTest Selenium Grid

To better understand LambdaTest Selenium Grid automation with Java, let’s take an example of Selenium Remote Webdriver test. Below is a program of Java code that tests a simple to-do application with normal functionalities, such as mark items as done, add items in the list, calculate total pending items, etc.


Java To-Do: Sample App

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


On running this test, a URL will be opened by Selenium Webdriver, mark the first two items as done in the list, add an item to the list, and return the total number of pending items. The test results will be displayed on the test console (or command-line interface if you are using terminal/cmd) and LambdaTest dashboard. LambdaTest Dashboard helps testers to view their text logs, screenshots and video recording for the entire Selenium tests.

Executing the Test

To execute the test, you need to run the below command in terminal/cmd.

cd to/file/location
javac -classpath ".:/path/to/selenium/jarfile:" JavaTodo.java
java -classpath ".:/path/to/selenium/jarfile:" JavaTodo


Example: 

cd /home/admin1/eclipseworkspace/Demojavac -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


Configuring Capabilities of Java for Selenium Automation

To connect with LambdaTest Selenium automation gird, first, you’ll have to configure your test scripts. In the above Java code, the first thing you might have noticed is the invoking of remote WebDriver instead of the native browser Webdrivers. So, if you want to run this test with Firefox browser on your local machine, you will have to use Firefox browser driver like this:

  • Local Driver

     FirefoxDriver driver = new FirefoxDriver(); 
    But, to run LambdaTest Selenium Grid, you’ll have to change it to remote WebDriver, and at the same time pass capabilities related to the browser, browser versions, etc. Then it will look something like this:

  • Remote Web Driver

WebDriver driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);


In the above code, we passed the browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object. The capabilities object in the above code can be defined as:

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


Here, the most important capabilities are browserName, version, and platform. These capabilities define which browser environment you want to run the test on. The remaining capabilities are important in test management and debugging.

Frameworks for Java Selenium Automation

Now, if you want to use the Lambdatest Selenium 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 that 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 and are supported on LambdaTest platform.

Similarly, users can run automated scripts written in programming languages like Javascript, Ruby, Python, C#, and PHP

Conclusion

Different web applications are developed in different languages, so it becomes challenging to automate your cross-browser testing with every programming language. But, Selenium provides support across multiple languages that make it easy to perform automation testing. 


Related Articles

Topics:
selenium - web browser automation ,selenium automation testing ,cross browser testing ,cross browser testing tool ,lambdatest ,selenium test automation ,selenium testing tutorial ,selenium tutorials ,browser testing ,web app testing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}