My Thoughts: WebDriver Tutorial Part 3 :Writing first script using Webdriver

Sunday, August 12

WebDriver Tutorial Part 3 :Writing first script using Webdriver

If you have missed the previous post then you can have a look at here .

Now we are ready to start our first script in Webdriver using Eclipse .Open your Eclipse and follow the below steps

1.Create new Java Project
2.Add the Webdriver Jar files to the created Project
3.Create a new Package under Java Project
4.Create a Java class file under the Package
5.Write the code in the Java class file and run it.

1. Create a new Java project

Goto File >> New >>Java Project
We will get a popup which will prompt us to provide the project name.
Give the project name say "ExploreWebDriver" then click on Finish button.

Vamshi Kurra - New java project creation popup in Eclipse

2.Add the Webdriver Jar files to the created Project

Right click on created project then goto
Build Path>>Configure Build Path >>Select Libraries tab>>Add External jars
Which will open a folder search prompt , goto the locations where you have downloaded WebDriver jar files and add them .Then click on "Ok" button
You need to add below jar files.If you don't have these files downloaded on your computer then you can download them from the below pages :
http://seleniumhq.org/download/
http://code.google.com/p/selenium/downloads/list
You can also download all jars from my shared location
https://docs.google.com/folder/d/0B00rtzEfza2uZnRBZnNVWHFVNjA/edit
  • a.selenium-java-2.32.0-srcs.jar
  • b.selenium-java-2.32.0.jar
  • c.selenium-server-standalone-2.32.0.jar

Vamshi Kurra- Adding External jar files in Eclipse

3.Create a new Package under Java Project

 Goto creatd project  i.e. "ExploreWebDriver" and expand it.Now we will see a separate folder named "src". Right Click on it and then Goto
New>>Package>>Give the name of package in the "package creation" popup. Say "learning" is the name of the Package.Now click on finish button.

Vamshi Kurra - New Java Package popup

4.Create a Java class file under the Package

Right click on the created package i.e "Learing" and then goto
New>>Class>>we will get "New Java Class" popup. Enter the name of the class say "GoogleSearch" and click on Finish.

Vamshi Kurra- Adding new Java Class in Eclipse

5.Write the code in the Java class file and run it.

Copy the below code and paste it in the created Java class file.

package learning;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GoogleSearch {
 
 public static void main(String args[]){
  WebDriver driver=new FirefoxDriver();
  System.out.println("Loading Google search page");
  driver.get("http://google.com");
  System.out.println("Google search page loaded fine"); 
 }

}

Here is how your Eclipse project hierarchy looks like :

Vamshi Kurra- Eclipse project hierarchy

Now Click run .
Script will run successfully and a new firefox window will be opened with the Google.com .

8 comments:

  1. I want to open internet explorer not firefox browser...
    If u know plz tell me how?

    ReplyDelete
  2. Below post is for opening interner explorer. http://www.mythoughts.co.in/2012/05/starting-opera-browser-using.html

    ReplyDelete
  3. Naveen ChhaniwalFriday, May 10, 2013

    Hi vamshi,

    I am new on selenium. i am trying to implement your code, but i colud not open google page. i could not identify problem can you help me.


    Thanks in advance

    ReplyDelete
  4. Naveen ChhaniwalFriday, May 10, 2013

    issue is resolved now, it's happens because of old version of jar files......:)

    ReplyDelete
  5. if i dont know java or any other lang. then can i record scripts using IDE and export them in eclipse in java/webdriver format??

    ReplyDelete
  6. Selenium IDE has its own limitations. In some cases export works fine but in most of the cases it may not work due to the limitations of Selenium IDE.

    It is good to have basic of java and guess what they are good enough to work :)

    ReplyDelete
  7. Greetings! I've been reading your weblog for some time now and finally got the
    bravery to go ahead and give you a shout out from Humble Tx!
    Just wanted to say keep up the excellent job!

    My web-site; Wholesale Snapbacks

    ReplyDelete