← Back to Home

Introduction to Selenium

🌟 What is Selenium?

Selenium is an open-source tool for automating web applications across different browsers. It supports multiple programming languages like Java, Python, C#, Ruby, and JavaScript, making it a popular choice for testers worldwide.

🌟 Why Selenium?

🌟 How to Install Selenium

  1. Install Java JDK if not installed
  2. Install Eclipse IDE or IntelliJ IDEA for Java development
  3. Download Selenium WebDriver Java bindings from selenium.dev
  4. Add Selenium JAR files to your project’s build path
  5. Install ChromeDriver for your browser from ChromeDriver site

🌟 Creating Your First Selenium Script

Here’s a simple Java Selenium script that will open Google and search for β€œSelenium”:


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

public class FirstSeleniumScript {
  public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.google.com");
    driver.findElement(By.name("q")).sendKeys("Selenium");
    driver.findElement(By.name("q")).submit();
    System.out.println("Page Title is: " + driver.getTitle());
    driver.quit();
  }
}
  

🌟 Best Practices

🌟 Sample Website for Practice

You can practice Selenium on this sample site: OrangeHRM Demo

Selenium Locators Explained

🌟 What are Locators?

Locators help Selenium identify elements on a web page to perform actions like click, type, or verify. They are critical for any Selenium automation script.

🌟 Types of Locators in Selenium

🌟 Code Examples


// By ID
driver.findElement(By.id("username")).sendKeys("admin");

// By Name
driver.findElement(By.name("password")).sendKeys("admin123");

// By Class Name
driver.findElement(By.className("submit-btn")).click();

// By Tag Name
List<WebElement> inputs = driver.findElements(By.tagName("input"));

// By Link Text
driver.findElement(By.linkText("Forgot Password?")).click();

// By Partial Link Text
driver.findElement(By.partialLinkText("Forgot")).click();

// By CSS Selector
driver.findElement(By.cssSelector("input[type='text']")).sendKeys("sample");

// By XPath
driver.findElement(By.xpath("//button[@type='submit']")).click();
  

🌟 Best Practices

🌟 Sample Project

Try identifying these elements on the sample demo website: OrangeHRM Demo.

Use these locators in your practice scripts to automate login functionality.

XPath in Selenium

🌟 What is XPath?

XPath stands for XML Path Language. It is a powerful way to navigate through HTML or XML elements to locate them precisely. Selenium uses XPath to find dynamic elements on web pages.

🌟 Types of XPath

🌟 XPath Syntax Basics

🌟 XPath Code Examples


// absolute example
driver.findElement(By.xpath("/html/body/div/form/input")).sendKeys("hello");

// relative example
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("admin");

// contains()
driver.findElement(By.xpath("//button[contains(text(),'Login')]")).click();

// text()
driver.findElement(By.xpath("//h1[text()='Dashboard']")).isDisplayed();
  

🌟 Best Practices

🌟 Sample Practice

Try finding elements on the OrangeHRM demo:

Actions Class in Selenium

🌟 What is the Actions Class?

The Selenium Actions class helps you handle advanced user interactions such as mouse hover, drag and drop, double click, right click, and more. These are often needed for modern web applications with dynamic behavior.

🌟 Common Actions Supported

🌟 How to Use the Actions Class


import org.openqa.selenium.interactions.Actions;

WebDriver driver = new ChromeDriver();
Actions actions = new Actions(driver);

WebElement menu = driver.findElement(By.id("menu"));

actions.moveToElement(menu).perform(); // mouse hover

actions.contextClick(menu).perform();  // right click

actions.doubleClick(menu).perform();   // double click
  

🌟 Drag and Drop Example


WebElement source = driver.findElement(By.id("draggable"));
WebElement target = driver.findElement(By.id("droppable"));

actions.dragAndDrop(source, target).perform();
  

🌟 Best Practices

🌟 Sample Practice

Try practicing mouse hover or drag-and-drop on:

Handling Dropdowns in Selenium

🌟 What is a Dropdown?

A dropdown (or select element) lets users choose one or more options from a list. In Selenium, dropdowns can be handled using the Select class in Java.

🌟 Using the Select Class

Selenium provides a built-in Select class that works with HTML <select> tags.

🌟 Basic Example


import org.openqa.selenium.support.ui.Select;

