In this section, we will guide you through writing your first Selenium script. This script will automate a simple task in a web browser, allowing you to see Selenium in action. By the end of this lesson, you will have a basic understanding of how to set up and execute a Selenium test.

Key Concepts

  • Selenium WebDriver: A tool for automating web application testing, allowing you to control a browser programmatically.
  • WebDriver Interface: The main interface to control the browser.
  • Basic Script Structure: Understanding the components of a Selenium script.

Prerequisites

Before you start, ensure you have:

  • Installed Selenium WebDriver (covered in the previous section).
  • A basic understanding of Java programming (or the language you are using with Selenium).

Writing Your First Selenium Script

Step 1: Set Up Your Project

  1. Create a New Project: Open your IDE (e.g., IntelliJ IDEA, Eclipse) and create a new Java project.
  2. Add Selenium Libraries: Ensure that the Selenium WebDriver libraries are added to your project. This can be done by adding the Selenium JAR files to your project's build path.

Step 2: Write the Script

Below is a simple Selenium script that opens a browser, navigates to a website, and prints the page title.

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

public class FirstSeleniumScript {
    public static void main(String[] args) {
        // Set the path to the chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize a new ChromeDriver instance
        WebDriver driver = new ChromeDriver();

        // Navigate to a website
        driver.get("https://www.example.com");

        // Get the title of the page
        String pageTitle = driver.getTitle();

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

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

Explanation of the Code

  • System.setProperty: This line sets the system property for the ChromeDriver, which is necessary to control the Chrome browser.
  • WebDriver driver = new ChromeDriver(): This initializes a new instance of the ChromeDriver, which opens a new browser window.
  • driver.get("https://www.example.com"): This command navigates the browser to the specified URL.
  • driver.getTitle(): This retrieves the title of the current page.
  • driver.quit(): This closes the browser and ends the WebDriver session.

Step 3: Run the Script

  1. Compile and Run: Compile your Java program and run it. You should see a browser window open, navigate to the specified URL, and then close after printing the page title to the console.

Practical Exercise

Exercise: Modify the script to navigate to a different website and print both the page title and the current URL.

Solution:

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");
        WebDriver driver = new ChromeDriver();

        // Navigate to a different website
        driver.get("https://www.google.com");

        // Get the title of the page
        String pageTitle = driver.getTitle();
        // Get the current URL
        String currentUrl = driver.getCurrentUrl();

        // Print the title and URL of the page
        System.out.println("Page title is: " + pageTitle);
        System.out.println("Current URL is: " + currentUrl);

        driver.quit();
    }
}

Common Mistakes and Tips

  • Incorrect Path to WebDriver: Ensure the path to the WebDriver executable is correct.
  • Browser Compatibility: Make sure the WebDriver version matches the browser version.
  • Driver Not Closed: Always use driver.quit() to close the browser and free up resources.

Conclusion

Congratulations! You've written and executed your first Selenium script. This foundational knowledge will be crucial as you progress to more complex automation tasks. In the next section, we will delve deeper into understanding the WebDriver interface and its capabilities.

Test Automation with Selenium

Module 1: Introduction to Test Automation

Module 2: Getting Started with Selenium

Module 3: Locating Web Elements

Module 4: Interacting with Web Elements

Module 5: Synchronization in Selenium

Module 6: Test Frameworks and Selenium

Module 7: Advanced Selenium Concepts

Module 8: Selenium Grid and Parallel Testing

Module 9: Continuous Integration and Selenium

Module 10: Best Practices and Troubleshooting

© Copyright 2024. All rights reserved