Monday, 15 September 2014

Get Started with Selenium Webdriver Using Java

What is Selenium Webdriver ?

Selenium is a browser automation tool, which automate the control of a browser so that repetitive tasks can be automated.

The earlier versions of Selenium were Selenium Core & Selenium RC etc. and there were some limitations. So, Webdriver came into action and replaced both of them. Those earlier versions were using selenium server for running the test where as Selenium WebDriver do not require selenium server for running test.


So, basically Webdriver in Selenium is an Interface, which is designed to accurately simulate the way that a user will interact with a web application.
Selenium Webdriver supports most of the browsers to run your test cases and many programming languages like C#, Java, Python, Ruby, .Net, Perl, PHP, etc.. Selenium Webdriver is also know as Selenium 2.0. And to go ahead with this Selenium Webdriver, we need to have decent knowledge on any of the mentioned languages above. Lets see this with Java.


For learning java(Core Java/ Java Basics), you can refer to these sites mentioned below. You may find some other sites as well from google.

http://javabeginnerstutorial.com/core-java/
http://www.tutorialspoint.com/java/



How to configure Selenium webdriver ?


  1. Download and install Java.
  2. Download Eclipse and extract to a folder, no need to install. To open Eclipse, click the 'eclipse.exe' file only. Eclipse is an IDE to write java code and run.
  3. Download Webdriver Jar files as shown in below screen shot. The client version may change as per time and release date, use the latest file.












  4. The downloaded file will be in Zip format, extract and save those.
  5. Now Start Eclipse, double click on 'eclipse.exe' file to open. Then select one Workspace folder( Create one folder in a drive and select that).

  1. Now Create new java project from File > New > Project > Java Project and enter a Name there.
    It can also be done in different way.
    File > New > Java Project and then enter a Name for Project(say 'Webdriver_First_Project'). Click finish to create that project and same will be appeared on the screen.


  1. After creating project, it will look like below.


  1. Now Create Package.
    Right click on project
    'Webdriver_First_Project' > New > Package . Say Package we gave is 'Test'. After creating package, it will look like below.

  2. Now Create Class.
    Right click on package
    'Test' > New > Class. Say class name is 'FirstWebdriverProgram'.
    Also you can check the check box 'public static void main(String[] args)', so on clicking finish, it will show the Main function in default. After creating, it will look like below.


  3. In step 3, you have downloaded the jar file and in steps 4, you have saved those files. Now we will be adding those to build path.

    Right click on project
    'Webdriver_First_Project' > Select Properties > Select Java Build Path > Libraries Tab > Click On Add External Libraries button .
    Now choose those two jar files present on the folder 'selenium-2.43.0'

    Again click on 'Add External Libraries' button and select jar field from folder 'selenium-2.43.0/libs'.

    *Note: Build path can be added in other way also.
Right click on project 'Webdriver_First_Project' > Build Path > Configure Build Path > Click On Add External Libraries button


Now all required jar files are added and can be seen under 'Referenced Libraries' folder.


                                     


Now we are ready to write our first program.

Simple java code to browse a website, type and click a button.

package Test;



import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstWebdriverProgram {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver(); // Here we initialize the Firefox driver


driver.get("http://www.seleniumhq.org/"); // Here It will open page in Firefox browser


driver.findElement(By.id("q")).sendKeys("Webdriver"); // Here it will find the search field and enter  the  phrase  'Webdriver'

driver.findElement(By.id("submit")).click(); // Here after entering the text, it will click Go button.


driver.close(); // It will close the current window


driver.quit(); // It will quit the driver and close all associated windows




}



}



In this above code, beside each line, its work is mentioned. We will get into details further.
For now, Want to share few more things.



When you initialize the driver “ WebDriver driver = new FirefoxDriver(); ”, there you will get some error. So, there we need to mouse hover on Webdriver, it will ask to import the package. Same for FirefoxDriver.


For importing related package, we can use shortcut “Shift + Ctrl + O ”. It will import everything which is required in that java file.



Here we have used some web driver methods like get, findElement, close & Quit. Apart from these, there are so many other methods also.
While typing, after driver. , hold on for a second, it will show all methods as suggestions there.



Anyways, we will be seeing all these later on in next blogs.

6 comments :