It is very similar to run Safari web browser in selenium webdriver tests as other browsers. But you need to do some preparation to make it to be able to listen and understand what webdriver commands means.
SafariDriver change normal client server relationship. It use WebSocket to talk with WebDriver client script. You need to install a web browser extension which just implement the SafariDriver before using it.
SafariDriver can only be run with Safari 5.1+ on windows, As a result, Windows version of it will not work on newer Safari version because Apple has decided to remove windows version of it.
If you have not installed Safari 5.1 on your windows, you can click here or google “apple web browser for windows” to get download link.
If you run selenium webdriver tests script on mac os, you can use Safari 10+ directly. Because Safari 10+ support WebDriver by default.
Now i will show you how to prepare your apple web browser and how to write test script code in java step by step.
How to start using apple web browser in Selenium tests script?
Running selenium automation tests script for multiple web browsers will probably need extra setups, SafariDriver is the same. So you need do following steps.
1. Click here to download the latest extension.
2. Install that extension by double click your downloaded extension file and click “install” in the popup dialog.
3. Make sure SafariDriver is enabled by following step.
Go to Safari —> Preferences —> Extensions.
Check “Enable WebDriver” checkbox.
4. Restart this web browser.
How to write java code to launch SafariDriver in selenium webdriver?
Use Safari web browser to run selenium test script is very similar as using IE, Firefox and Chrome. We just need to instantiate SafariDriver class instance to do that. The package for this class is org.openqa.selenium.safari. It support https:// and http:// protocols only and this browser driver can not handle alerts, we should control alerts any time for it in java code.
Below is the example code to use SafariDriver.
//Instantiate apple WebDriver WebDriver sDriver = new SafariDriver(); //Use apple browser WebDriver to browse webpage http://dev2qa.com sDriver.get("http://dev2qa.com"); //Find element on dev2qa.com WebElement ele = sDriver.findElement(By.xpath("//header[@id=\"masthead\"]/div/h1/a")); //Click the web element above. ele.click();