Running Selenium tests in Safari Browser is exactly same as with other browsers. However you have to prepare your Safari browser to be capable of understanding Selenium WebDriver commands. Lets learn how to set up your Safari browser and how to write your first test.

How to run Selenium tests in Safari browser?

Step 1 – Set Up WebDriver Extension for Safari browser

2) Installthe Safari Browser Extension – Go to the folder where file has downloaded and double click on it. You will get a prompt, as shown in image below, there select “Install“

Download folder image

3) Enable WebDriver browser Extension – Now open the preferencespane on Safari browser. Go to Safari >> Preferences and open the preferences window.

In the preferences window select Extension. There you will find Selenium web driver listed in the extensions list, select the check box. As shown in the below image

Safari extension window

Note : Make sure that you have “Enable WebDriver“ check-box enabled.

4) Restart your Browser – All you have to do here is to restart your browser.

Write Selenium WebDriver code to Launch Safari

As I said earlier running selenium tests in Safari is exactly similar to working with Firefox or IE. Safari browser is represented by a class called SafariDriver in the org.openqa.selenium.safari package. All we have to do is create an instance of the SafariDriver class. Below is a sample code to do that

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

packageUsage;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.safari.SafariDriver;

publicclassSafariUsage{

publicstaticvoidmain(String[]args)

{

WebDriver driver=newSafariDriver();

driver.get("http://store.demoqa.com");

//Find some element on DemoQa.com

WebElement element=driver.findElement(By.id("login"));

element.click();

}

}

Here you can see that all we have to do is create an instance of SafariDriver and use it like a regular WebDriverthat we have been using it like for other browsers.

Issues:

Only http:// and https:// protocols are supported on Safari.

Safari is not able to handle alerts, so we have to suppress alerts in case of Safari. We will learn this in another chapter.

Author: Virender Singh

I am Virender Singh and I am a software Engineer.I have been in the Software profession for more than 12 years now. I worked on large spectrum of projects, from being a QA engineer to being a Development Engineer. At present I work as a Software Engineer for Microsoft India Development centre. I love to learn new technologies, specially in the field of Image Processing and Digital Signal processing. You can find me at following locations @LinkedIn@FaceBook@ToolsQA@Github