How to Automate Tests with Selenium Safari Driver?

How to Automate Tests with Selenium Safari Driver?

Everyone knows Google Chrome is the most popular web browser. But Safari is actually the second most popular, with 17% of people using it.

Safari is so popular because it comes on all Apple devices. And since many programmers like using Apple computers, it’s important to make sure websites work well on all versions of Safari.

Newer versions of Safari (10 and up) have a built-in tool called safaridriver to help test websites. Older versions of Safari used to need a special extension, but that’s not needed anymore. Now, Safari uses Safaridriver to follow the rules of the WebDriver protocol (a way to control web browsers with code).

In this guide, we’ll learn Safari browser automation and how to use Selenium with safaridriver to test websites in Safari.

How to Automate Tests with Selenium Safari Driver?

How to Automate Tests with Selenium Safari Driver?

To use Selenium for automated testing, you usually need to install a special driver for your web browser. For example, you need Chrome WebDriver for Chrome or Geckodriver for Firefox. But with Safari on a Mac, you don’t need to download a separate driver.

Starting with Safari 10, the browser comes with built-in support for Selenium. The driver is already on your Mac, so you don’t need to tell Selenium where to find it when you want to control Safari browser automation.

Download Safari Driver for Selenium WebDriver

We don’t need to download Safari’s driver for Selenium. Safari already has a built-in driver called safaridriver, which is available in most Selenium tools. Apple’s web developer team takes care of safaridriver. The safaridriver on your Mac is usually found at /usr/bin/safaridriver. Safari Technology Preview, a special version of Safari, also has its own safaridriver in a different location.

different location

Both the regular Safari and Safari Technology Preview drivers can work at the same time because each one only opens the specific Safari browser it’s connected to.

The old SafariDriver, made by the Selenium project, is not supported anymore and shouldn’t be used. Safari will even give you an error if you try to install it.

SafariDriver

To use the Selenium safaridriver for testing, you only need Safari 10 or later on your Mac. These versions already have built-in support for Selenium.

If you’re using Python or Java, Selenium versions 3.0.0 and newer support the native driver for Safari.

Configure Safari for Enabling WebDriver Support

Even though Safari 10 and newer versions have built-in support for Selenium WebDriver, it’s not turned on automatically. Here’s how to turn it on:

Step 1: Open your computer’s Terminal and type this command:

/usr/bin/safaridriver –enable

You might need to do this if you updated your Mac from an older version.

Step 2: Open Safari, go to Preferences, then Advanced, and make sure the box next to “Show Develop menu in menu bar” is checked.

Show Develop menu in menu bar

Step 3: In Safari, go to the Develop menu and click “Allow Remote Automation.”

Allow Remote Automation

Step 4: Type this command in Terminal to let Safari start the tool for running tests:

/usr/bin/safaridriver

Follow the instructions that pop up. Since we’re using Apple’s tool for tests on macOS Catalina, you only need to run the first command once.

Apple's tool

Features of Safari Driver on MacOS

To keep your information safe while using WebDriver for tests, there are extra protections. These protections make sure that your normal browsing and other tests are kept separate from the tests you run with Selenium.

Here are some important things about using Selenium’s Safari driver on Macs:

Isolated Automation Windows

Apple’s Safari team made a special tool called safaridriver to make sure your automated tests stay separate from your regular browsing. This means your tests won’t mess with your usual settings or what you normally do in Safari.

The Safari windows used for testing look different from regular ones. They have an orange search bar, kind of like how Firefox has a special look when you’re running tests with its tool.

Isolated Automation Windows

Every time you start a new test with safaridriver, it’s like starting a new private browsing session. This means your tests won’t be affected by what you did in previous tests.

Web Inspector

You can use Safari’s Web Inspector (or Inspect) tool while your tests are running and after they’re done. This tool lets you see details about items on the webpage, check how fast things are loading, and more. All of this works even when your tests are running.

To open the tool while your tests are going, go to the Develop menu and click “Show Web Inspector.”

Selenium WebDriver Commands for Debugging

Selenium’s Safari tool for Macs lets you set special options to help you find problems in your tests.

One option is automatic inspection. When you turn this on, the tool gets ready to use Safari’s Web Inspector and JavaScript debugger in the background. This is like having a special set of tools ready to check your website while your tests are running. You can even pause your tests to look at details in the debugger.

Another option is automatic profiling. This makes the tool start recording what’s happening on your website while your tests are running. You can see these details in the timeline part of Web Inspector.

Glass Pane

Think of a “glass pane” as an invisible shield that covers the Safari window when your tests are running. This shield stops you from accidentally clicking, typing, or closing the window while the test is working.

But, if something goes wrong with the test, or you need to check something, you can break the shield. This will stop the test, but the window will stay open so you can look at it.

When you try to break the shield, a message pops up:

  • Turn off All Automation: This turns off the special setting that lets you run tests in Safari. You’ll have to turn it back on if you want to run tests again.
  • Stop Session: This stops the current test, and you can use Safari normally again.
  • Continue Session: This keeps the test going from where it stopped. You won’t be able to use Safari normally until the test is finished.

One Session at a Time

You can only connect one testing tool to Safari at a time. This means you can only have one Safari window open for testing, and only one tool controlling it.

Apple says this is to stop different tests from fighting over things like which window you’re looking at, what you’re typing, and so on. It’s kind of like how you can only focus on one window at a time on your Mac.

FAQs: Safari Browser Automation

Does Selenium Support Safari Browser Automation?

Selenium has a special add-on for Safari browsers that lets you run Selenium scripts, which are like sets of instructions for testing websites. But, you can only use this add-on if your Safari version is 10 or newer.

How Can You Automate the Safari Browser by Selenium?

  • Step 1: Get the special add-on for Safari and install it.
  • Step 2: Go to Safari, then Preferences. In the window that opens, turn on “Enable WebDriver.”
  • Step 3: Close and reopen Safari.

Setting up Apple’s Safari tool for Safari browser automation tests on a Mac is easy because you don’t need to install anything extra. But, even though it’s simple, there are some problems. For example, clicks might not work correctly during tests. If you don’t have a Mac, you can install macOS on a virtual machine to try it out.

These problems don’t happen when you run your tests on a special online service like LambdaTest. Using a service like that also makes your tests faster and gives you the results you expect.

Read more: