Test on Real Browsers Seamlessly using BrowserStack Automate

Run your website on Different Browsers and test cross browser compatibility on real devices

Get Started free
Home Guide How to Launch Browser in Selenium

How to Launch Browser in Selenium

By Pooja Deshpande, Community Contributor -

With the increase in the number of active internet users globally, websites have gained prominence for both customers and businesses. As user expectations are increasing, websites are getting more complex, layered, and function-rich than ever before. To engage and retain customers, developers are under mounting pressure to build better websites in shorter durations. These websites must have improved features, exceptional visuals, and technical excellence for customer delight.

Hence, website developers have to deliver better website features frequently to prevent the loss of traffic, revenue, and credibility. Test Automation is essential to meet the customers’ expectations in terms of quality and turnaround time. This is why browser automation is essential in the website development process, for testing the website thoroughly in a shorter span of time.

What is Browser Automation? 

Browser Automation, as the name suggests, automates the testing of web applications in order to deliver a quality-rich product. Browser automation helps to interact with the browser’s web elements and perform actions as a real user would do. Selenium is one of the most popular browser automation frameworks that help to leverage the maximum benefits of browser automation.

Why is Browser Automation required?

Automation is the need of the hour with increasing demand for more and more complex web applications that are function rich, support advanced technologies, enhanced UI’s, etc. With these growing demands and restricted timelines, manual testing becomes difficult in order to achieve the expected quality and ROI (return on investment) through testing. 

Test Automation can help to reduce manual efforts and time required for testing and also deliver a high-quality product depending upon the test automation strategy, tool, framework and resources used.

This way it helps to deliver a high quality product with less manual efforts in a short period of time. We usually automate regression test cases in order to avoid repetitive tasks. This helps to avoid any human errors if regression was to be done manually. Though 100% automation is not possible and manual efforts are always required, it reduces the possibility of human errors and also it is not a good idea to execute the regression suites manually during every release. 

Moreover, apart from the functional regression tests, it is also important to ensure a consistent and seamless user experience across different browsers and devices. Which why Cross Browser Testing is essential to check if the website is compatible with different versions of browsers. 

Cross Browser Testing is in trend as new browsers with different versions are being introduced in the market. It becomes necessary for developers to design applications that are compatible with all types of browser versions and devices. Browser automation can help to execute our tests across multiple device-browser combinations.

Pro Tip: Testing on a real device cloud like BrowserStack, helps you perform cross browser testing on 3000+ real browser-device combinations for a comprehensive testing experience.

Try BrowserStack for Free

Browser Automation using Selenium Webdriver

Selenium WebDriver allows browser automation, by connecting client to server. There are driver classes for different browsers in Selenium, which implement the Webdriver interface in order to interact with the browser. Let’s understand what happens internally when you instantiate ChromeDriver.

WebDriver driver = new ChromeDriver();

Here we are initialising the ChromeDriver class that implements the WebDriver interface.

This ChromeDriver class will interact with the chrome browser to perform the user actions.

How Browser Automation in Selenium works

As seen in the above diagram, webdriver acts as an interface between client and server.

How to Launch a Browser in Selenium?

When working with Selenium for web automation, one of the first tasks is to launch a browser that Selenium will control. Selenium supports various browsers, including Chrome, Firefox, Edge, and Safari. Each browser requires its corresponding WebDriver to interact with Selenium.

In this guide, we’ll focus on launching the Chrome browser using Selenium. Chrome is a popular choice for many automation tasks due to its speed and robust developer tools. To start automating with Chrome, you’ll need to set up Selenium with ChromeDriver, the WebDriver specifically for Chrome.

Pre-Requisites

There will be two prerequisites for this

  1. Setting up Selenium with the ChromeDriver
  2. Configuring your IDE (for e.g Eclipse IDE for the running the code)

Setting up Selenium with ChromeDriver in Java involves a series of steps. Here’s a detailed guide to help you through the process:

