Selenium is a popular open-source tool used for automating web applications. It provides a comprehensive set of features that make it an ideal choice for software testing and quality assurance. Selenium is composed of several components, including Selenium IDE, Selenium Remote Control, Selenium Web Driver, and Selenium Grid, each of which offers different functionality to help you automate your testing tasks. In this article, Novatesting will focus on Selenium WebDriver and show you how to use it with Java to automate testing of a web application. We’ll start by providing a brief overview of what Selenium is and its components, then we’ll cover how to prepare for practicing Selenium WebDriver. Finally, we’ll provide several Java code examples that demonstrate how to run Selenium on different browsers, use XPath and CSS Selectors to locate elements, and interact with them. Whether you’re just getting started with Selenium or you’re an experienced tester, this article will help you learn more about how Selenium works and how you can use it to automate your testing tasks.

  1. What is Selenium?

Selenium is an open-source software testing suite for automated testing of web applications. It provides a range of tools for automating the testing process, including the Selenium WebDriver which is the focus of this article.

  1. Components of Selenium

There are four components to the Selenium suite:

2.1. Selenium IDE

Selenium IDE is a Firefox plugin that records and plays back user interactions with a web application. It is a great tool for getting started with Selenium and quickly generating test cases.

2.2. Selenium Remote Control

Selenium Remote Control (RC) allows you to write automated tests for a web application in any programming language, including Java, C#, and Python. It provides a Selenium server that acts as a proxy between your test code and the browser.

2.3. Selenium Web Driver

Selenium WebDriver is a tool for writing automated tests of websites. It allows you to programmatically control a web browser and interact with a web page just like a user would. It is the successor to Selenium RC and provides a more modern and maintainable architecture.

2.4. Selenium Grid

Selenium Grid allows you to run your tests on multiple machines, in parallel, and across different operating systems and browsers. This is useful for reducing test execution time and allowing you to test your application in multiple environments.

  1. Preparing for practicing Selenium Webdriver

Before you can start using Selenium WebDriver, you need to have a basic understanding of the Java programming language and be familiar with the basics of writing and running Java code. You will also need to have the Java Development Kit (JDK) installed on your machine.

  1. JAVA Code Examples

The following Java code examples demonstrate how to use Selenium WebDriver to automate testing of a web application.

4.1. Run selenium on browsers

Browser Firefox

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirefoxExample {

public static void main(String[] args) {
    // Set the location of the Firefox driver executable
    System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");

    // Create a new instance of the Firefox driver
    WebDriver driver = new FirefoxDriver();

    // Navigate to the URL
    driver.get("https://www.example.com");

    // Find the element with the ID "search-input" and enter some text
    driver.findElement(By.id("search-input")).sendKeys("Selenium WebDriver");

    // Find the search button and click it
    driver.findElement(By.id("search-button")).click();
}

Browser Chrome

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeExample {

public static void main(String[] args) {
    // Set the location of the Chrome driver executable
    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

    // Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Navigate to the URL
driver.get("https://www.example.com");

// Find the element with the ID "search-input" and enter some text
driver.findElement(By.id("search-input")).sendKeys("Selenium WebDriver");

// Find the search button and click it
driver.findElement(By.id("search-button")).click();

}

}

4.2. Using XPath

XPath is a language used for selecting nodes from an XML document. In the context of Selenium, it can be used to locate elements on a web page. Here’s an example of how to use XPath to locate an element and interact with it:

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver;

public class XPathExample {

public static void main(String[] args) {
// Set the location of the Firefox driver executable
System.setProperty(“webdriver.gecko.driver”, “/path/to/geckodriver”);

// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();

// Navigate to the URL
driver.get("https://www.example.com");

// Find the element with the XPath "//input[@id='search-input']" and enter some text
driver.findElement(By.xpath("//input[@id='search-input']")).sendKeys("Selenium WebDriver");

// Find the search button and click it
driver.findElement(By.xpath("//button[@id='search-button']")).click();

}

public static void main(String[] args) {
// Set the location of the Firefox driver executable
System.setProperty(“webdriver.gecko.driver”, “/path/to/geckodriver”);

// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();

// Navigate to the URL
driver.get("https://www.example.com");

// Find the element with the XPath "//input[@id='search-input']" and enter some text
driver.findElement(By.xpath("//input[@id='search-input']")).sendKeys("Selenium WebDriver");

// Find the search button and click it
driver.findElement(By.xpath("//button[@id='search-button']")).click();

}

}

4.3. Using CSS Selector

CSS Selectors are patterns used to select elements from a style sheet. In the context of Selenium, they can be used to locate elements on a web page. Here’s an example of how to use CSS Selectors to locate an element and interact with it:

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver;

public class CSSSelectorExample {

public static void main(String[] args) {
// Set the location of the Firefox driver executable
System.setProperty(“webdriver.gecko.driver”, “/path/to/geckodriver”);

// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();

// Navigate to the URL
driver.get("https://www.example.com");

// Find the element with the CSS selector "#search-input" and enter some text
driver.findElement(By.cssSelector("#search-input")).sendKeys("Selenium WebDriver");

// Find the search button and click it
driver.findElement(By.cssSelector("#search-button")).click();

}

}

This article has provided a basic overview of Selenium and demonstrated how to use Selenium WebDriver with Java to automate testing of a web application. Whether you’re just getting started with Selenium or you’re an experienced tester, these examples should help you get a better understanding of how Selenium works and how you can use it to automate your testing tasks.

Keep in mind that these are just a few simple examples and there is much more that you can do with Selenium. Whether you’re testing a single web page or an entire application, Selenium offers a powerful and flexible testing solution that can help you achieve your goals.

In conclusion, Selenium is a great tool for automating web application testing, and Java is one of the most popular programming languages to use with Selenium. Whether you’re new to Selenium or you’re an experienced tester, these Java code examples should help you get started and learn more about how Selenium works.

Leave a Reply

Your email address will not be published. Required fields are marked *