Skip to content

Software Testing Class

  • Testing
    • Learn Database Testing
    • Performance Testing
    • Testing Differences
  • Learn Tools
    • JIRA
    • JMeter
    • Selenium
    • SoapUI
    • QTP
  • Online Courses
  • Testing Quiz
    • Software Testing
    • QTP
  • Testing
    • Learn Database Testing
    • Performance Testing
    • Testing Differences
  • Learn Tools
    • JIRA
    • JMeter
    • Selenium
    • SoapUI
    • QTP
  • Online Courses
  • Testing Quiz
    • Software Testing
    • QTP

My First Project using Selenium WebDriver

  • STC Admin
  • July 31, 2016

In the previous chapter, we learned to install WebDriver and integrated it with Eclipse MARS.1 IDE. In this chapter, we are going to develop a test script in JAVA using Selenium WebDriver as our first project.

Read more: Selenium WebDriver Tutorial series

We have already created a JAVA class ‘SeleniumTestClass’ in the last chapter, now we are going to write the test script to verify the title of the LinkedIn website. Below are the steps for that.

  • We will first lookup the LinkedIn website homepage.
  • Fetch the title of the homepage using Selenium WebDriver.
  • Using Java programming language, we will compare the fetched title from the homepage against the expected title. This is our actual test case.
  • If the expected website title matches with the actual website title, we will declare the test as passed.

Given below is the WebDriver JAVA program to write the above test case.

package seleniumpackage;

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

public class SeleniumTestClass {

 public static void main(String[] args) {

 /*declaration and instantiation of objects/variables*/
 WebDriver driver = new FirefoxDriver();
 String baseWebUrl = "https://www.linkedin.com/";
 String expectedWebsiteTitle = "World’s Largest Professional Network | LinkedIn";
 String actualWebsiteTitle = "";

 /*Launch Firefox browser and browse the Base URL*/
 driver.get(baseWebUrl);

 /* get the actual value of the title*/
 actualWebsiteTitle = driver.getTitle();

 /*
 * Compare the Website actual title against the expected title
 * If both titles matches then result is "Passed" else "Failed"
 */
 if (actualWebsiteTitle.contentEquals(expectedWebsiteTitle)){
 System.out.println("Test Passed!");
 } else {
 System.out.println("Test Failed!");
 }

 /*closing Firefox Browser*/
 driver.close();

 /*Exiting the System*/
 System.exit(0);
 }
}

JAVA Code Explanation for ‘SeleniumTestClass’:

JAVA Package

  • Java package ‘seleniumpackage’ is the actual folder where JAVA class ‘SeleniumTestClass’ is located.

Importing Predefined JAVA Class and Interface

  • Next, we need to import the interface definition of WebDriver from ‘openqa.selenium.WebDriver’. This is required to instantiate a new browser that loads with a particular driver.
  • Next, we need to import class definition for the Firefox browser from ‘openqa.selenium.firefox.FirefoxDriver’. This is required to instantiate a Firefox-specific driver onto the web browser that was instantiated by the WebDriver class.

Creating a class Object and variables

WebDriver driver = new FirefoxDriver();
  • Using the above statement, we are instantiating an object for the Firefox browser driver. Here that object variable has given name as ‘driver’.
  • FirefoxDriver constructor arguments are blank, therefore the object is instantiated with the default constructor. It is equivalent to launching the Firefox browser in a safe mode.

Launch the Firefox browser and browse the Base URL

String baseWebUrl = "https://www.linkedin.com/";
driver.get(baseWebUrl);
  • WebDriver has a get (baseURL) method which accept the base URL of a website as a parameter and launches a new browser session and browse the website.

Get the actual value of the title

String actualWebsiteTitle = driver.getTitle();

  • Webdriver has a ‘getTitle’ method that fetches the title of a website that was opened in a new browser session using the base URL. The return value is copied to a string variable ‘actualWebsiteTitle’.

Actual Test Case

Our test says the expectedWebsiteTitle is equal to “World’s Largest Professional Network | LinkedIn”. This value is compared against the actualWebsiteTitle. If values are equal, the condition will be true, and then block will be executed I.e. ‘Test Passed!’ If the above condition is false then else block will be executed which will print ‘Test Failed!’ The code block is shown below.

if (actualWebsiteTitle.contentEquals(expectedWebsiteTitle)){
System.out.println ("Test Passed!");
}
else {
System.out.println ("Test Failed!");
}

Closing Browser Session

  • Once test is completed, the browser session can be terminated using the below statement which includes the close method of the WebDriver.

driver.close();

[/code]

Entire Program Termination

  • Once test is completed, the program can be terminated or closed using the below statement.
System.exit(0);
  • This is to be noted that above statement should always be used after closing the browser session. If this command is used before closing the browser session then the program will be terminated but the browser session will be left open in the Operating System.

