How to run Selenium scripts in Safari Browser in Mac using SafariDriver Java, Python, Ruby, C#

Yosuva ArulanthuC#, Java, Python, Ruby, Selenium, Test Automation, Testing, XPATHLeave a Comment

As a prerequisite, we need to enable the Remote Automation feature from the developer menu. To do so, enable the Safari Developer menu first with the steps below:

  1. Go to Safari -> Preferences-> Advanced
  2. Tick mark the Checkbox with the label – Show Develop menu in the menu bar

Once done, go to the Develop menu and click on the Allow Remote Automation option to enable it.

SafariDriver Example

Once this is done, users can straightaway get started with the programming part without downloading Safari WebDriver.

Below is the sample code to run selenium script in safari browser

Java

package selenium;

import java.util.concurrent.TimeUnit;

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

public class TestSafari {

public static void main(String[] args) throws InterruptedException {
WebDriver driver= new SafariDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) ;
driver.manage().window().maximize();

driver.get("https://google.com");
driver.findElement(By.xpath("//div[text()='Accept all']")).click();
driver.findElement(By.name("q")).sendKeys("yosuva");
driver.findElement(By.name("q")).submit();
driver.quit();
 
}

}

Python

import os
import unittest
from selenium import webdriver
class SafariTest(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Safari()

    def tearDown(self):
        self.driver.quit()
    def test_example_1(self):
        driver = self.driver
        driver.get('https://google.com')
        driver.find_element_by_xpath('//div[text()='Accept all']').click()
        driver.find_element_by_name('q').send_keys('yosuva')
        driver.find_element_by_name('q').submit()

if __name__ == "__main__":
    unittest.main()

Ruby

require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers

def setup
  @driver = Selenium::WebDriver.for :safari
end

def teardown
  @driver.quit
end

def run
  setup
  yield
  teardown
end

run do

  @driver.get 'https://google.com'
  @driver.find_element(xpath: '//div[text()='Accept all']').click
  @driver.find_element(name: 'a').send_keys 'yosuva'
  @driver.find_element(name: 'a').submit

end

C#

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
public class SafariTest
{
    IWebDriver Driver;

    [SetUp]
    public void SetUp()
    {
        Driver = new SafariDriver();
    }

    [TearDown]
    public void TearDown()
    {
        Driver.Quit();
    }

    [Test]
    public void UploadFileFromDisk()
    {
        Driver.Navigate().GoToUrl("https://google.com");
        Driver.FindElement(By.Xpath("//div[text()='Accept all")).Click();
        Driver.FindElement(By.Name("q")).SendKeys("yosuva");
        Driver.FindElement(By.Name("q")).Submit();
    }
}

This is what happens when we run the above code

  1. Launch Safari browser
  2. Visit https://www.google.com
  3. Click Accept all button
  4. Enter the search query “yosuva”
  5. Submits the details
  6. Close the browser

Leave a Reply

Your email address will not be published.