My Thoughts: August 2012

Saturday, August 18

WebDriver Tutorial Part 4 : Working with TestNG framework in Eclipse


Writing a script in plain Java is simple like what we have done in the previous post. But there is more we can do using WebDriver.

Say If you want to run multiple scripts at a time ,better reporting and you want to go for datadriven testing (Running the same script with multiple data) then plain Java script is not enough .

So it is recommended to use any of the existing frameworks like TestNG or JUnit. Select one of the framework and start using it.

In this post , I start with TestNG. If you are planning to use TestNG then you need to
  • 1.Install TestNG Eclipse plug-in 
  • 2.Customize the output directory path
  • 3.Start writing scripts
1.Install TestNG Eclipse plug-in 

Detailed instruction on how to install TestNG plug-in can be found here

2.Customize the output directory path

This is not a mandatory step. But it is good to have it.Using this you can tell TestNG where to store all the output results files. 

In Eclipse Goto Window>>Preferences>>TestNG>>
Set the Output Direcory location as per your wish and click on "Ok".

Vamshi Kurra -TestNG

3.Start writing scripts
Writing scripts using TestNG is so simple. We will start with the editing of script we have written in the previous post .
Here is the edited script using TestNG.
package learning;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class GoogleSearchTestNG {

 WebDriver driver;
 
 @BeforeTest
 public void start(){
  driver = new FirefoxDriver();
 }
 
 @Test
 public void Test(){ 
  System.out.println("Loading Google search page");
  driver.get("http://google.com");
  System.out.println("Google search page loaded fine");
 }
 
 @AfterTest
 public void close(){
  driver.quit(); 
 }
}

If you look at the above code the main chnages we have done is 
1.Imported new TestNG files

2.Now there is no main function.We removed it.

3.We have split the single main function into 3 functions. i.e start(), test(), close()
start()  -- Initialize the WebDriver
Test()  -- Perform our exact requirement.
close() -- Close the Browser once

4.There are 3 different annotations named "BeforeTest" , "Test" , "AfterTest".
@BeforeTest -- Keep this annotaion before a method which has to be called initially when you run the script. In our script ,we put it before start() method because we want to initialize the WebDriver first.
@Test    -- Keep this annotation before a method which will do the exact operation of your script.
@AfterTest -- Keep this annotation before mehod which has to run at the end. In our script , closing browser has to be done at the end. So we put this annotation before close() method.

TestNG is so powerful . But now we stop here :) and experiment more on WebDriver functions. Once that is done we will come back to TestNG.

Can't wait and learn more ?? , you can always refer documents here

Going forward all the example scripts in this site will refer to TestNG.

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 .

Sunday, August 5

WebDriver Tutorial Part 2 : Installing Java and Eclipse IDE


Once we are familiar with selenium IDE , the next step is "Choosing a programnning language" and start scripting .

Selenium supports multiple languages like Java, C#, Phython, Ruby,Php, Perl . Depending upon your flexibility select any one of the language as your scripting langauge and install that language components.

Suppose If you are planning to use Java as your programming language (like me) then here are steps for installing it.

Step 1 : Installing Java:

Before installing check whether your computer already has Java or not , by visiting
http://java.com/en/download/installed.jsp

If you don't have Java istalled on your computer , then you can start installing Java by visiting
http://www.java.com/en/download/help/windows_manual_download.xml
 Above site will have step by step instructions on how to install Java.

Step 2: Set up Path and Class Path for Java :

Here are the step to step instructions on how to setup PATH variables in java (with screenshots :) )
http://www.roseindia.net/java/java-classpath.shtml

Here is the website which describes all the above details clearly
http://www.ugrad.cs.ubc.ca/~cs211/tutorials/Eclipse/Eclipse_Java_Windows_XP.html

Step 3 :Installing Eclipse 

Eclipse is an opensource IDE(Integrated Development Environment ). Download Eclipse from
http://www.eclipse.org/downloads

Follow the below tutorial which gives the basic idea on how to configure Eclipse and use it.
http://www.vogella.com/articles/Eclipse/article.html