Writing your first test case in Selenium WebDriver

Writing your first test case in Selenium WebDriver

In the following post I will go step by step and explain how to write your first test case using Selenium Webdriver and execute it using TestNG.

1. Right click on the src folder ->New -> Package.

2. Provide package name something like com.stm.test and click “Finish”.

3. Right click on the newly created package – > New -> Class.

4. Provide class name as “RegistrationTest” and click Finish.

5. Write the code given below for your first test.

I am taking an example of automating register user functionality for demo site http://newtours.demoaut.com

The code for the first test is as follows:


package com.stm.test;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;

import org.testng.annotations.Test;

public class RegistrationTest


@Test //This is TestNG annotation

public void testRegister()


WebDriver driver = new FirefoxDriver();






driver.findElement(By.name(“userName”)).sendKeys(“[email protected]”);

driver.findElement(By.name(“address1”)).sendKeys(“Test Address”);

driver.findElement(By.name(“city”)).sendKeys(“Test City”);

Select select = new Select(driver.findElement(By.name(“country”)));


driver.findElement(By.name(“email”)).sendKeys(“[email protected]”);








6. After finishing the test right click on the test and click on RunAs – >TestNG Test

7. After executing the test select the project and press F5 to refresh the project. A new folder “test-results” will get created which will show you the results for the execution. Right click on index.html->open with->web browser to see the execution report.

If you like this post hit like button for facebook and share it!!


Share via
Copy link
Powered by Social Snap