First Test Script in Selenium

  

Here is a simple Selenium WebDriver test script in Java that:

  • Launches Chrome

  • Opens Google's homepage

  • Prints the page title

  • Closes the browser


First Selenium Test Script in Java

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

public class FirstSeleniumTest {
    public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "C:\\Path\\To\\chromedriver.exe");

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

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

        // Print the title of the page
        System.out.println("Page title is: " + driver.getTitle());

        // Close the browser
        driver.quit();
    }
}


Instructions to Run This Code:

  • Make sure you have:

    • Java installed

    • Selenium .jar files added to your project build path

    • chromedriver.exe downloaded and path set correctly

  • Replace C:\\Path\\To\\chromedriver.exe with the actual path to your ChromeDriver executable.
  • Run the file using your IDE or javac/java commands.

If it is maven project, then sample code is:

 1. Create a Maven Project

You can create it in Eclipse, IntelliJ.

2. Update pom.xml with Selenium Dependency

Edit pom.xml and add Selenium dependency:

<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>com.selenium.test</groupId>
    <artifactId>FirstSeleniumTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Selenium Java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.21.0</version>
        </dependency>
    </dependencies>
</project>

Run:
mvn clean install


3. Write First Selenium Test (App.java)

package com.selenium.test;

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

public class App {
    public static void main(String[] args) {
        // Set path to ChromeDriver
        System.setProperty("webdriver.chrome.driver", "C:\\Path\\To\\chromedriver.exe");

        // Launch Chrome browser
        WebDriver driver = new ChromeDriver();

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

        // Print page title
        System.out.println("Page title is: " + driver.getTitle());

        // Close browser
        driver.quit();
    }
}

File: src/main/java/com/selenium/test/App.java



4. Download ChromeDriver

Update the path in code:

System.setProperty("webdriver.chrome.driver", "C:\\Path\\To\\chromedriver.exe");


5. Run the Project


mvn compile mvn exec:java -Dexec.mainClass="com.selenium.test.App"

No comments:

Post a Comment