Step 1: Install Java Development Kit (JDK)

1. Download JDK: Go to the Oracle JDK download page or adopt an open-source version like OpenJDK.

2. Install JDK: Follow the installation instructions specific to your operating system.

3. Set Up Environment Variables:

  • Windows:
    1. Right-click on ‘This PC’ or ‘My Computer’ and select ‘Properties’.
    2. Click on ‘Advanced system settings’.
    3. Click on ‘Environment Variables’.
    4. Add a new system variable JAVA_HOME and set its value to the path of your JDK installation.
    5. Edit the ‘Path’ variable to include %JAVA_HOME%\bin.
  • Mac/Linux:
    1. Open a terminal.
    2. Add export JAVA_HOME=/path/to/your/jdk to your .bashrc or .zshrc file.
    3. Add export PATH=$JAVA_HOME/bin:$PATH to the same file.
    4. Run source ~/.bashrc or source ~/.zshrc to apply the changes.

Step 2: Install an Integrated Development Environment (IDE)

1. Choose an IDE: Popular choices include IntelliJ IDEA, Eclipse, or NetBeans.

2. Install the IDE: Download and follow the installation instructions for your chosen IDE.

Step 3: Download and Set Up Selenium WebDriver

1. Download Selenium: Go to the Selenium website and download the Selenium Java client library.

2. Add Selenium to Your Project:

  • Maven Project: Add the following dependency to your pom.xml file:
<dependency>

    <groupId>org.seleniumhq.selenium</groupId>

    <artifactId>selenium-java</artifactId>

    <version>4.20.0</version>

</dependency>
  • Non-Maven Project: Extract the downloaded Selenium Java client library and add the JAR files to your project’s build path.

Step 4: Download and Set Up ChromeDriver

1. Download ChromeDriver: Go to the ChromeDriver download page and download the version that matches your version of Chrome.

2. Extract the ChromeDriver: Extract the downloaded file to a directory of your choice.

3. Set Up ChromeDriver Path:

  • Windows:
    1. Place chromedriver.exe in a directory (e.g., C:\WebDriver).
    2. Add this directory to your system’s PATH variable.
  • Mac/Linux:
    • Place chromedriver in a directory (e.g., /usr/local/bin).
    • Ensure the directory is in your PATH.

The below example shows how to launch different browsers like chrome, firefox, IE with the help of WebDriverManager class and properties file.

package com.qa.browserstack.base;

import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class BasePage {

WebDriver driver;
Properties prop;

public WebDriver init_driver(Properties prop)
{
String browser = prop.getProperty("browser");

if(browser.equals("chrome"))
{

WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();

}else if(browser.equals("firefox"))
{

WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();

}
else
{
System.out.println("Please provide a proper browser value..");
}

driver.manage().window().fullscreen();
driver.manage().deleteAllCookies();
driver.get(prop.getProperty("url"));

return driver;
}

public Properties init_properties()
{
prop = new Properties();
try {
FileInputStream ip = new FileInputStream("Properties file path");
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e)
{
e.printStackTrace();
}

return prop;


}
}

In this example, we are passing the browser value with the help of the properties file. Depending upon the given value, the specific driver will be instantiated. This is also a way, using which you can perform cross browser testing across different browsers.

On executing the above code for chrome browser, you can see the browser is launched successfully. Similarly you can launch other browsers as well.

Launching a Browser in Selenium

This code snippet provides a structured way to set up and launch a web browser for automated testing with Selenium.

Here’s a step-by-step guide on how it works:

1. Create the BasePage Class

  • Purpose: Define a class named BasePage to handle browser setup and configuration.

2. Declare Instance Variables

  • WebDriver driver: This variable will hold the WebDriver instance that controls the browser.
  • Properties prop: This variable will store configuration details like browser type and URL.

3. Method to Initialize WebDriver

  • Method Name: init_driver(Properties prop)
  • Purpose: Set up the WebDriver based on the browser type specified in the properties file.

