Tuesday, December 18, 2012

Difference between @BeforeTest and @BeforeMethod annotations in TestNG



@BeforeTest
@BeforeMethod
To execute a set-up method before any of the test methods included in the < test > tag in the testng.xml file.
To execute a set-up method before any of the test methods annotated as @Test.

Example Testng.xml :
<?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=".BeforetestandBeforemethod">
              <methods>
                  <include name="toBeforeTest"></include>
                  <include name="toBeforeMethod"></include>
              </methods>
          </class>
      </classes>
  </test>

 <methods>
</suite> <!-- Suite -->

Example Java code such as:
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class BeforetestandBeforemethod {

        @BeforeTest
         public void beforeTest() {
           System.out.println("@BeforeTest");
         }
        @BeforeMethod
        public void beforemethod(){
               System.out.print("@BeforeMethod");
        }
        
        public void helper() {
                  System.out.println("Flow");
                }

                @Test
                public void toBeforeMethod() {
                  helper();
                  System.out.println("BeforeMethod");
                }

                @Test
                public void toBeforeTest() {
                  helper();
                  System.out.println("BeforeTest");
                }
      
}

Execute code by TestNG, then the Output would be:

When run  <include name="toBeforeTest"></include>


Run  <include name="toBeforeMethod"></include>


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