Monday, December 17, 2012

TestNG – Test Automation with Selenium



Why do you use TestNG?
You see that TestNG is framework support get proper reports, handle checkpoint or excaption handling.
You can to use Junit or TestNG or anything else to support  for you.
In previous post, I have explained  setup TestNG in eclipse the following link such as: http://howtesting.blogspot.com/2012/12/setting-up-testng-with-eclipse.html


I have a sort code below:

public class google_login extends SeleneseTestBase {
       WebDriver driver;
       String url;

@BeforeMethod
       public void setUp()throws Exception{
      
              driver = new FirefoxDriver();
              url = "http://www.gmail.com";
              driver.get(url);
       }
      
@Test
       public void rungoogle() throws IOException, InterruptedException, BiffException, Exception{
        //    driver = new FirefoxDriver();
       //     String url = "http://www.gmail.com";
              driver.getCurrentUrl();
              File file = new File("D:\\ECLIPSE\\workspace_eclipseclassic\\seleniumdriver\\test.xls");
             
              //Reading data in Excel file
              Workbook wb = Workbook.getWorkbook(file);
              jxl.Sheet sheet = wb.getSheet(0) ;
              int rows = sheet.getRows(); 
              String filename="D:\\ECLIPSE\\workspace_eclipseclassic\\seleniumdriver\\result.xls" ;
             
              //Create sheet
              HSSFWorkbook hwb = new HSSFWorkbook();
              HSSFSheet sheet1 =  ((HSSFWorkbook) hwb).createSheet("Login Info");
             
              //Create row
              HSSFRow row1 =   sheet1.createRow(0);

              for(int row = 0; row < rows; row ++){
                     driver.get(url);
                    
                     //Reading data from excel file to write element Email fields
                     WebElement email = driver.findElement(By.id("Email"));
                     email.clear();
                     String emailuser = sheet.getCell(0,row).getContents();
                     email.sendKeys(emailuser);
                    
                     WebElement pass = driver.findElement(By.id("Passwd"));
                     pass.clear();
                     String passuser = sheet.getCell(1,row).getContents();
                     pass.sendKeys(passuser);
                    
                     WebElement btnlogin = driver.findElement(By.id("signIn"));
                     btnlogin.click();
                    
                     driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
             
                     try{
                           if(driver.findElement(By.id("gbqfq")).isDisplayed())
                           System.out.println("Passed");
                           row1.createCell(row).setCellValue("Pass");
                           driver.manage().deleteAllCookies();
                     }
                     catch(NoSuchElementException e){ 
                           System.out.println("Failed");
                           row1.createCell(row).setCellValue("Fail");
                     }
                     //    
                     FileOutputStream fileOut =  new FileOutputStream(filename);
                     hwb.write(fileOut);
                     fileOut.close();
       public static void writeresult(String result) throws IOException{
                        ByteArrayOutputStream f = new ByteArrayOutputStream();
                        byte buf[] = result.getBytes();
                        f.write(buf);
                        OutputStream f2 = new FileOutputStream("D:\\ECLIPSE\\workspace_eclipseclassic\\seleniumdriver\\output.xls");
                        f.writeTo(f2);
                        f2.close();
                        f.close();   
                  }//writeresult
              }
        
@AfterMethod
public void tearDown(){
       driver.close();
}




The Java class “Google_Demo” is implemented in Eclipse IDE using TestNG framework.
In the above code, you see “setUp” and “tearDown” which are marked with @BeforeMethod and @AfterMethod annotations and @Test annotation.

Hence Before executing each test, setUp method will be executed. After the execution of each test, tearDown gets executed.
How to execute the test?
Click Run –> Run As –> TestNG Test
 

 

Output of the execution as below:


You should be Convert to TestNG.



It’s displayed such as:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test">
    <classes>
      <class name="com.dienmay_login"/>
    </classes>
   
  </test> <!-- Test -->
</suite> <!-- Suite -->





You can config some function in TestNG. Run and show the report of TestNG






10 comments:

  1. hi ,

    All of your posting are really useful for selenium tester , Its really great effort .

    -Jash

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.

    angularjs-Training in velachery

    angularjs-Training in annanagar

    angularjs Training in chennai

    angularjs Training in chennai

    ReplyDelete
  4. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    blueprism training in chennai | blueprism training in bangalore | blueprism training in pune | blueprism online training

    ReplyDelete
  5. I know you feel more happy when you get things done and best of all those things are your most precious treasure.
    Java training in Tambaram | Java training in Velachery

    Java training in Omr | Oracle training in Chennai

    ReplyDelete
  6. I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete