From Tests to Tasks: Selenium Practical Guide for Web Automation

Asara kumarasena
6 min readDec 29, 2023

--

What is Selenium?

Selenium is an open-source automation testing tool that supports multiple languages such as Java, Python, C#, Perl, PHP…etc, multiple web browsers such as Chrome, Firefox, Safari, IE…etc, and multiple operating systems such as Windows, Mac OS, Linux.

Usages of Selenium

Selenium is a versatile tool with a wide range of applications. While it’s commonly used for automating tests in web applications, its capabilities extend beyond testing, making it a valuable asset for browser automation in various scenarios.

  1. Repetitive Tasks

Selenium can be your go-to solution for automating repetitive tasks on the web. Whether you need to log into a website and download data or submit forms regularly, you can create Selenium scripts to execute these actions at predetermined intervals, saving you time and effort.

2. Web Scraping

If you’re looking to gather data from a website that hasn’t exposed an API, you can use the selenium to achieve that requirement. However, it’s essential to exercise caution and be aware of the website’s terms of service, as some sites may prohibit web scraping.

3. Testing

Selenium is a powerful tool for web testing, but effective testing involves making assertions on the actions performed by Selenium. There are many test Runners available in the field according to the programming languages.

Main Components

Selenium Test Suite contains three main components.

  • Selenium WebDriver
  • Selenium Integrated Development Environment (IDE)
  • Selenium Grid

Introducing WebDriver

When embarking on the journey of desktop or mobile website test automation, you’ll quickly become acquainted with WebDriver APIs. WebDriver operates just like a real user interacting with the browser such as clicking buttons, filling out forms, navigating between pages, and verifying content. So, software testers and developers can write scripts or code to simulate user interactions with a web application. So you should have some programming skills to use the WebDriver.

Further, it allows you to run tests in parallel, which can significantly speed up test execution and improve testing efficiency. Moreover, It can be integrated with testing frameworks like TestNG, and JUnit(It is used in our example of the selenium test suite)

Streamlining Test Case Development with IDE

Integrated Development Environment( IDE )takes the form of a user-friendly Chrome and Firefox extension and is widely recognized as the most efficient way to create test cases. This ingenious tool captures a user’s interactions with the browser, automatically generating Selenium commands in the process, complete with parameters tailored to the context of the web elements involved.

Empowering Test Automation with Selenium Grid

After mastering WebDriver and IDE, you may find yourself faced with the need to expand your testing horizons. Imagine having to run your tests on a variety of browsers and operating systems to ensure comprehensive compatibility. Here, Selenium Grid steps into the spotlight.

Selenium Grid is a powerful tool that empowers you to distribute your test cases across different machines and platforms. The beauty of this system is that it grants you control over when to initiate your tests from a local end, while the actual test execution occurs seamlessly on a remote end. This architecture facilitates parallel testing on diverse environments, greatly enhancing efficiency and coverage in your test automation efforts.

So in this article, I am going to discuss how we can use Selenium Web Driver and JUnit testing framework for a practical scenario.

Configure Selenium Web Driver

Configurations depend on the language you choose to write test suites and in this article, I am explaining how to configure selenium web driver in JAVA.

I created a Maven project to implement the test suite and you can add the Maven dependency of the selenium web driver and JUnit as below to your pom.xml file. Ok, wait, What is JUnit?

JUnit is a widely used open-source testing framework to better organize your code and it provides annotations to identify test methods, asserts for testing expected results, and test runners for running tests.

Annotations:- @Test ,@Before ,@After ,@BeforeEach ,@AfterEach

Assertions:- assertEquals, assertTrue, assertFalse , assertNull, assertSame

Here, I used the current latest web driver and you can check the Selenium download page to find the latest version.

        <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.14.1</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>compile</scope>
</dependency>

Let’s write a Selenium JAVA Script

Use Case:- Go to https://bulletins.wayne.edu/courses/. From there have the test suite locate and click the “Computer Science” link. This should land you on https://bulletins.wayne.edu/courses/csc/. Subsequently, verify the list of CSC courses that contain the “CSC 5991” special topics course.

First, create a test case for this use case before start writing the script.

IDENTIFIER: TC001

TEST CASE: Verify "CSC 5991" in Computer Science Course List

PRECONDITIONS:
• The web browser is open.
• The WebDriver is initialized.

INPUT VALUES:
URL: https://bulletins.wayne.edu/courses/

EXECUTION STEPS:
• Open the web browser and navigate to the URL: https://bulletins.wayne.edu/courses/
• Locate and click on the "Computer Science" link.
• Wait for the page to load.
• Verify that the current URL is now https://bulletins.wayne.edu/courses/csc/.
• Find the list of Computer Science courses on the page.
• Check if "CSC 5991" (special topics course) is present in the list.

OUTPUT VALUES:
• If "CSC 5991" is found in the list of Computer Science courses, mark the test as "Passed."
• If "CSC 5991" is not found, mark the test as "Failed."

POSTCONDITIONS:
• Close the web browser.

Here is the complete Java script for the above use case.

package org.example;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Courses {
@Test
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
String title =null;

// Load Courses page
driver.get("https://bulletins.wayne.edu/courses/");
driver.getTitle();
title = driver.getTitle();
// Check Web Page title is equal to the expected one
assertEquals("Complete Course List < Wayne State", title);
//wait 500 milisceconds until loading the web page
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));

// Click the "Computer Science" link
WebElement computerScienceLink = driver.findElement(By.linkText("CSC - Computer Science"));
computerScienceLink.click();
title = driver.getTitle();driver.getTitle();
// Check Web Page title is equal to the expected one
assertEquals("CSC - Computer Science < Wayne State", title);
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));

// Locate the element that contains "CSC 5991" special topics course
WebElement courseElement = driver.findElement(By.xpath("//*[contains(text(), 'CSC 5991')]"));

// Check if the element is present on the page
if (courseElement.isDisplayed()) {
System.out.println("The 'CSC 5991' special topics course is available on the webpage.");
} else {
System.out.println("The 'CSC 5991' special topics course is not available on the webpage.");
}

driver.quit();
}
}

The complete pom.xml file should be like this.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>SeleniumProject</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.14.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Configure the maven-assembly-plugin to package an executable JAR with dependencies -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.example.Courses</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

So in this program, I’ve used the maven-assembly-plugin to compile the program with dependencies and once you run the “mvn clean install command”, you can get the executable jar file from the target folder and you can use the below command to run the .jar file.

java -jar SeleniumProject-1.0-jar-with-dependencies.jar

You have to keep in mind that the Selenium web driver version and your web browser version should be matched to run the scripts without any issues. You can find more information regarding this here. Further, in this example I used only a few selenium elements and you can refer to the official website for more information about Elements and how they can be used in different use cases.

That’s it. Until the next article, Happy Automation 🥳🥳

--

--

Asara kumarasena

Graduate Student @Wayne State University | Former Software Engineer @WSO2