Skip to main content

_java

Example

Run the following command on the terminal:

$ /usr/bin/safaridriver --enable

After that, make sure it's enabled. To do that:

  1. open Safari
  2. go to Develop
  3. click on Allow Remote Automations

Let's wire up a simple test, so we can see that everything works as expected.

Expected Behavior

Let's wire up a simple test, so we can see that everything works as expected.

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class Safari {
WebDriver driver;

@Before
public void setUp() throws Exception {
driver = new SafariDriver();
}

@After
public void tearDown() throws Exception {
driver.quit();
}

@Test
public void dropdownTest() {
driver.get("http://the-internet.herokuapp.com/");
String title = driver.getTitle();
assertThat(title, is(equalTo("The Internet")));
}

}

When we run the test, we should see a successful communication between Selenium and Safari.

Summary

Keep in mind that Safari can load without you realizing it (since it doesn't obtain focus when launching with Selenium). When that happens you'll need to switch to Safari in order to see what the test is doing.

And if you're running Safari on a remote node (or set of nodes), you'll need to install and enable the SafariDriver browser extension on each of them.

Thanks to Roman Isko for contributing the initial code for this tip.

Happy Testing!