Home >>Selenium Tutorial >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:
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.
For windows, click on the "chromedriver_win32.zip" download.
The file that you access will be in zipped format. Unpack the contents in a convenient directory.
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.
driver.findElement(By.id (<element ID>))
driver.findElement(By.id ("lst-ib"))
driver.findElement(By.name (<element name>))
driver.findElement(By.name ("btnK"))
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.
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.
A driver object is instantiated through:
WebDriver driver=new ChromeDriver();
In WebDriver we use navigate() .to() method to launch a new website.
driver.navigate().to("http://www.google.com/");