a. Retrieve Browser Type

  • Action: Get the browser type from the prop object.
  • Purpose: Determine which browser to launch (Chrome or Firefox).

b. Set Up Browser

  • If Chrome:
    • Action: Use WebDriverManager to download and set up the ChromeDriver.
    • Create: Instantiate ChromeDriver to launch Chrome.
  • If Firefox:
    • Action: Use WebDriverManager to download and set up the FirefoxDriver.
    • Create: Instantiate FirefoxDriver to launch Firefox.
  • If Not Recognized:
    • Action: Print an error message if the browser type is invalid.

c. Configure the Browser

  • Fullscreen Mode: Set the browser window to fullscreen.
  • Clear Cookies: Remove all cookies to start with a clean slate.
  • Open URL: Navigate to the URL specified in the properties file.

d. Return WebDriver Instance

  • Purpose: Return the initialized WebDriver so it can be used for further actions.

4. Method to Load Properties

  • Method Name: init_properties()
  • Purpose: Load configuration settings from a properties file.

a. Create Properties Object

  • Action: Initialize a new Properties object to store settings.

b. Load the Properties File

  • Action: Open and read the properties file to load the settings.
  • Error Handling: Catch and print errors if the file is not found or cannot be read.

c. Return Properties Object

  • Purpose: Return the prop object containing the loaded configuration settings.

How to Open an Incognito Window in Selenium?

While the above method explaining how to launch a browser, this method is used when you need to open an Incognito Window in Selenium.

ChromeOptions class in Selenium helps to open the browser in incognito mode.

You have to pass incognito as an argument to the addArguments method of ChromeOptions class.

Let’s take the same example as above and pass incognito as an argument while launching chrome browser

package com.qa.browserstack.base;

import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class BasePage {

WebDriver driver;
Properties prop;

public WebDriver init_driver(Properties prop)
{
String browser = prop.getProperty("browser");

if(browser.equals("chrome"))
{
ChromeOptions option = new ChromeOptions();
option.addArguments("incognito");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(option);

}else if(browser.equals("firefox"))
{

WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();

}
else
{
System.out.println("Please provide a proper browser value..");
}

driver.manage().window().fullscreen();
driver.manage().deleteAllCookies();
driver.get(prop.getProperty("url"));

return driver;
}

public Properties init_properties()
{
prop = new Properties();
try {
FileInputStream ip = new FileInputStream("Properties File Path");
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e)
{
e.printStackTrace();
}

return prop;


}
}

Chrome browser opens in incognito mode as seen below.

Opening an Incognito Window in Selenium

Step-by-Step Summary of Browser Initialization Code

Step 1. Create the BasePage Class

  • Purpose: Define a class named BasePage to handle the setup and configuration of the browser for automated testing.

Step 2. Declare Instance Variables

  • WebDriver driver: Stores the WebDriver instance used to control the browser.
  • Properties prop: Holds configuration details such as browser type and URL.

Step 3. Method to Initialize WebDriver

  • Method Name: init_driver(Properties prop)
  • Purpose: Set up and configure the WebDriver based on the properties provided.

a. Retrieve Browser Type

  • Action: Get the browser type from the prop object.
  • Purpose: Determine which browser to launch (Chrome or Firefox).

b. Set Up Browser

  • If Chrome:
    • Create Options: Instantiate ChromeOptions to configure browser-specific settings.
    • Add Arguments: Add the incognito argument to open Chrome in incognito mode.
    • Set Up Driver: Use WebDriverManager to download and set up the ChromeDriver.
    • Create Driver: Instantiate ChromeDriver with the configured options.
  • If Firefox:
    • Set Up Driver: Use WebDriverManager to download and set up the FirefoxDriver.
    • Create Driver: Instantiate FirefoxDriver without additional options.
  • If Browser Type Is Not Recognized:
    • Action: Print an error message to indicate an invalid browser type.

