Getting Started With TestNG And Selenium Web Driver

Prerequisites

What We Are Building

We will be building a TestNG Framework using Java which will run on Chrome browser. This is a basic understanding of Selenium Web Driver.

If you are beginner to all these then it’s for you, follow this article to setup and start testing with Selenium Web Driver.

Setting Up Selenium and Eclipse

We will start by setting up Environment Variable for Java, to do that please download and install Java Development Kit from the above link and navigate to the following directory.

C:\Program Files\Java\jdk-14.0.1\bin


Now, we need to set this URI as a PATH variable in our System’s Environment Variable, if you are not sure about how to do that you can follow this instruction here.

To finalize this JDK setup, we need to add a User Variable with the following Variable Name and Value:

Variable Name: JAVA_HOME
Variable Value: C:\Program Files\Java\jdk-14.0.1

You may test your installation by typing the following command in your Terminal / Console.

λ java -version
Java version "14.0.1" 2020-04-14
Java(TM) SE Runtime Environment (build 14.0.1+7)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mix mode, sharing)

Assuming that you have already downloaded Eclipse IDE, let’s create one new project by clicking on File > New > Java Project and one package inside of that project directory.

Now you will have to include Selenium JAR files into project’s class path from Configure Build Path option, to do that right click on the project directory and select Build Path>Configure Build Path

On the Java Build Path window, select Classpath > Add External JARs...

Now you need to download and import all the JAR files provided by Selenium Package.

Next, you need to download and import the TestNG JAR files and dependencies into Build Path as shown above. Now it's the time to add TestNG plugins for eclipse from Eclipse Marketplace as shown in below.

In this tutorial, you will be creating one test script for the following test case:
#1. Verify Website Title

To perform the above test, create one java class TitleVerification.java inside of the project directory and paste the following code.

You need to mention your chrome driver's path here:
System.setProperty("webdriver.chrome.driver", "D:\\Drivers\\chromedriver_win32\\chromedriver.exe");

Now your dish is almost ready & just one step to go, that is you will have to execute the above code as TestNG, to do that right click on the context menu and select Run As > TestNG

If everything goes right, you will see this on your ecplise IDE's Console, indicating that the given test case PASSED

Selenium Web Driver is a powerful tool for web based application testing as well as it supports different kinds of reporting utilities.
Selenium offers great ease of use and flexibility of choosing any scripting language / different browsers / operating systems, it enables you to create powerful tests that scale.

You can download this complete tutorial from my GitHub Repository

Copy link
Powered by Social Snap