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.
- 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
- Install the extension by double click your downloaded extension file and click the install button in the popup dialog.
- Open Safari web browser and click Safari —> Preferences —> Extensions menu item.
- Click the WebDriver item on the Extensions popup window left side.
- Then check the Enable WebDriver checkbox on the window right side to enable it.
- Restart the Safari web browser if needed.
2. Prerequisites To Run Selenium WebDriver On Safari Browser That Version >= 10.
- Safari web browser version 10+ supports selenium webdriver by default, so you do not need to install any extension.
- But you should enable the Safari browser Allow Remote Automation option by clicking the Develop —> Allow Remote Automation menu item in Safari.
- If you see a checkmark before the Allow Remote Automation menu item, it means the option has been enabled.
- 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.
- Use Safari web browser to run the selenium test script is similar to using IE, Firefox, Chrome.
- We need to use a org.openqa.selenium.safari.SafariDriver class instance to do that.
- Create an eclipse maven project, and create a java class com.dev2qa.webdriver.safari.SafariBasic in it.
- 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(); } }
- Below is the example project source file structure.
$ tree /F ./ ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ └── dev2qa │ │ │ └── webdriver │ │ │ └── safari │ │ │ └── SafariBasic.java
- 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
- How To Create Maven Project In Eclipse
- How To Add Selenium Server Standalone Jar File Into Your Java Project
- How To Use Firefox Webdriver To Launch Firefox Browser Automatically In Java.
- How To Run Test Cases Using Google Chrome In Selenium Automation Script.
- How To Run Microsoft Internet Explorer And Edge In Selenium Webdriver Automation Script.
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.
Hi Safari,
Scripts are executing faster than usual. Is there a way to control the speed of the script execution?
You have two method to control the script execution speed.
Use java thread sleep method or use webdriver WebDriverWait class.
You can see below article for examples.
https://www.dev2qa.com/webdriverwait-example/