Wednesday, 3 July 2013

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.
  1. package learning;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.firefox.FirefoxDriver;  
  5. import org.testng.annotations.AfterTest;  
  6. import org.testng.annotations.BeforeTest;  
  7. import org.testng.annotations.Test;  
  8.   
  9. public class GoogleSearchTestNG {  
  10.   
  11.  WebDriver driver;  
  12.    
  13.  @BeforeTest  
  14.  public void start(){  
  15.   driver = new FirefoxDriver();  
  16.  }  
  17.    
  18.  @Test  
  19.  public void Test(){   
  20.   System.out.println("Loading Google search page");  
  21.   driver.get("http://google.com");  
  22.   System.out.println("Google search page loaded fine");  
  23.  }  
  24.    
  25.  @AfterTest  
  26.  public void close(){  
  27.   driver.quit();   
  28.  }  
  29. }  
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.

No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...