Getting Started with Selenium: A Step-by-Step Guide

Getting Started with Selenium: A Step-by-Step Guide

In this blog, we’ll explore how to get started with Selenium, step by step.

Step 1: Visit the Selenium Website

To begin, head over to the official Selenium website by searching “Selenium download” in your browser. The official site is selenium.dev, where you’ll find support for multiple programming languages such as:

  • Java

  • C#

  • Ruby

  • Python

  • JavaScript

Choose the version that fits your programming language preference. I will be working with Java for this tutorial.

Step 2: Using Maven for Dependency Management

If you’re working on a Maven project, managing dependencies is a breeze. Instead of manually downloading files, follow these steps:

  1. Visit mvnrepository.com.

  2. Search for the required Selenium dependency.

  3. Copy the dependency code.

  4. Paste it into your project’s pom.xml file.

  5. Save the file.

Maven will automatically download all the necessary JAR files for you. This approach is quick, efficient, and reduces the risk of errors.

Step 3: Adding TestNG for Testing

TestNG is a powerful unit testing framework for Java. It’s great for validating whether your test cases pass or fail using assertions. To include TestNG:

  1. Add its dependency to your pom.xml file (just like Selenium).

  2. Save the file.

You’re now ready to write reliable and robust test cases with TestNG.

Step 4: Creating Your First Java Class

Once your dependencies are set, it’s time to write some Selenium code. Here’s how:

  1. Right-click your project’s source folder.

  2. Select New > Class.

  3. Name the class, e.g., SeleniumIntroduction.

  4. Check the option for public static void main.

All the code will go inside the main method, as that’s where Java executes the program.

Step 5: Invoking a Browser

Selenium supports automating different browsers like Chrome, Firefox, Edge, and Safari. To automate Chrome, use the ChromeDriver class. Example:

WebDriver driver = new ChromeDriver();

Why Use WebDriver Instead of ChromeDriver?

  • WebDriver is an interface that standardizes method names across all browser drivers.

  • Referring to the object as WebDriver makes the code flexible. For example, switching to Firefox requires only this change:

WebDriver driver = new FirefoxDriver();

This ensures compatibility across browsers without significant code changes.

Step 6: Understanding WebDriver and Browser Drivers

The WebDriver interface doesn’t define how methods like get() or close() work internally. Browser-specific classes like ChromeDriver and FirefoxDriver implement WebDriver to provide their logic.

For example:

  • The close() method behaves consistently across browsers, even if its internal implementation varies.

Best Practices:

  1. Always refer to the browser driver as a WebDriver object for compatibility.

  2. Avoid browser-specific methods unless absolutely necessary.

Selenium and Browser Invocation

Understanding Browser Restrictions

Browsers have strict security restrictions that prevent direct access from external tools like Selenium. Selenium overcomes this using browser-specific drivers like ChromeDriver.

Setting Up ChromeDriver

  1. Manual Configuration:

    • Download ChromeDriver from the official site.

    • Match the version with your Chrome browser.

    • Use the System.setProperty() method to specify the ChromeDriver path:

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
      
  2. Using Selenium Manager (Recommended):

    • Selenium Manager automates ChromeDriver setup, downloads the correct version, and eliminates manual configuration.

Cross-Browser Testing with Selenium

Running Tests on Firefox:

  1. Download GeckoDriver from the official Mozilla repository.

  2. Set the path:

     System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
    

    Initialize FirefoxDriver:

     WebDriver driver = new FirefoxDriver();
    

Running Tests on Microsoft Edge:

  1. Download EdgeDriver from the official Microsoft site.

  2. Set the path:

     System.setProperty("webdriver.edge.driver", "path/to/msedgedriver");
    
  3. Initialize EdgeDriver:

     WebDriver driver = new EdgeDriver();
    

Running Tests on Chrome:

  1. Download ChromeDriver.

  2. Set the path:

     System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
    
  3. Initialize ChromeDriver:

     WebDriver driver = new ChromeDriver();
    

Key Takeaways

  1. Use the appropriate WebDriver for each browser:

    • GeckoDriver for Firefox

    • EdgeDriver for Edge

    • ChromeDriver for Chrome

  2. Avoid using deprecated browsers like Internet Explorer.

Troubleshooting Browser Driver Issues

If your tests fail to invoke a browser, consider these troubleshooting steps:

  1. Update Versions: Ensure your browser and Selenium library are up to date.

  2. Driver Compatibility: Verify that the browser driver matches your browser version.

  3. Permission Issues: On Linux/Mac, ensure the driver file has executable permissions:

     chmod +x path/to/driver
    
  4. Use Selenium Manager: It resolves most compatibility and setup issues automatically.

What’s Next?

Now that we’ve covered browser invocation, the next step is to learn how to:

  • Navigate URLs.

  • Locate web elements.

  • Perform actions like clicking buttons or retrieving text.

Stay tuned for the upcoming blogs where we’ll dive deeper into Selenium’s powerful capabilities.

Did you find this article valuable?

Support Samiksha's Blog by becoming a sponsor. Any amount is appreciated!