In last section, we have gone through the download and installation of Selenium client server. Now we can write a sample test case script in eclipse.
To proceed with sample selenium test case, first we need to analyse the requirement. According to requirement we will create the test scenario. After creating scenario we will make the test case based on each scenario.
Here we will discuss on creating test case script for a sample login form. The form has User name, Password and a log in button. Let’s make test case for Login scenario. Below is the sample Test step for this scenario.
- Open the web page
- Provide User name
- Provide password
- Click on Log in Button
- Verify the success page
Now we can create another test case for clear scenario also.
- Open the Web page
- Provide the User name
- Provide the Password
- Click on clear
- Verify User name and password(User name and password should be cleared)
Let’s create the sample selenium Test case script for the above Log in scenarion in Selenium.
<html> <body> <form action="loginAction" id="loginForm"> <label>User name</label> <input type="text" name="username"><br> <label>Password:;</label> <input type="text" name="password"><br> <button type="submit" id="loginButton">Log In</button> <button type="reset" id="reset">Clear</button> </form> </body> </html>

<html> <body> <p class="message" id="loginResponse">Welcome to foo. You logged in successfully.</p> </body> </html>

import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Testcase1 { public static void main(String[] args) { System.setProperty ("webdriver.firefox.marionette" , "C:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); String baseUrl = "http://www.testinglpoint.com/test/newtours/"; String expectedTitle = "TestingLPoint"; String actualTitle = ""; driver.get(baseUrl); actualTitle = driver.getTitle(); if (actualTitle.contentEquals(expectedTitle)){ System.out.println("Test Passed!"); } else { System.out.println("Test Failed"); } driver.close(); } }
Explanation:
Above code is the sample selenium example of the verifying the Home page title. Here we have defined the FirefoxDriver() to run our test case in Firefox browser and save this response in driver object.
After that we call get() to load the home page URL of the website. This method will create the new session and load the page that has mentioned as parameter in get().
After that, Line, this will call getTitle() to retrieve the page title of the page and actualTitle.contentEquals(expectedTitle) will verify whether the actual title and expected title are same or not.
driver.close() will close the active browser opened for testing. If you want to close all browser which were opened through multiple WebDriver instance, you can use driver.quit().
driver.quit() will close all browser that were opened through the current test script.
You can create more complicated test case by using in-build selenium methods.