Selenium WebDriver in Java with Different Browsers

We will create our first Selenium WebDriver with java for all major browser and create test script to launch bowser and open URL into that browser, here we will write test scripts for browsers “Google Chrome”, “Mozilla Firefox” & “Internet Explorer” using their respective drivers.


Using Java project, we would be creating test script for below following scenario.
1. Launch google chrome/Firefox/Internet Explorer browser.

2. Enter following URL https://rahulshettyacademy.com/AutomationPractice/

3. Verify title of page that is opened.

4. Print the title on console.

5. Close the driver before ending the program.

Here is the sample code for chrome browser and above scenario.


Explaining the code

Importing Packages

To get started, we need to import following two packages:
1. org.openqa.selenium.*- contains the WebDriver class needed to instantiate a new browser loaded with a specific driver.

2. org.openqa.selenium.chrome.ChromeDriver - contains the ChromeDriver class needed to instantiate a Chrome-specific driver onto the browser instantiated by the WebDriver class.

If your test needs more complicated actions such as accessing another class, taking browser screenshots, or manipulating external files, you will need to import more packages.

Instantiating objects and variables

Normally, this is how a driver object is instantiated.

WebDriver driver = new ChromeDriver();

A ChromeDriver class with no parameters means that the default chrome profile will be launched by our Java program.

For reusing the variables and keeping script clean we saved the values of BaseURL and expected title in variables.

Launching a Browser Session

WebDriver's get() method is used to launch a new browser session and directs it to the URL that you specify as its parameter.

driver.get(baseURL);

Get the Actual Page Title

The WebDriver class has the getTitle() method that is always used to obtain the page title of the currently loaded page.

actualTitle = driver.getTitle();

Compare the Expected and Actual Values

This portion of the code simply uses a basic Java if-else structure to compare the actual title with the expected one.

if (actualTitle.equalsIgnoreCase(expectedTitle))
{
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}

Terminating a Browser Session

The "close()" method is used to close the browser window.

driver.close();

Running the Test

There are two ways to execute code in Eclipse IDE.

1. On Eclipse's menu bar, click Run > Run.
2. Press Ctrl+F11 to run the entire code.

If you did everything correctly, Eclipse would output "Test Passed!"

Here is sample code for firefox browser for about scenario.

Here is sample code for Internet Explorer browser for about scenario.

In this way we can create web driver session for all three browsers and launch each browser and perform testing as per our requirement.


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 

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

  1. Excellent post. There’s a quite a lot to learn from you and your blogs, awesome sharing thank you so much.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}