Javatpoint Logo
Javatpoint Logo

Selenium WebDriver- First Test Case

In this section, you will learn how to create your First Selenium Automation Test Script.

Under this test, we will automate the following scenarios:

  • Invoke Google Chrome browser.
  • Open URL: www.google.com
  • Click on the Google Search text box.
  • Type the value "javatpoint tutorials"
  • Click on the Search button.

We will create our test case step by step to give you a complete understanding of each component in detail.

Step1. Launch Eclipse IDE and open project "Demo_Test" which we have created in the previous section (Configure Selenium WebDriver) of this Tutorial. We will write our first Selenium test script in the "First.class" file under the "Demo_Test" test suite.


Selenium WebDriver First Test Case

Note: To invoke a browser in Selenium, we have to download an executable file specific to that browser. For example, Chrome browser implements the WebDriver protocol using an executable called ChromeDriver.exe. These executable files start a server on your system which in turn is responsible for running your test scripts in Selenium.

Step2. Open URL: https://sites.google.com/a/chromium.org/chromedriver/downloads in your browser.

Step3. Click on the "ChromeDriver 2.41" link. It will redirect you to the directory of ChromeDriver executables files. Download as per the operating system you are currently on.


Selenium WebDriver First Test Case

For windows, click on the "chromedriver_win32.zip" download.


Selenium WebDriver First Test Case

The downloaded file would be in zipped format. Unpack the contents in a convenient directory.


Selenium WebDriver First Test Case

Note: Selenium developers have defined properties for each browser that needs the location of the respective executable files to be parsed in order to invoke a browser. For example, the property defined for Chrome browser - webdriver.chrome.driver, needs the path of its executable file - D:\ChromeDriver\chromedriver.exe in order to launch chrome browser.


Selenium WebDriver First Test Case

Step4. We would need a unique identification for the web elements like Google Search text box and Search button in order to automate them through our test script. These unique identifications are configured along with some Commands/Syntax to form Locators. Locators help us to locate and identify a particular web element in context of a web application.

The method for finding a unique identification element involves inspection of HTML codes.

  • Open URL: https://www.google.com in your Chrome browser.
  • Right click on the Google search text box and select Inspect Element.

Selenium WebDriver First Test Case
  • It will launch a window containing all the specific codes involved in the development of the test box.

Selenium WebDriver First Test Case
  • Pick the value of id element i.e. "lst-ib".

Selenium WebDriver First Test Case
  • Given below is the Java syntax for locating elements through "id" in Selenium WebDriver.
  • Here is the complete code for locating Google Search text box in our test script.
  • Now, right click on the Google Search button and select Inspect Element.

Selenium WebDriver First Test Case
  • It will launch a window containing all the specific codes involved in the development of the Google Search button.

Selenium WebDriver First Test Case
  • Pick the value of name element i.e. "btnK".

Selenium WebDriver First Test Case
  • Given below is the Java syntax for locating elements through "name" in Selenium WebDriver.
  • Here is the complete code for locating Google Search button in our test script.

Step5. Now it is time to code. We have embedded comments for each block of code to explain the steps clearly.

The Eclipse code window will look like this:


Selenium WebDriver First Test Case

Step6. Right click on the Eclipse code and select Run As > Java Application.


Selenium WebDriver First Test Case

Step7. The output of above test script would be displayed in Google Chrome browser.


Selenium WebDriver First Test Case

Explanation of the Code

Import Packages/Statements

In java, import statements are used to import the classes present in another packages. In simple words, import keyword is used to import built-in and user-defined packages into your java source file.

  1. org.openqa.selenium.WebDriver - References the WebDriver interface which is required to instantiate a new web browser.
  2. org.openqa.selenium.chrome.ChromeDriver - References the ChromeDriver class that is required to instantiate a Chrome-specific driver onto the browser instantiated by the WebDriver class.

Instantiating objects and variables

A driver object is instantiated through:

Launch Website

To launch a new website, we use navigate().to() method in WebDriver.

Click on an element

In WebDriver, user interactions are performed through the use of Locators which we would discuss in later sessions of this tutorial. For now, following instance of code is used to locate and parse values in a specific web element.

Next TopicWebDriver Commands




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials