In this tutorial, we would discuss one of the very basic topics of automation testing. To be able to execute the desired automated test cases, the user is required to launch the web browser first. Thus in this tutorial, we would learn “How to launch Firefox, Internet Explorer, Chrome and Safari using Selenium WebDriver?”.

HtmlUnitDriver

HtmlUnitDriver is a purely java based driver. The test is executed the same way as in any other browser but the user would not be able to see any browser. Thus it executes the test script in GUI less (Headless) mode.

Syntax:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class HtmlUnitDriverTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Create webDriver reference
        WebDriver driver;
        
        // Launch HtmlUnitDriver
        driver = new HtmlUnitDriver();
        
        // Open the web page
        driver.get("http://google.com");
        
        // Enter the text in the search box
        WebElement searchText = driver.findElement(By.name("q"));
        searchText.sendKeys("HtmlUnitDriver");
        
        // Close the driver
        driver.quit();

    }

}

Mozilla Firefox

Firefox is the most traditional browser, thus Selenium WebDriver do not require any additional utility to be set before launching the browser. The Selenium package automatically references towards the default location of the firefox.exe, thus the user need not to set any other property. FirefoxDriver comes as a part of Selenium package and is present in xpi format.

Syntax:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirefoxDriverTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Create webDriver reference
        WebDriver driver;
        
        // Launch FirefoxDriver
        driver = new FirefoxDriver();
        
        // Open the web page
        driver.get("http://google.com");
        
        // Enter the text in the search box
        WebElement searchText = driver.findElement(By.name("q"));
        searchText.sendKeys("FirefoxDriver");
        
        // Close the driver
        driver.quit();

    }

}

Internet Explorer

To execute test script on Internet Explorer, user needs to download an external utility InternetExplorerDriver, a standalone server to execute test scripts on Internet Explorer.

There are 3 simple steps to be able to execute test scripts on Internet Explorer:

  1. User can download the server from here and keep it in a desired location.
  2. Set the system’s property to point to the location where the server file is kept.
  3. Launch the Internet Explorer browser.

Syntax:


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class InternetExplorerDriverTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Create WebDriver reference
        WebDriver driver;
        
        // Set path for Internet Explorer Driver executable
        System.setProperty("webdriver.ie.driver", "C:/lib/IEDriverServer.exe");
        
        // Launch InternetExplorerDriver
        driver = new InternetExplorerDriver();
        
        // Open the web page
        driver.get("http://google.com");
        
        // Enter the text in the search box
        WebElement searchText = driver.findElement(By.name("q"));
        searchText.sendKeys("InternetExplorerDriver");
        
        // Close the driver
        driver.quit();

    }

}

Google Chrome

Like Internet Explorer, Google Chrome also requires user to download an external utility ChromeDriver, a standalone server to execute test scripts on Google Chrome. User can download the ChromeDriver file from here. The rest of the process would remain the same as that of Internet Explorer.

Syntax:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeDriverTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Create WebDriver reference
        WebDriver driver;
        
        // Set path for Chrome Driver executable
        System.setProperty("webdriver.chrome.driver", "C:/lib/chromedriver.exe");
        
        // Launch ChromeDriver
        driver = new ChromeDriver();
        
        // Open the web page
        driver.get("http://google.com");
        
        // Enter the text in the search box
        WebElement searchText = driver.findElement(By.name("q"));
        searchText.sendKeys("ChromeDriver");
        
        // Close the driver
        driver.quit();

    }

}

Apple Safari

SafariDriver comes bundled with the Selenium server’s package, thus user is not required to download or set any external entity. It is readily available to execute the test scripts on Safari. Thus, user can directly launch the Safari Browser similar to that of Mozilla Firefox.

Syntax:

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

public class SafariDriverTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Create webDriver reference
        WebDriver driver;
        
        // Launch SafariDriver
        driver = new SafariDriver();
        
        // Open the web page
        driver.get("http://google.com");
        
        // Enter the text in the search box
        WebElement searchText = driver.findElement(By.name("q"));
        searchText.sendKeys("SafariDriver");
        
        // Close the driver
        driver.quit();

    }

}

I hope the tutorial gives a clear picture to the user in order to launch different browsers.

Share this:

5 thoughts on “How to launch Firefox, Internet Explorer, Chrome and Safari using Selenium WebDriver?

  1. I think the admin of this site is really working hard in support of his web page, because here every information is quality based material.

Leave a Reply to crew locksmith las vegas Cancel reply

Your email address will not be published. Required fields are marked *