Home >>Selenium Tutorial >Selenium WebDriver First Test Case

Selenium WebDriver First Test Case

Selenium WebDriver - First Test Case

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

We'll be automating the following situations under this test:

  • Trigger a browser running Google Chrome.
  • Official URL: www.google.com
  • Click on the text box in Google Quest.
  • Type the value "phptpoint tutorials"
  • Click the Search button.

Step by step we will build our test case to give you an in-depth overview of each aspect.

Step 1. Start Eclipse IDE and open "Demo Test" project that we developed in the previous section of this Tutorial (Configure Selenium WebDriver). Under the "Demo_Test" test suite, we must compose our first Selenium test script into the "First.class" folder.

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

Step 3. Click on the link named "ChromeDriver 2.41." It will lead you to the executables files directory at ChromeDriver. Download according to the latest operating system you are running.

Selenium WebDriver

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

Selenium WebDriver

The file that you access will be in zipped format. Unpack the contents in a convenient directory.

Selenium WebDriver

Step 4. For the web elements such as Google Search text box and Search button we will need a specific identifier to simplify them via our test script. Such specific identifiers are designed to form Locators along with other Commands / Syntax. Locators help us define and locate a specific site element in the sense of a web application.

The procedure for identifying a particular element of identity involves inspecting the HTML codes.

Selenium WebDriver

Selenium WebDriver

driver.findElement(By.id (<element ID>))
driver.findElement(By.id ("lst-ib"))  

Selenium WebDriver

Selenium WebDriver

driver.findElement(By.name (<element name>))  
driver.findElement(By.name ("btnK"))
  • Open URL: https://www.google.com in your Chrome browser.
  • Right click on the Google search text box and select Inspect Element.
  • It will open a window that contains all of the specific codes involved in test box creation.
  • Pick the value of id element i.e. "lst-ib".
  • The Java syntax for locating elements by “Id" in Selenium WebDriver is provided below.
  • 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
  • It will open a window that contains all of the different codes involved in Google Search button creation.
  • Pick the value of name element i.e. "btnK".
  • 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. For each block of code we've embedded comments to explain the steps clearly.

import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  
  
public class First {  
  
    public static void main(String[] args) {  
        
    // declaration and instantiation of objects/variables  
    System.setProperty("webdriver.chrome.driver", "D:\\ChromeDriver\\chromedriver.exe");  
    WebDriver driver=new ChromeDriver();  
      
// Launch website  
    driver.navigate().to("http://www.google.com/");  
          
    // Click on the search text box and send value  
    driver.findElement(By.id("lst-ib")).sendKeys("phptpoint tutorials");  
          
    // Click on the search button  
    driver.findElement(By.name("btnK")).click();  
      
    }  
  
}  

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

Step 7. The output of the test script above will show on the Google Chrome browser.

Explanation of the Code Import Packages/Statements

Import statements in java are used to import the classes that are present in other packages. Import keyword is used in simple words to import built-in and user-defined packages into your source file for java.

  • Org.openqa.selenium. WebDriver — The WebDriver app to launch a modern web browser.
  • Org.openqa.selenium.chrome. ChromeDriver — Describes the version of ChromeDriver used to install a Chrome-specific driver on a WebDriver level enabled application.

Instantiating objects and variables

A driver object is instantiated through:

WebDriver driver=new ChromeDriver();  

Launch Website

In WebDriver we use navigate() .to() method to launch a new website.

driver.navigate().to("http://www.google.com/");  

No Sidebar ads