Selenium Webdriver example code in java

Selenium Webdriver example code in java

Selenium Webdriver example code in java

Selenium Webdriver example code is simple to write. Let us see how to write a simple program in selenium web driver using java.

The below scenario shows how to invoke a browser, opening an URL, getting title of the current page, comparing it with expected result and printing in console and finally closes and quits the browser.

Best Selenium Online Training

Our Trainer Profile 

Below is simple Selenium webdriver example code in java

package Demo_Pack;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

public class MyFirstSeleniumProgram {

public static void main(String[] args) {

System.setProperty(“webdriver.chrome.driver”,”C:\\Drivers\\chromedriver.exe”);

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

String URL = (“http://www.google.com”);

driver.get(URL);

String actualResult = driver.getTitle();

System.out.println(“Your page title Is : “+actualResult);

String expectedResult = “Google”;

Assert.assertEquals(Expected,Actual);

System.out.println(“Title is verified”);

driver.close();

}

}

Detail explanation of the above program

1.   Importing necessary packages:

We imported below packages on above program

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.junit.Assert;

  • openga.selenium.WebDriver :-  WebDriver is an interface and it is available in package org.openga.selenium so we have to import this package with “.WebDriver”  which consists of web driver interface which helps to invoke a browser with a particular driver.
  • openqa.selenium.chrome.ChromeDriver:- Which consists of ChromeDriver class which helps to invoke chrome driver into the browser implements by the webdriver interface.
  • junit.Assert:- This consists of assert equals method which helps us to compare the Actual result with Expected Result.

2.   Setting path for respective browser:

The path of the driver executable must be set by the webdriver ie. called driver system property.

System.setProperty(“webdriver.chrome.driver”,”C:\\Drivers\\chromedriver.exe”);

WebDriver driver = new ChromeDriver();

We have to set your system property by mentioning exact path of your chrome driver.

Chrome Driver to invoke chrome browser:

Chrome doesn’t have built in server to run our script in the browser. So chrome driver helps you to communicate your scripts in selenium and chrome browser.

Initiating Browser:

The below line of code represents initialization of chrome browser using selenium WebDriver

WebDriver driver = new ChromeDriver();

In Detail

  • WebDriver is a interface .
  • driver is a reference variable of web driver.
  • ChromeDriver is a class.

Here we are creating a reference variable of interface means creating an object to launch the browser.

3.   Maximize the browser window:

driver.manage().window().maximize();

The above line of code represents how to maximize a window using Selenium WebDriver as by default window is not in maximized when it is opened by WebDriver

4.   Launch URL:

String URL = (“http://www.google.com”);

driver.get(URL);

driver.get() is a method which is used to open the browser with given URL. It opens the Chrome browser and type given URL in the browser.

5.   Get the Title:

The below lines of code represents how to get the Title of the current window in browser and prints in Console

String Actual = driver.getTitle();

System.out.println(“Your page title Is : “+Actual);

Here getTitle() is a method to capture the title of the currently opened window in respective browser and System.out.println() method helps to print the title in the console screen.

6.   Comparing the Title:

String Expectedresult = “Google”;

Assert.assertEquals(Expectedresult,actualResult);

System.out.println(“Title is verified”);

The above lines of code represents how to compare the Actual result with Expected Result using Assert class. Here assertEquals(Expected Value , Actual  Value) method compares our Expected Title with Actual Title and an AssertionError  message is thrown if Expected and Actual are not equal. Then we are printing “Title is Verified” message in Console output screen.

assertEquals() method returns TRUE OR FALSE based on comparison. If both expected and actual results are matching it will return true and it will pass your test case. If not it will return false then it will fail your test case.

7.   Closes and quitting Browser:

Below line of code represents how to close the browser before ends up with program.

driver.close();

driver.close():

It represents to close the current window of the browser we are performing the current script.

8.    Running the above test case:

Click on Run > Run from Eclipse’s menu bar

Output:

We can see the below message in Console screen after running our script successfully in Eclipse.

Leave a Comment

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

18 Years Experienced Trainer