c. Configure the Browser

  • Fullscreen Mode: Set the browser window to fullscreen.
  • Clear Cookies: Remove all cookies to start with a clean session.
  • Open URL: Navigate to the URL specified in the properties file.

d. Return WebDriver Instance

  • Purpose: Return the initialized and configured WebDriver for use in tests.

Step 4. Method to Load Properties

  • Method Name: init_properties()
  • Purpose: Load configuration settings from a properties file.

a. Create Properties Object

  • Action: Instantiate a new Properties object to hold the configuration settings.

b. Load the Properties File

  • Action: Open and read the properties file using FileInputStream.
  • Error Handling: Catch and print errors if the file is not found or if there is an issue reading it.

c. Return Properties Object

  • Purpose: Return the prop object containing the loaded configuration settings.

How to Open a Page using Selenium?

To open a page or website in Selenium, you need to pass the url of the page in driver.get() method. In this example, we are passing the url through the properties file. Then using the driver.get() method to access the specified url.

Properties file for Opening given URL on Browser using Selenium

package com.qa.browserstack.base;

import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class BasePage {

WebDriver driver;
Properties prop;

public WebDriver init_driver(Properties prop)
{
String browser = prop.getProperty("browser");

if(browser.equals("chrome"))
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();

}else if(browser.equals("firefox"))
{

WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();

}
else
{
System.out.println("Please provide a proper browser value..");
}

driver.get(prop.getProperty("url"));

return driver;
}

public Properties init_properties()
{
prop = new Properties();
try {
FileInputStream ip = new FileInputStream("Properties File Path");
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e)
{
e.printStackTrace();
}

return prop;


}
}

The above code opens browserstack.com URL in Chrome browser as seen in the image below.

Opening a Page URL using Selenium

1. Define the BasePage Class

  • Purpose: Create a class named BasePage to manage browser setup and configuration for automated testing.

2. Declare Instance Variables

  • WebDriver driver: This variable holds the WebDriver instance, which is used to control the browser.
  • Properties prop: This variable contains configuration settings, such as the browser type and URL to open.

3. Method to Initialize WebDriver

  • Method Name: init_driver(Properties prop)
  • Purpose: Set up and configure the WebDriver based on the settings provided in the properties file.

a. Retrieve Browser Type

  • Action: Get the type of browser to use from the prop object.
  • Purpose: Determine which browser (Chrome or Firefox) to launch.

b. Set Up Browser

  • If Chrome:
    • Action: Use WebDriverManager to automatically download and set up the ChromeDriver.
    • Create: Instantiate a ChromeDriver to open Chrome.
  • If Firefox:
    • Action: Use WebDriverManager to automatically download and set up the FirefoxDriver.
    • Create: Instantiate a FirefoxDriver to open Firefox.
  • If Browser Type Is Not Recognized:
    • Action: Print an error message indicating that an invalid browser type was provided.

c. Navigate to URL

  • Action: Open the URL specified in the properties file using
    driver.get(prop.getProperty(“url”)).
  • Purpose: Direct the browser to the specified web address.

d. Return WebDriver Instance

  • Purpose: Return the configured WebDriver instance for further use in your tests.

4. Method to Load Properties

  • Method Name: init_properties()
  • Purpose: Load configuration settings from a properties file.

a. Create Properties Object

  • Action: Instantiate a new Properties object to store the configuration data.

b. Load the Properties File

  • Action: Open and read the properties file using FileInputStream.
  • Error Handling: Catch and print errors if the file is not found or if there are issues reading it.

c. Return Properties Object

  • Purpose: Return the prop object containing the loaded configuration settings.

How to perform Google Search Automation in Selenium

Google Search is widely used by the end-users, and it makes a significant use case for Browser Automation. For any website, it is important that users are able to search, find the website in Google SERP (Search Engine Results Page), and click to access the website. This makes it a valid test case for Selenium Browser Automation.

BrowserStack Automate Banner 8

Let’s understand Google Search Automation in Selenium using the example of searching “browserstack” string in Google.

