Selenium First Script

Writing Your First Selenium Script

Now that we have completed the setup, let's write our first Selenium script and execute it in Chrome browser. We will follow the steps below:
  1. Create a New Project: Open Eclipse and create a new Java project.
  2. Add Selenium Libraries: then Add the Selenium Java libraries to your project.
  3. Write the Script: Create a new Java class and write the following code:
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class FirstSeleniumScript {
        public static void main(String[] args) {
            // Set the path of the ChromeDriver
            System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
    
            // Initialize WebDriver
            WebDriver driver = new ChromeDriver();
    
            // Open a website
            driver.get("https://nicetesters.com");
    
            // Print the title of the page
            System.out.println("Title: " + driver.getTitle());
    
            // Close the browser
            driver.quit();
        }
    }
                                    
  4. Run the Script: To Execute the script, right click on the class and then find Run as option in eclipse and then Java Application.

Explanation

Let's go through the code line by line:

  • Import Statements:
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    These lines import the necessary classes from the Selenium library. Like WebDriver class is the main interface for testing web applications. And, ChromeDriver is a class that implements the WebDriver interface for the Chrome browser. So we need to import these classes.
  • Class Declaration:
    public class FirstSeleniumScript {
    This line declares a public class named FirstSeleniumScript.
  • Main Method:
    public static void main(String[] args) {
    This line declares the Java main method, which is the entry point of any Java application. This method will be executed by Java without calling it.
  • Set the Path of the ChromeDriver:
    System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
    This line sets the system property for the ChromeDriver file. We need to replace "path/to/chromedriver" with the actual path to the ChromeDriver file on your system.
  • Initialize WebDriver:
    WebDriver driver = new ChromeDriver();
    This line creates an java object of the ChromeDriver, which launches a new Chrome browser window.
  • Open a Website:
    driver.get("https://nicetesters.com");
    This line navigates the browser to the specified URL (https://nicetesters.com) and open the webpage.
  • Print the Title of the Page:
    System.out.println("Title: " + driver.getTitle());
    This line fetch the title of the current page and prints it to the console.
  • Close the Browser:
    driver.quit();
    This line closes the browser and ends the WebDriver session.
Next we will learn Selenium important classes.