In this tutorial, we will go through a step by step process to setup Selenium Java in Eclipse in Mac.

Step 1: Install Java Development Kit (JDK)

  • Download JDK: Go to Oracle JDK or OpenJDK and download the latest version.
  • Install: Follow the installation steps for Mac.
  • Verify Installation: Open Terminal and run the following command.
java -version

You should see the installed version of Java.

Step 2: Install Eclipse

Eclipse: Download from Eclipse if you prefer Eclipse.

Step 3: Download Selenium JAR files

  • Go to the Selenium Downloads page.
  • Under Selenium Client & WebDriver Language Bindings, download the Java library.
  • Extract the downloaded file to get the selenium-server-<version>.jar and client-combined-<version>.jarfiles.

Step 4: Set Up WebDriver for Your Browser

Selenium requires a WebDriver to communicate with browsers. Download the WebDriver for the browser you want to automate:

  • ChromeChromeDriver
  • FirefoxGeckoDriver
  • Safari: Safari has a built-in WebDriver. You need to enable it in Safari’s Developer settings.

For Chrome or Firefox:

  • Download the driver and unzip it.
  • Move the driver executable to /usr/local/bin so it’s accessible from anywhere.

Step 5: Set Up Selenium in Your IDE

  • Create a New Project: Open IntelliJ or Eclipse and create a new Java project.
  • Add Selenium JARs:
    • In IntelliJ: Go to File > Project Structure > Libraries > + (Add) and select the Selenium JAR files you downloaded.
    • In Eclipse: Right-click the project > Build Path > Configure Build Path > Add External JARs and add the Selenium JARs.

Step 6: Write a Test Script

Here’s a basic example script that opens a Chrome browser and navigates to a website:

</>
Copy
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumTest {
    public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
        
        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();
        
        // Open a website
        driver.get("https://www.google.com");
        
        // Print the page title
        System.out.println("Page title is: " + driver.getTitle());
        
        // Close the browser
        driver.quit();
    }
}

Step 7: Run Your Selenium Test

  • In IntelliJ, right-click the Java file and select Run.
  • In Eclipse, right-click the Java file, choose Run As > Java Application.

Step 8: Enable Safari’s WebDriver (if using Safari)

  • Open Safari and go to Safari > Preferences > Advanced.
  • Enable Show Develop menu in menu bar.
  • Go to Develop > Allow Remote Automation to enable Safari’s WebDriver.

This setup should get Selenium running on your Mac with Java. Let me know if you have questions!