How To Run Test Cases Using Apple Safari In Selenium Automation Script

This article will tell you how to run the Apple Safari web browser in the selenium webdriver test script. If your Apple Safari version is smaller than version 10.0, you need to install a Safari web browser extension. If your Safari version is equal to or bigger than version 10, the Safari web browser support webDriver by default, then you do not need to install any browser extension.

1. Prerequisites To Run Selenium WebDriver On Safari Browser That Version < 10.

  1. First, you need to download and install the Safari web browser selenium webdriver extension, the extension download URL is http://selenium-release.storage.googleapis.com/2.48/SafariDriver.safariextz
  2. Install the extension by double click your downloaded extension file and click the install button in the popup dialog.
  3. Open Safari web browser and click Safari —> Preferences —> Extensions menu item.
  4. Click the WebDriver item on the Extensions popup window left side.
  5. Then check the Enable WebDriver checkbox on the window right side to enable it.
  6. Restart the Safari web browser if needed.

2. Prerequisites To Run Selenium WebDriver On Safari Browser That Version >= 10.

  1. Safari web browser version 10+ supports selenium webdriver by default, so you do not need to install any extension.
  2. But you should enable the Safari browser Allow Remote Automation option by clicking the Develop —> Allow Remote Automation menu item in Safari.
  3. If you see a checkmark before the Allow Remote Automation menu item, it means the option has been enabled.
  4. If you do not enable this option, when you run the selenium webdriver test script with Safari, you will get the below error message.
    org.openqa.selenium.SessionNotCreatedException: Could not create a session: You must enable the 'Allow Remote Automation' option in Safari's Develop menu to control Safari via WebDriver.

3. How To Launch Safari Web Browser In Selenium Webdriver Example.

  1. Use Safari web browser to run the selenium test script is similar to using IE, Firefox, Chrome.
  2. We need to use a org.openqa.selenium.safari.SafariDriver class instance to do that.
  3. Create an eclipse maven project, and create a java class com.dev2qa.webdriver.safari.SafariBasic in it.
  4. Then add the below java source code to the SafariBasic.java file.
    package com.dev2qa.webdriver.safari;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.safari.SafariDriver;
    
    public class SafariBasic {
        
        public static void StartSafari() {
            
            try {
                
                //Instantiate apple WebDriver
                WebDriver safariDriver = new SafariDriver();
                
                // Maximize the Safari browser window.
                safariDriver.manage().window().maximize();
                
                //Use apple browser WebDriver to browse web page http://www.google.com
                safariDriver.get("http://www.google.com");
                
                //Sleep 2 seconds to wait for the page load complete
                Thread.sleep(2000);
                
                
                // Get the google search box object by the search box name.
                WebElement searchBox = safariDriver.findElement(By.name("q"));
                // Input search keyword text in the google search box.
                searchBox.sendKeys("selenium");
                
                // Press the keyboard Enter key to close the search tips drop-down list and search.
                searchBox.sendKeys(Keys.ENTER);
                
                
                Thread.sleep(2000);
                
                // Close the Safari web browser.
                safariDriver.close();
                
            }catch(Exception ex){
                
                ex.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            
            SafariBasic.StartSafari();
    
        }
    
    }
    
  5. Below is the example project source file structure.
    $ tree /F ./
    ├── pom.xml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── com
    │   │   │       └── dev2qa
    │   │   │           └── webdriver
    │   │   │               └── safari
    │   │   │                   └── SafariBasic.java
    
  6. pom.xml.
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>2021JavaMavenProject</groupId>
      <artifactId>2021JavaMavenProject</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    
      <dependencies>
      
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
    
      </dependencies>
    </project>

References

  1. How To Create Maven Project In Eclipse
  2. How To Add Selenium Server Standalone Jar File Into Your Java Project
  3. How To Use Firefox Webdriver To Launch Firefox Browser Automatically In Java.
  4. How To Run Test Cases Using Google Chrome In Selenium Automation Script.
  5. How To Run Microsoft Internet Explorer And Edge In Selenium Webdriver Automation Script.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Hari
Hari

Hi safari,

Is there any possibility of running selenium script chrome driver in IPad or Iphone, which is connected to windows 10 laptop.
I want to check the website on both Android and IOS devices but i don’t have mac.

Rashmi
Rashmi

Hi Safari,
Scripts are executing faster than usual. Is there a way to control the speed of the script execution?

3
0
Would love your thoughts, please comment.x
()
x