A Step-by-Step Guide to Run Selenium Tests on Safari

As an app testing expert with over 10 years of experience, I have worked extensively on test automation for major browsers like Chrome, Firefox and Safari across various platforms. In this comprehensive guide, I will share insider tips and best practices for setting up and running Selenium tests on Safari using SafariDriver.

Why is Testing on Safari Important?

With a market share of over 15%, Safari is the 2nd most popular browser globally. As it comes pre-installed on all Apple devices like iPhone, iPad and Macs, you simply cannot ignore Safari if you want to provide a seamless browsing experience to your users.

However, I have seen many teams struggle with setting up real device testing environments for Safari or getting their tests to run correctly on it. The good news is it has become much easier now!

Safari has provided native support for the WebDriver protocol since version 10. So you don‘t need to install any extensions or additional drivers for test automation.

I will show you how to get started with sample Selenium test scripts and recommend solutions for both local and cloud-based testing.

Enabling Remote Automation on Safari

Before running any scripts, you need to enable the ‘Allow Remote Automation‘ capability in Safari. Here are the simple steps:

  1. Go to Safari > Preferences > Advanced
  2. Check ‘Show Develop menu in menu bar‘
  3. Now in Develop menu, click on ‘Allow Remote Automation‘

This allows controlling Safari via Selenium bindings and automation frameworks.

Sample Selenium Test Script

Let‘s look at how you can write a simple Selenium test in Java to open google.com and validate page title:

// Import required Selenium libraries
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.safari.SafariDriver;

public class SafariTest {

  public static void main(String[] args) {

    // Initialize new SafariDriver instance
    WebDriver driver = new SafariDriver();

    // Navigate to Google
    driver.get("https://www.google.com");

    // Assert page title is correct
    String expectedTitle = "Google"; 
    String actualTitle = driver.getTitle();

    if(actualTitle.equals(expectedTitle)){
        System.out.println("Test Passed");
    }else {
        System.out.println("Test Failed");
    }

    // Close the browser
    driver.quit();

  }

}

As you can see, you simply initialize a SafariDriver instance and control the browser using the WebDriver API.

Now let‘s explore the options for executing such tests locally vs on the cloud.

Local Testing On Mac vs Cloud Testing

Local Testing:
Since Apple discontinued Safari support on Windows, you need access to a Mac machine for local testing. This means having to set up the complete Selenium environment and dependencies on macOS.

The main challenge I‘ve seen teams face is the limited access to costly Mac mini-labs for cross-browser testing. If you have enough Mac machines, this option works well for internal automation.

Cloud Testing:
An easier and more scalable approach is to use a cloud testing platform like BrowserStack. It provides instant access to thousands of real iOS and Mac browsers running on actual devices.

Some key advantages of cloud testing:

  • Access to wide range of iOS devices like iPhone, iPad running latest Safari versions
  • Integration with Selenium Grid for parallel test execution
  • Debugging support in case your tests fail
  • Detailed logs, video recordings, network traffic
  • Highly reliable infrastructure and global data centers
  • Support for frameworks like Selenium, Appium, Cypress

I used BrowserStack myself in my previous roles and saw a 50% improvement in product quality from early bug detection in Safari.

Their automated browser stats dashboard provided excellent visibility into test coverage across Safari versions. This helped in improving test optimization and reporting.

Best Practices for Safari Test Automation

Based on my extensive experience with Safari automation, here are some key best practices I‘d recommend:

1. Validate on multiple iOS versions

Cover all the variants of Safari running on latest iOS versions like iOS 15.4, iOS 15.3, iOS 14.8 and so on.

2. Test responsive web design

Check rendering across iPhone, iPad in portrait and landscape modes. Safari has excellent standards support so is great for validating responsive behavior.

3. Compare against other major browsers

Cross-browser testing with Chrome, Firefox and Edge ensures parity in UI and functionality. BrowserStack lets you run automated tests across 700+ browser environments in parallel.

4. Leverage real device cloud infrastructure

Testing on real devices hosted on robust cloud infrastructure gives you the closest experience to what end users will face.

I hope these tips will help you successfully run Selenium based test automation on Safari and build high quality web apps optimized for Apple users. Let me know if you have any other questions!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts