In this section, you will learn how to create your First Selenium Automation Test Script.
Under this test, we will automate the following scenarios:
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.
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.
For windows, click on the "chromedriver_win32.zip" download.
The downloaded file would be in zipped format. Unpack the contents in a convenient directory.
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.
Step5. Now it is time to code. We have embedded comments for each block of code 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("rookienerd tutorials"); // Click on the search button driver.findElement(By.name("btnK")).click(); } }
The Eclipse code window will look like this:
Step6. Right click on the Eclipse code and select Run As > Java Application.
Step7. The output of above test script would be displayed in Google Chrome browser.
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.
A driver object is instantiated through:
WebDriver driver=new ChromeDriver();
To launch a new website, we use navigate().to() method in WebDriver.
driver.navigate().to("http://www.google.com/");
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.
driver.findElement(By.id("lst-ib")).sendKeys("rookienerd tutorials");