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.
Let us consider a test case in which we will try to automate the following scenarios in Safari 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.
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.
Step4. Double click on the downloaded file.
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.
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:
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.
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:
Step7. Right click on the Eclipse code and select Run As > Java Application.
Upon execution, the above test script will launch the Safari browser and automate all the test scenarios.