WebElement dropdown = driver.findElement(By.id("country"));
Select select = new Select(dropdown);

// select by visible text
select.selectByVisibleText("India");

// select by index
select.selectByIndex(2);

// select by value attribute
select.selectByValue("US");
  

🌟 Working with Multi-select


if(select.isMultiple()) {
   select.selectByVisibleText("Option1");
   select.selectByVisibleText("Option2");
}
  

🌟 Best Practices

🌟 Sample Practice

Try practicing on a sample site: Guru99 Register Demo

Handling Alerts in Selenium

🌟 What is an Alert?

An alert is a small popup window that displays a message or asks for confirmation (like JavaScript alerts, confirms, or prompts). Selenium can handle alerts using its built-in Alert interface.

🌟 Types of Alerts

🌟 Basic Example


Alert alert = driver.switchTo().alert();

// accept the alert
alert.accept();

// dismiss (if confirm box)
alert.dismiss();

// get alert text
String msg = alert.getText();

// send text to a prompt
alert.sendKeys("sample text");
  

🌟 Best Practices

🌟 Sample Practice

You can try practice alerts here: DemoQA Alerts

Waits in Selenium

🌟 Why Waits are Important?

Web pages often load elements dynamically, so actions might fail if Selenium tries to interact before elements are ready. Waits help synchronize your tests with the application to avoid flakiness.

🌟 Types of Waits

🌟 Code Examples


// Implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// Explicit wait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));

// Fluent wait
Wait fluentWait = new FluentWait<>(driver)
    .withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofSeconds(5))
    .ignoring(NoSuchElementException.class);

WebElement foo = fluentWait.until(driver -> driver.findElement(By.id("foo")));
  

🌟 Best Practices

🌟 Sample Practice

Try testing waits on dynamic elements at: Herokuapp Dynamic Loading

TestNG Framework in Selenium

🌟 What is TestNG?

TestNG is a popular testing framework for Java that makes it easier to organize, execute, and report Selenium tests. It supports annotations, parallel execution, groups, priorities, and data-driven testing.

🌟 Why use TestNG with Selenium?

🌟 Basic TestNG Example


import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class LoginTest {
    @BeforeClass
    public void setup() {
        System.out.println("Open Browser");
    }
    @Test
    public void login() {
        System.out.println("Perform Login");
    }
    @AfterClass
    public void tearDown() {
        System.out.println("Close Browser");
    }
}
  

🌟 TestNG XML Configuration


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MySuite">
  <test name="MyTest">
    <classes>
      <class name="LoginTest"/>
    </classes>
  </test>
</suite>
  

🌟 Best Practices

🌟 Sample Practice

You can practice running TestNG tests in Eclipse or IntelliJ by installing the TestNG plugin from the marketplace.

DataProvider in TestNG

🌟 What is DataProvider?

DataProvider is a feature in TestNG that allows you to pass multiple sets of data to a single test method. This helps build data-driven tests without duplicating code.

🌟 Why use DataProvider?

🌟 Basic DataProvider Example


import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class LoginDataTest {

  @DataProvider(name = "loginData")
  public Object[][] getData() {
      return new Object[][] {
          {"admin", "admin123"},
          {"user", "user123"},
          {"guest", "guest123"}
      };
  }

  @Test(dataProvider = "loginData")
  public void loginTest(String username, String password) {
      System.out.println("Logging in with: " + username + " / " + password);
  }
}
  

🌟 Best Practices

🌟 Sample Practice

Practice creating a DataProvider for a login test on: OrangeHRM Demo

Parameterization in Selenium

🌟 What is Parameterization?

Parameterization means passing different input values to your test scripts without hardcoding them, making your tests reusable and data-driven.

🌟 How to do Parameterization in Selenium?

🌟 Example with TestNG XML Parameters


// testng.xml
<parameter name="username" value="admin"/>

// Java
@Parameters({"username"})
@Test
public void testLogin(String user) {
    System.out.println("Logging in with: " + user);
}
  

🌟 Example from Excel (Apache POI)


// simplified example
FileInputStream fis = new FileInputStream("testdata.xlsx");
Workbook wb = WorkbookFactory.create(fis);
Sheet sh = wb.getSheet("Login");
String username = sh.getRow(0).getCell(0).getStringCellValue();
  

