Selenium WebDriver- Running test on Safari Browser

In this section, we will learn how to run our Selenium Test Scripts on Safari Browser.

Safari Browser implements the WebDriver protocol using SafariDriver. The SafariDriver is the link between your tests in Selenium and the Safari Browser. SafariDriver has been implemented as a plugin in safari browser and this provides a perfect match of client and server machine where SafariDriverServer acts as server and Selenium-Java/Language binding acts as client.

Note
Note: Previously SafariDriver was supporting safari browser on Windows machine but recently Apple has decided to remove its support for windows and then execution on safari has become the job of Mac machine. So for the same, we need mac machine where safari browser should be installed.

Let us consider a test case in which we will try to automate the following scenarios in Safari browser.

  • Launch Safari browser.
  • Open URL: www.google.com
  • Type the value "rookienerd tutorials"
  • Click on the Search button.
  • Close the browser

We will create our fifth test case in the same test suite (Demo_Test).

Step1. Right click on the "src" folder and create a new Class File from New > Class. Give your Class name as "Fifth" and click on "Finish" button.

Selenium WebDriver Running test on Safari Browser Selenium WebDriver Running test on Safari Browser

Step2. Open URL: https://www.seleniumhq.org/download/ in your Safari browser. It will direct you to the 'downloads' page of Selenium official website. Scroll down through the web page and locate SafariDriver.

Step3. Click on the "Latest Release" option to download the latest version of SafariDriver.

Selenium WebDriver Running test on Safari Browser

Step4. Double click on the downloaded file.

Selenium WebDriver Running test on Safari Browser

This will launch a pop-up box on your Safari browser extension window. Click on the "Trust" button to configure WebDriver in your Safari browser.

Selenium WebDriver Running test on Safari Browser Selenium WebDriver Running test on Safari Browser

Step5. Restart your browser.

Before writing the test script, let us first understand how we can initialize SafariDriver in Selenium. Safari browser is represented by a class called SafariDriver in the org.openqa.selenium.safari package. All we have to do is to create an instance of SafariDriver class.

Here is a sample code to do that:

snippet
WebDriver driver = new SafariDriver();

Step6. Now, it is time to code. We have embedded comments for each block of code to explain the steps clearly.

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

public class Fifth {

	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("rookienerd tutorials");
			
		  // Click on the search button
		driver.findElement(By.name("btnK")).click();
		
		  // Close the Browser
		driver.close();
		
	}

The Eclipse code window will look like this:

Selenium WebDriver Running test on Safari Browser

Step7. Right click on the Eclipse code and select Run As > Java Application.

Selenium WebDriver Running test on Safari Browser

Upon execution, the above test script will launch the Safari browser and automate all the test scenarios.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +