Home Guide How to Run Selenium Tests on Safari using SafariDriver

How to Run Selenium Tests on Safari using SafariDriver

By Jash Unadkat, Technical Content Writer at BrowserStack -

Safari is the second most popular browser in the world, occupying a 16% market share, positioned right after Chrome. A key reason behind its significant market share is the fact that Safari is the default browser for all Apple devices. Naturally, web-developers across the globe need to ensure that their websites are thoroughly tested and optimized for all versions of Safari.

This article will discuss how to run Selenium tests on the Safari browser using the SafariDriver.

Note: Earlier versions of Safari require the installation of the Safari WebDriver extension. However, this extension is no longer supported for Safari testing. For Safari versions 10 and above, SafariDriver comes bundled by default with the browser that’s preinstalled in the macOS.

One can find the Safari Driver (v10 and above) at the following path – /usr/bin/safaridriver. This means that Safari now provides native support for the WebDriver API. It also means that users can simply point their tests from their editor to Safari and run them without installing any external library or driver.

Also, one needs to have a Mac machine to test on the latest versions of Safari as Apple terminated support for Safari on Windows in 2012. Another option is to use a cloud-based tool like BrowserStack, where one can access Safari on real Apple devices through their own machine, irrespective of the operating system.

Running Selenium Tests on Safari using SafariDriver

Prior to Safari automation, enable the Remote Automation feature from the developer menu. To do so, enable the Safari Developer menu first with the steps below:

  1. Go to Safari -> Preferences-> Advanced
  2. Tick mark the Checkbox with the label – Show Develop menu in menu bar

Once done, go to the Develop menu and click on the Allow Remote Automation option to enable it.

SafariDriver Example

Once this is done, users can straightaway get started with the programming part without downloading Safari WebDriver.

This article assumes that the reader knows how to set up a basic Selenium environment along with the required dependencies or libraries. Let’s assume the use of Selenium with Java for the upcoming examples.

Let’s consider a simple test scenario with three steps:

  1. Launch Safari browser
  2. Visit https://www.google.com
  3. Enter the search query “BrowserStack”
  4. Click on the search button
  5. Close the browser

A Java program for the above test scenario is as follows:

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

public class SafariDemo { 

public static void main(String[] args) { 

// Instantiate a SafariDriver class. 
WebDriver driver = new SafariDriver(); 
// Launch Website 
driver.navigate().to("http://www.google.com/"); 

// Click on the search text box and send value 
driver.findElement(By.id("lst-ib")).sendKeys("BrowserStack"); 

// Click on the search button 
driver.findElement(By.name("btnK")).click(); 

// Close the Browser 
driver.close(); 

}

Executing the above code successfully will

  1. Launch the Safari browser
  2. Navigate to Google website
  3. Enter the search query as “BrowserStack” and
  4. Click on the search button

Also read: How to run Selenium Test on Chrome using ChromeDriver


Thus, Safari automation is achieved without explicitly downloading and referring to a specific driver executable as in the case of Chrome and Firefox.

Exploring critical bugs faster is only possible when tests are executed on real devices. Testing teams or individual testers can use BrowserStack’s cloud Selenium Grid for running automated Safari tests on iOS and macOS environments in parallel. As all the browsers are installed on actual Mac and iOS devices, teams are able to test their websites in real user conditions, thus identifying every possible bug that can show up in the site’s real-world usage.

Try Automated Safari Testing Now

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Run a Free Test Now