Software Testing Class

My First Project using Selenium WebDriver

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.

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

Importing Predefined JAVA Class and Interface

Creating a class Object and variables

WebDriver driver = new FirefoxDriver();

Launch the Firefox browser and browse the Base URL

String baseWebUrl = "https://www.linkedin.com/";
driver.get(baseWebUrl);

Get the actual value of the title

String actualWebsiteTitle = driver.getTitle();

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

driver.close();

[/code]

Entire Program Termination

System.exit(0);

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.

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!!!
 
Exit mobile version