Live Cross Browser Testing and Debugging On Safari Browser

Configuring SafariDriver for Test Automation

safaridriver --enable

Characteristics of WebDriver for Safari

  • Isolated Automation Windows: Test execution is limited to special automation windows that are separate from normal browsing windows, user settings, and preferences. An automated browser always starts from a clean state like a private browsing session. It doesn’t have access to Safari’s browsing history, AutoFill data, or other sensitive information available in a normal browsing session. These isolated browser sessions ensure that current automated tests are completely unaffected by a previous test session’s persistent state.
  • Glass Panes: To prevent any kind of interaction with the web window during test execution, Safari installs a transparent “glass pane” over the automation window while the browser is being used for WebDriver testing.
  • Web Inspector and WebDriver: For debugging purposes, you can use a web inspector during or after the execution of a web driver test. To launch Web Inspector while a test is running, choose the Develop > Show Web Inspector menu item (shortcut: ⌥⌘I). During the running of WebDriver tests, the web inspector features such as console logging, the Elements tab, and the script debugger are fully functional.
  • One Session at a Time, to Mimic User Interaction: Only one WebDriver session can be attached at a time and only one Safari browser online instance can be active at any given time. This is to ensure that the simulated behavior of mouse, keyboard, and touch accurately reflects what a user can do in a macOS browser environment and also prevents automated tests from competing with each other for window and keyboard focus.

Running Up First Test On Safari Using SafariDriver

  • Java(JDK)
  • IDE i.e. Eclipse or Intellij
  • Selenium Client dependency
  • TestNG dependency
  1. Launch Safari browser
  2. Direct to https://www.pcloudy.com/
  3. Click on the Login button to open the login page
  4. Assert the title of the login page
  5. Close the Safari browser
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestScript {
private WebDriver driver;
public String expectedTitle = "Remote Mobile Web & Application
Testing on Real Android Devices - pCloudy";
@BeforeClass
public void setup()
{
driver = new SafariDriver();
}
@Test
public void verifyLoginPage()
{
driver.get("https://www.pcloudy.com/");
WebElement loginButton = driver.findElement(By.xpath("//a[text()='Login']"));
loginButton.click();
String actualTitle = driver.getTitle();Assert.assertEquals(actualTitle, expectedTitle,"Login Page Title didn't matched with the expected title");
}
@AfterClass
public void tearDown()
{
driver.quit();
}
}

Parallel Cross Browser Testing On pCloudy Browser Cloud

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Blog {
public String username = "ramit.dhamija@gmail.com";
public String accesskey = "5TfF4UcNRbN3JhucQ";
public static RemoteWebDriver driver = null;
public String gridURL = "https://prod-browsercloud-
in.pcloudy.com/seleniumcloud/wd/hub";
public String expectedTitle = "Remote Mobile Web & Application
Testing on Real Android Devices - pCloudy";
@BeforeClass
@Parameters(value = { "OS", "OSversion", "browser", "browserVersion" })
public void setUp(String OS, String OSversion, String browser,
String browserVersion) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("pCloudy_Username", username);
capabilities.setCapability("apiKey", accesskey);
capabilities.setCapability("clientName", username);
capabilities.setCapability("email", username);
capabilities.setCapability("os", OS);
capabilities.setCapability("osVersion", OSversion);
capabilities.setCapability("browserName", browser);
capabilities.setCapability("browserVersions", browserVersion);
capabilities.setCapability("pCloudy_EnableVideo", "true");
capabilities.setCapability("pCloudy_EnablePerformanceData", "true");
capabilities.setCapability("pCloudy_EnableDeviceLogs", "true");
try {
driver = new RemoteWebDriver(new URL(gridURL), capabilities);
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Test
public void testSimple() throws Exception {
try {
driver.get("https://www.pcloudy.com/");
WebElement loginButton = driver.findElement(By.xpath("//a[text()='Login']"));
loginButton.click();
String actualTitle = driver.getTitle();Assert.assertEquals(actualTitle, expectedTitle, "Login Page Title
didn't matched with the expected title");
System.out.println("Login Title matched"); File screenshot = ((TakesScreenshot)
driver).getScreenshotAs(OutputType.FILE);

try {
FileUtils.copyFile(screenshot, new
File("/home/ramit/Documents/loginPageScreenshot.png"));
} catch (IOException e) {
System.out.println(e.getMessage());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@AfterClass
public void tearDown() throws Exception {
if (driver != null) {
driver.quit();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite thread-count="2" name="cross browser test-safari"
parallel="tests">
<test name="Catalina-Safari-14">
<parameter name="OS" value="Mac" />
<parameter name="OSversion" value="Catalina" />
<parameter name="browser" value="safari" />
<parameter name="browserVersion" value="14" />
<classes>
<class name="main.java.src.Blog" />
</classes>
</test>
<test name="Catalina-Mojave-14">
<parameter name="OS" value="Mac" />
<parameter name="OSversion" value="Mojave" />
<parameter name="browser" value="safari" />
<parameter name="browserVersion" value="13" />
<classes>
<class name="main.java.src.Blog" />
</classes>
</test>
</suite>
  1. Mac-Catalina-Safari-14
  2. Mac-Mojave-Safari-13

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store