Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests
To set up WebDriver for Safari in Selenium using Java, you’ll need to follow these steps:
  1. Install SafariDriver: Safari comes with a built-in driver called SafariDriver, which is included with the Safari browser. Unlike other browsers, you don’t need to download any separate driver for Safari.

  2. Enable Remote Automation: You need to enable the ‘Allow Remote Automation’ option in Safari’s Develop menu. To enable the Develop menu, go to Safari > Preferences > Advanced and check “Show Develop menu in menu bar.” Then, go to Develop > Allow Remote Automation.

  3. Set Up Your Java Project: You need to have Java and Maven (or any other build tool) installed on your system. Create a Maven project and add the Selenium dependencies to your pom.xml file.

  4. Write Your Test Script: Use the Selenium WebDriver API to write your test scripts in Java.

Step 1: Install SafariDriver

Since SafariDriver is included with Safari, you don’t need to install anything extra. Just ensure that you have the latest version of Safari installed.

Step 2: Enable Remote Automation

  • Open Safari.
  • Go to ‘Safari > Preferences > Advanced‘.
  • Check the box for “Show Develop menu in menu bar”.
  • Go to ‘Develop‘ in the menu bar and check “Allow Remote Automation”.

Step 3: Set Up Your Java Project

Using Maven

  • Create a Maven Project: If you’re using an IDE like IntelliJ IDEA or Eclipse, you can create a new Maven project easily through the IDE’s interface.

  • Add Dependencies: Add the Selenium dependencies in your ‘pom.xml‘ file.

				
					<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.example</groupId>
    <artifactId>selenium-safari</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.11.0</version>
        </dependency>
    </dependencies>
</project>

				
			

Step 4: Write Your Test Script

Here’s a simple example of how to set up and use SafariDriver in your test script:

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class SafariTest {
    public static void main(String[] args) {
        // Set up the WebDriver for Safari
        WebDriver driver = new SafariDriver();
        
        // Navigate to a website
        driver.get("https://www.example.com");
        
        // Print the title of the page
        System.out.println("Title: " + driver.getTitle());
        
        // Perform other actions like finding elements, clicking, etc.
        
        // Close the browser
        driver.quit();
    }
}

				
			

Explanation:

  • Set up the WebDriver: The ‘SafariDriver‘ class is used to control the Safari browser.
  • Navigate to a website: Use the ‘get‘ method to navigate to a website.
  • Print the title of the page: Retrieve and print the title of the web page.
  • Perform actions: You can add more actions like finding elements, clicking buttons, etc.
  • Close the browser: Use the ‘quit‘ method to close the browser after the test is complete.

Additional Tips

  • WebDriverManager: Although WebDriverManager is typically used for other browsers, SafariDriver does not require it since it’s included with the browser.
  • Compatibility: Ensure your Safari version is compatible with the Selenium version you are using.
  • Permissions: On macOS Catalina and later, you might need to grant Safari additional permissions to allow remote automation.
Scroll to Top