Whenever you enter a search string in the google search box, it displays multiple options in suggestions. These suggestions are displayed with the help of Ajax (Asynchronous javascript and XML). It is a technology used for creating interactive web applications. 

You can store these suggestions and select the suitable option with the help of Selenium.

import java.time.Duration;
import java.util.List;

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

import io.github.bonigarcia.wdm.WebDriverManager;

public class GoogleSearchTest {

public static void main(String[] args) {

String Searchstring = "browserstack";
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
String url = "https://www.google.com/";
driver.get(url);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));

driver.findElement(By.name("q")).sendKeys(Searchstring);

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));

List<WebElement> searchitems = driver.findElements(By.xpath("//ul[@role='listbox']/li/descendant::div[@class='eIPGRd']"));

for(int i=0;i<searchitems.size();i++)
{
String listitem = searchitems.get(i).getText();
System.out.println(listitem);
if(listitem.contains(Searchstring))
{
searchitems.get(i).click();
break;
}
}
}

}

The above code searches browserstack string in the Google Search Box, as seen in the image below.

Google Search Automation in Selenium

In the above code, the list of all suggestions displayed in google search is captured using Selenium and then select the required search string.

Google Search Automation using Selenium

Console Output:

Automating Google Search using Selenium

Set Up: Import necessary libraries and define the GoogleSearchTest class.

Initialize WebDriver:

  • Set up ChromeDriver using WebDriverManager.
  • Create an instance of ChromeDriver to interact with the browser.

Navigate to Google:

  • Open Google’s homepage.
  • Maximize the browser window and set an implicit wait.

Perform and Handle Search:

  • Enter the search query into Google’s search box.
  • Wait for search suggestions to appear.
  • Retrieve all search suggestions and print each suggestion.
  • Click on the suggestion that matches the search query and stop further processing.

How to Launch Browser on Real Devices using BrowserStack?

BrowserStack Automate allows us to execute tests on various device-browser combinations which is helpful when the test suites are large and you need to perform parallel testing to run test cases simultaneously on different device-browser combinations.

Launch Browser on BrowserStack Cloud Selenium Grid

Let’s see an example to launch browser on BrowserStack:

Signup for Free, and get your BrowserStack username and access key before executing the below code.You can get your username and access key once you login to BrowserStack, Navigate to Dashboard and Click on Access Key as shown below.

Access Key

package BS;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
public class LaunchBrowserOnBS {

public static final String AUTOMATE_USERNAME = "your_username";
public static final String AUTOMATE_ACCESS_KEY = "your_password";
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("os_version", "10");
caps.setCapability("resolution", "1920x1080");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "latest");
caps.setCapability("os", "Windows");
caps.setCapability("name", "Test to launch chrome browser on BS"); // test name

final WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
try {
// go to bstackdemo.com
driver.get("https://www.browserstack.com/");

} catch (Exception e) {
// print any exception
System.out.println(e);
}
// quit the driver
driver.quit();
}
}

Talk to an Expert

Here’s how the BrowserStack Automate Dashboard looks like with all the details like Text Logs, Network Logs, and Video Logs.

BrowserStack Automate Dashboard

BrowserStack Automate Dashboard also contains the details of Input Capabilites and Device Capabilities as seen below.

Input Capabilities and Desired Capabilities on BrowserStack Automate Dashboard

You can play the test that has been performed or even download the test case. BrowserStack provides a wide range of capabilities that detail every step in detail along with the time of execution. Using Selenium IDE, you can record and playback the Browser Automation test for better debugging failed tests.

Try Browser Automation on Cloud Selenium Grid

Tags
Automated UI Testing Selenium Selenium Webdriver Website Testing

Featured Articles

What is Browser Automation?

7 Best Practices for Efficient Selenium Web Browser Automation

Launch Browsers in Selenium on Real Devices

Run Launch Browser in Selenium Tests on Real Device Cloud