Run the JAVA Program in Eclipse

In the Project Explorer, right-click on the Java class ‘SeleniumTestClass’ and navigate as Run As -> 2 Java Application. Click on this option to run the Java Program as shown below.

Run Selenium webdriver

Output Console

Since the expected output matches the actual output, therefore the system has displayed the test result as “Test Passed!” as shown below.

Selenium webdriver test output

This completes our first project using Selenium WebDriver and JAVA Programming language.

WebDriver Interface Methods and their uses:

Sr NoWebDriver MethodDescription and Use
1.Get (String);It accepts parameter as String type which is nothing but the website base URL. It opens up a new browser session and launces the website with the given base URL. It is equivalent to Selenium IDE’s “open” command.
2.getTitle();This method does not accept any input parameter. It fetches the title of the currently loaded page of the Website. It automatically trims out the leading and trailing white spaces. It returns a String data object which is nothing but the title of the Website Page. If the web page has no title then it will return null.
3.getPageSource();This method of WebDriver does not accept any input parameters. It is used to return the source code of the page which is nothing but the String.
4.getCurrentUrl();This method of WebDriver does not accept any input parameters. It is used to fetch the current URL as String that the browser is working on.
5.getText();This method of WebDriver does not accepts any input parameters. It is used to fetch the inner text of the web element that we specify for testing.
6.navigate().to();This method accepts the input parameter as string which is the website URL. It opens up a new web browser that fetches the web page that we have specified as a web URL. It is equivalent to the get () method that we just now discussed.
7.navigate().refresh();This method does not accept any input parameter and it is used to refresh the current web page.
8.navigate().back();This method does not accept any input parameter and it navigates the user to a page back on the web browser’s history.
9.navigate().forward();This method does not accept any input parameter and it navigates the user to a page ahead or forward on the web browser’s history.
10.close();This method does not accept any input parameter and it is used to close the current web browser session in the Operating System that WebDriver is controlling.
11.quit ();This method does not accept any input parameter and it is used to close all windows that were opened by the WebDriver while testing.

Conclusion:

In this chapter, we have developed our First Project With Selenium WebDriver and discussed the various methods that are present in the WebDriver. We will discuss in detail about other operations using Selenium WebDriver in the upcoming chapters.


⇓ Subscribe Us ⇓


If you are not regular reader of this website then highly recommends you to Sign up for our free email newsletter!! Sign up just providing your email address below:


 

Check email in your inbox for confirmation to get latest updates Software Testing for free.


  Happy Testing!!!
 
Related posts:
  1. Introduction To WebDriver And Its Comparison With Selenium RC – [Learn Selenium Basics]
  2. Keyboard Mouse Events, Uploading Files – Selenium Webdriver
  3. Learn Selenium Series – How To Enhance A Script Using Selenium IDE
  4. Create Your First SoapUI Project – Step By Step Guide
  5. Learn Selenium Series – How to use Locators in Selenium IDE
  6. How to handle Cookies in Selenium WebDriver
  7. How To Do Functional Testing In SoapUI?
  8. Planning a version in JIRA
  9. JIRA Agile User’s and Administrator’s Guide
  10. Tutorial 4: Writing The Fitnesse Fixtures – With Fitnesse Java Example

Leave a Comment Cancel reply

Share This Post

PrevPreviousGetting Started With Installation Of Selenium WebDriver – Learn Selenium
NextStart Testing Process Before Testing StartsNext

Download 200+ Software Testing Interview Questions and Answers PDF!!

QA Training

Popular Tutorial Series

Selenium
Tutorial Series For Beginners To Advanced FREE
Katalon
Tutorial for beginners, which will focus on discussing and learning Katalon Studio test automation tool.
SoapUI
Tutorial series is designed for beginners who want to start learning the WebService to advanced.
JIRA
Jira tutorial for beginners, and learn about the Atlassian JIRA tool.
Recent Posts
  • What Exactly Is a Test Environment in Software Testing?
  • Selenium Vs Playwright: Which Tool is Better?
  • Top 10 RPA Tools for 2025
  • The Ultimate Robotic Process Automation Tutorial Series
  • Accelerate your Oracle EBS Testing with OpKey’s AI powered Continuous Test Automation Platform
  • What The various DevOps Tools List?

About STC

© 2023 All Rights Reserved

Our mission is to help all testers from beginners to advanced on latest testing trends.

We provide free technical articles and tutorials that will help you to get updated in industry.

Get Started

  • Home
  • Subscribe Newsletter
  • Advertise with Us
  • Contact Us
  • Privacy Policy
  • Write For Us
  • Test Your Knowledge
  • Suggest A Tutorial

Tools

  • Selenium
  • Katalon Studio
  • Database Testing
  • Agile Testing
  • JMeter
  • TestLink
  • SoapUI
  • Jira
  • Cucumber Testing

Social