🌟 Best Practices

🌟 Sample Practice

Try parameterizing login data for OrangeHRM Demo with different usernames and passwords.

Page Object Model (POM) in Selenium

🌟 What is POM?

The Page Object Model (POM) is a design pattern in Selenium that helps you maintain clean, reusable, and scalable test code by separating page locators and actions into dedicated classes.

🌟 Why use POM?

🌟 Basic POM Example

Let’s automate a simple login page.


// LoginPage.java
public class LoginPage {
  WebDriver driver;

  @FindBy(id="username") WebElement usernameField;
  @FindBy(id="password") WebElement passwordField;
  @FindBy(id="loginBtn") WebElement loginButton;

  public LoginPage(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this);
  }

  public void login(String user, String pass) {
    usernameField.sendKeys(user);
    passwordField.sendKeys(pass);
    loginButton.click();
  }
}
  

Then in your test class:


// LoginTest.java
LoginPage lp = new LoginPage(driver);
lp.login("admin", "admin123");
  

🌟 Best Practices

🌟 Sample Practice

Try implementing POM for the login flow of OrangeHRM Demo.

Hybrid Framework in Selenium

🌟 What is a Hybrid Framework?

A Hybrid Framework combines multiple automation frameworks such as Data-Driven, Keyword-Driven, and Page Object Model. It takes the best features of each and creates a flexible, scalable, and reusable structure for automation.

🌟 Why use a Hybrid Framework?

🌟 How does it work?

Typically, a Hybrid Framework has:

🌟 Example Folder Structure


/tests
  LoginTest.java
/pages
  LoginPage.java
/keywords
  KeywordLibrary.java
/utils
  ExcelUtils.java
/testdata
  LoginData.xlsx
/reports
  extent-report.html
  

🌟 Best Practices

🌟 Sample Practice

Try building a hybrid framework combining DataProvider, POM, and reusable keyword methods for: OrangeHRM Demo.

Behavior Driven Development (BDD) with Cucumber

🌟 What is BDD?

Behavior Driven Development (BDD) is a software development approach where test scenarios are written in plain English using a language called Gherkin. BDD bridges the gap between business, development, and testing teams by making test cases more readable and collaborative.

🌟 Why use Cucumber with Selenium?

🌟 Sample Gherkin Feature File


Feature: Login functionality

  Scenario: Successful login
    Given user is on login page
    When user enters valid credentials
    Then user should be redirected to the dashboard
  

🌟 Sample Step Definition


@Given("user is on login page")
public void user_is_on_login_page() {
  driver.get("https://opensource-demo.orangehrmlive.com/");
}

@When("user enters valid credentials")
public void user_enters_valid_credentials() {
  driver.findElement(By.id("txtUsername")).sendKeys("Admin");
  driver.findElement(By.id("txtPassword")).sendKeys("admin123");
  driver.findElement(By.id("btnLogin")).click();
}

@Then("user should be redirected to the dashboard")
public void user_should_see_dashboard() {
  Assert.assertTrue(driver.findElement(By.id("welcome")).isDisplayed());
}
  

🌟 Best Practices

🌟 Sample Practice

Try writing a login feature and step definition for OrangeHRM Demo using Cucumber and Selenium.

Jenkins for CI/CD in Selenium Automation

🌟 What is Jenkins?

Jenkins is an open-source continuous integration and continuous delivery (CI/CD) tool that helps automate building, testing, and deploying software. It integrates perfectly with Selenium frameworks to automatically run your automated tests.

🌟 Why use Jenkins with Selenium?

🌟 How to Set Up Jenkins for Selenium

  1. Download Jenkins from jenkins.io
  2. Install Java JDK if not installed
  3. Run Jenkins and open its dashboard on http://localhost:8080
  4. Install suggested plugins
  5. Create a new job (Freestyle or Pipeline)
  6. Configure Git repository and build steps (Maven commands, etc.)
  7. Save and trigger builds

🌟 Sample Maven Command for Jenkins


mvn clean test
  

🌟 Best Practices

🌟 Sample Practice

Try configuring a Jenkins Freestyle job to run your Selenium Maven project after each code commit from GitHub.

GitHub Integration with Selenium

🌟 Why use GitHub with Selenium?

