Thursday, 17 April 2025

To read data from an Excel file in Selenium with Java,

 How read Data from Excel for selenium java 

To read data from an Excel file in Selenium with Java, the most commonly used library is Apache POI. Here's a basic step-by-step guide to help you get started:


✅ 1. Add Apache POI Dependency

If you're using Maven, add these dependencies in your pom.xml:

<dependencies>
    <!-- Apache POI for Excel -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.3</version>
    </dependency>
</dependencies>

✅ 2. Create a Utility Class to Read Excel

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ExcelUtils {

    public static String getCellData(String filePath, String sheetName, int rowNum, int cellNum) {
        String value = "";
        try {
            FileInputStream fis = new FileInputStream(new File(filePath));
            Workbook workbook = new XSSFWorkbook(fis);
            Sheet sheet = workbook.getSheet(sheetName);
            Row row = sheet.getRow(rowNum);
            Cell cell = row.getCell(cellNum);

            value = cell.toString();  // You can handle different types here if needed

            workbook.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return value;
    }
}

✅ 3. Use ExcelUtils in Your Selenium Test

public class LoginTest {
    public static void main(String[] args) {
        String filePath = "C:\\TestData\\data.xlsx";
        String username = ExcelUtils.getCellData(filePath, "Sheet1", 0, 0);
        String password = ExcelUtils.getCellData(filePath, "Sheet1", 0, 1);

        System.out.println("Username: " + username);
        System.out.println("Password: " + password);

        // WebDriver code
        // driver.findElement(By.id("username")).sendKeys(username);
        // driver.findElement(By.id("password")).sendKeys(password);
    }
}

🔄 Tips:

  • .xlsx files are handled by XSSFWorkbook, .xls by HSSFWorkbook.

  • Add null checks if your Excel might have empty cells.

  • You can enhance this utility to read full rows, columns, or convert to 2D array.


Let me know if you want to read all data as a table or use DataProvider with TestNG too.

Tuesday, 15 April 2025

Selenium WebDriver Basic Methods You Should Know

 

🔍 Selenium WebDriver Basic Methods You Should Know

Now that your setup is ready, let’s explore some basic Selenium WebDriver methods in Java that you’ll use all the time. These methods help you interact with web elements, navigate between pages, and control the browser like a pro.

🚗 1. get(String url)

Opens a web page in the browser.

driver.get("https://www.google.com");

📄 2. getTitle()

Returns the title of the current web page.

String title = driver.getTitle();
System.out.println("Page title: " + title);

🔗 3. getCurrentUrl()

Gets the current page URL.

String currentUrl = driver.getCurrentUrl();

🧱 4. findElement(By locator)

Finds a single web element using a locator (like ID, name, XPath, etc.).

WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Selenium WebDriver");

🧮 5. findElements(By locator)

Finds multiple elements that match the locator and returns them as a list.

List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Total links: " + links.size());

🎯 6. click()

Clicks on a button, link, checkbox, etc.

driver.findElement(By.id("submitBtn")).click();

🖊 7. sendKeys(String text)

Enters text into an input field or textarea.

driver.findElement(By.name("username")).sendKeys("myUser123");

↩ 8. navigate().back(), navigate().forward(), navigate().refresh()

Used for browser navigation, like the back and forward buttons.

driver.navigate().back();    // Go back to the previous page
driver.navigate().forward(); // Move forward in browser history
driver.navigate().refresh(); // Refresh the current page

📷 9. getPageSource()

Returns the full HTML source of the current page.

String source = driver.getPageSource();

🛑 10. quit() vs. close()

  • quit() closes all browser windows and ends the WebDriver session.

  • close() closes only the current browser window.

driver.close(); // Just the active tab
driver.quit();  // Entire browser session

🧠 Quick Tip: Locators You’ll Use Often

By.id("id")
By.name("name")
By.className("className")
By.tagName("tagName")
By.linkText("full link text")
By.partialLinkText("partial text")
By.cssSelector("css selector")
By.xpath("xpath expression")

✅ Practice Makes Perfect

Using these basic methods, you can:

  • Open a browser

  • Search on Google

  • Click links

  • Fill login forms

  • Navigate back and forth

I

Excel Data read by Selenium java

 Code to read data from Excel  package mit; import java.io.File; import java.io.FileInputStream; import java.util.Arrays; import java.util.I...