Open In App

How to Run Safari Driver in Selenium Using Java?

Last Updated : 20 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Selenium is a well-known software used for software testing purposes. Selenium consists of three parts. One is Selenium IDE, one is Selenium Webdriver and the last one is Selenium Grid. Among these, Selenium Webdriver is the most important one. Using Webdriver online website testing can be done. There are three main Webdrivers present. For the Chrome browser, ChromeDriver is present. For the Firefox browser, Gecko Driver is applicable and for Microsoft Edge, there will be MSEdgeDriver present. Excluding these, many more drivers are present for other browsers. In this article, the process of running SafariWebdriver is implemented. It is useful for those who use Mac OS. This simple Java program can be run.

Pre-Requisites

  • For running SafariDriver, the Java jdk version must be installed in the machine previously.
  • The latest version of Safari should be installed.
  • It is preferable to install Eclipse IDE on the machine so that running this code will be easier.
  • The most important prerequisite is latest SafariDriver should be downloaded on the machine.

How to Run Safari Driver in Selenium Using Java?

  • Here, using SafariDriver, the home page of Google is going to open. For, that some methods need to be imported.
  • First, the Google home page link is to be stored in a string.
  • Then in the program, the property of the browser is to be set. setProperty() method is going to be used here.
  • In the setProperty() method, the first argument is to be the Webdriver that is to be going to use. Here, using SafariDriver specifically that argument have to be passed. And in the second argument, the location of the SafariDriver.exe is to be passed.

Note: In this case, SafariDriver.exe is stored in Eclipse, so maybe the location seems different. But also, a complete File Explorer path can also be passed.

  • Then a new object called driver should be implemented which is a type of WebDriver. Here, in this case, it will be SafariDriver.
  • Then using that driver object, the get() method will be used. This get() method of WebDrivers helps to open some URLs provided. Here the home page of Google is going to be opened. So, only the string where the URL has been stored will be passed. Executing this method will go to open a new Chrome window.
  • Then the sleep() method is going to be implemented. This delays the programs for some time. So that the output can be visible easily.
  • At last, the opened Safari window has to be closed. For that reason, the quit() method is going to be implemented.

Below is the complete implementation of the above approach:

Java
// Importing All Necessary Items
import java.io.*;
import java.lang.Thread;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class GoogleHomePage {
    public static void main(String[] args)
    {
        // Try-Catch Block For Implementing Sleep Method
        try {
            // String Where Home Page URL Is Stored
            String baseUrl = "https://www.google.com/";
          
            // Implementation of SetProperty Method
            System.setProperty(
                "webdriver.safari.driver",
                "test/resources/safaridriver.exe");
          
            // Creating New Object driver Of Webdriver
            WebDriver driver = new SafariDriver();
          
            // Calling the Home Page By Using Get() Method
            driver.get(baseUrl);
          
            // Delaying The Output
            Thread.sleep(2000);
          
            // Closing The Opened Window
            driver.quit();
        }
        catch (Exception e) {
            // Catching The Exception
            System.out.println(e);
        }
    }
}

Output:

If the above code is run, then a new Safari Window will be opened. This open window will be controlled by SafariDriver.exe.

Output

Output

Hence, the program runs successfully.

Conclusion

Setting up SafariDriver, making sure the Safari browser is installed and configured on your Mac, and using Selenium’s WebDriver interface to automate browser actions are all necessary when using Java to run Safari Driver in Selenium. You may efficiently create and run automated tests for Safari by initializing SafariDriver and importing the required Selenium libraries.

FAQs on How to Run Safari Driver in Selenium Using Java?

Can I use SafariDriver to conduct headless tests?

No, headless mode is not supported by SafariDriver. A visible Safari browser window will open during the test.

Does SafariDriver need me to establish a system property, just like other browsers (like ChromeDriver) do?

It’s not necessary to set a system property for SafariDriver.

In order to utilize SafariDriver, which version of Safari is required?

SafariDriver is compatible with Safari versions 10 and up.


Similar Reads

Difference between driver.dispose(), driver.close() and driver.quit()
In Selenium driver.close(), driver.quit(), driver.dispose() methods are used to close the browser windows in their own functionality. Each method serves a similar purpose but has different functionality. The driver.close() method is used to close the current browser window. The driver.quit() method is used to close all the browser windows and final
3 min read
How to Run Gecko Driver in Selenium Using Java?
Selenium is a well-known software used for software testing purposes. Selenium is consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver, online website testing can be done. There are three main web drivers present. For th
3 min read
How to Run Internet Explorer Driver in Selenium Using Java?
Selenium is a well-known software used for software testing purposes. Selenium consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver online website testing can be done. There are three main Webdrivers present. For the Chr
3 min read
How to Run Opera Driver in Selenium Using Java?
Selenium is a well-known software used for software testing purposes. Selenium consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver online website testing can be done. There are three main Webdrivers present. For the Chr
3 min read
How to Run Edge Driver in Selenium Using Eclipse?
Selenium is a well-known software used for software testing purposes. Selenium consists of 3 parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using webdriver online website testing can be done. There are 3 main webdrivers present. For the Chrome brow
3 min read
find_element(By.Name) driver method - Selenium Python
Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using Selenium WebDriver. After you have installed selenium and checked out - Navigating links using get method, you might want to play more with Selenium Python. After one has opened a page
3 min read
find_elements_by_name() driver method - Selenium Python
Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. After you have installed selenium and checked out - Navigating links using get method, you might want to play more with Selenium Python. After one has opened a page
2 min read
find_elements_by_xpath() driver method - Selenium Python
Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. After you have installed selenium and checked out - Navigating links using get method, you might want to play more with Selenium Python. After one has opened a page
2 min read
find_elements_by_link_text() driver method - Selenium Python
Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. After you have installed selenium and checked out - Navigating links using get method, you might want to play more with Selenium Python. After one has opened a page
2 min read
find_elements_by_partial_link_text() driver method - Selenium Python
Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. After you have installed selenium and checked out - Navigating links using get method, you might want to play more with Selenium Python. After one has opened a page
2 min read
Article Tags :
Practice Tags :