GitHub is a powerful version control and collaboration platform that helps you store, share, and manage your Selenium automation code with teams. It allows you to track changes, manage branches, and integrate with CI/CD tools like Jenkins.

🌟 Benefits of Using GitHub

🌟 How to Use GitHub with Selenium

  1. Create a free account on GitHub
  2. Initialize a new repository for your Selenium project
  3. Use git commands or GitHub Desktop to push your project code
  4. Share the repo link with your team
  5. Integrate your repository in Jenkins under "Source Code Management"

🌟 Sample Git Commands


git init
git add .
git commit -m "first selenium project"
git remote add origin https://github.com/yourusername/yourrepo.git
git push -u origin main
  

🌟 Best Practices

🌟 Sample Practice

Create a GitHub repo, push your Selenium Maven project, and link it to a Jenkins build pipeline.

Maven for Selenium Projects

🌟 What is Maven?

Maven is a popular build automation and dependency management tool for Java projects. It makes it easy to manage Selenium libraries, build processes, and test execution from a single configuration file.

🌟 Why use Maven with Selenium?

🌟 Installing Maven

  1. Download Maven from maven.apache.org
  2. Unzip and add Maven’s bin directory to your system PATH
  3. Verify by running mvn -v in your terminal

🌟 Sample pom.xml for Selenium


<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>seleniumproject</artifactId>
  <version>1.0</version>
  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.10.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.9.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
  

🌟 How to Run Tests with Maven

Use the following command to execute tests:


mvn clean test
  

🌟 Best Practices

🌟 Sample Practice

Create a Maven project for Selenium and configure it to run TestNG tests automatically.

Reporting in Selenium Automation

🌟 Why is Reporting Important?

Reports help you visualize test execution results, identify failures quickly, and share outcomes with your team or stakeholders. Selenium alone does not generate rich reports, so you typically integrate it with reporting tools.

🌟 Popular Reporting Tools

🌟 ExtentReports Setup Example


// Maven dependency
<dependency>
  <groupId>com.aventstack</groupId>
  <artifactId>extentreports</artifactId>
  <version>5.1.0</version>
</dependency>

// Java example
ExtentReports extent = new ExtentReports();
ExtentSparkReporter spark = new ExtentSparkReporter("target/Spark.html");
extent.attachReporter(spark);

ExtentTest test = extent.createTest("LoginTest");
test.pass("Login test passed");

extent.flush();
  

🌟 Best Practices

🌟 Sample Practice

Try setting up ExtentReports for your Selenium TestNG project to generate detailed reports with pass/fail screenshots.

Sample Selenium Project Walkthrough

🌟 Project Overview

Let’s put everything together in a simple Selenium Maven + TestNG project following POM design pattern. We’ll automate login for the OrangeHRM demo site as a practical example.

🌟 Project Structure


/src
  /main
    /java
      /pages
        LoginPage.java
      /tests
        LoginTest.java
      /utils
        ExcelUtils.java
/testdata
  LoginData.xlsx
/testng.xml
  

🌟 LoginPage.java (Page Object)


public class LoginPage {
  WebDriver driver;
  @FindBy(id="txtUsername") WebElement username;
  @FindBy(id="txtPassword") WebElement password;
  @FindBy(id="btnLogin") WebElement loginButton;

  public LoginPage(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this);
  }

  public void login(String user, String pass) {
    username.sendKeys(user);
    password.sendKeys(pass);
    loginButton.click();
  }
}
  

🌟 LoginTest.java


public class LoginTest {
  WebDriver driver;
  LoginPage login;

  @BeforeClass
  public void setup() {
    driver = new ChromeDriver();
    driver.get("https://opensource-demo.orangehrmlive.com/");
    login = new LoginPage(driver);
  }

  @Test
  public void testLogin() {
    login.login("Admin", "admin123");
    Assert.assertTrue(driver.findElement(By.id("welcome")).isDisplayed());
  }

  @AfterClass
  public void tearDown() {
    driver.quit();
  }
}
  

🌟 testng.xml


<suite name="OrangeHRMSuite">
  <test name="LoginTest">
    <classes>
      <class name="tests.LoginTest"/>
    </classes>
  </test>
</suite>
  

🌟 Best Practices

🌟 Next Steps

Enhance this project by adding DataProviders, external data sources, and Jenkins for CI/CD.