Showing posts with label testng. Show all posts
Showing posts with label testng. Show all posts

Tuesday, March 12, 2019

Run failed test cases in Java


Create a class that fails test case


 package testNG;  
 import org.testng.Assert;  
 import org.testng.annotations.Test;  
 public class TestFailClass {  
      @Test(retryAnalyzer=MyRetry.class)  
      public void failMethod(){  
           System.out.println("FAILED CLASS");  
           Assert.assertTrue(false);  
      }  
 }  

Create a class that re-runs the failed test cases

 package testNG;  
 import java.util.ArrayList;  
 import java.util.List;  
 import org.testng.TestNG;  
 public class RunFailed {  
      public static void main(String[] args) {  
           TestNG testRunner = new TestNG();  
           List<String> failedXmls = new ArrayList<String>();  
           //Enter the full path for the 'test-faailed.xml' file  
           failedXmls.add("C:/Users/Mutturaj.h/workspace/hulagabal/test-output/testng-failed.xml");  
           testRunner.setTestSuites(failedXmls);  
           testRunner.run();  
      }  
 }  

Retry failed test cases TestNG in Java


Create test cases which fails

 package testNG;  
 import org.testng.Assert;  
 import org.testng.annotations.Test;  
 public class TestFailClass {  
      @Test(retryAnalyzer=MyRetry.class)  
      public void failMethod(){  
           Assert.fail("Test failed");  
      }  
 }  

Create a retry class which implements IRetryAnalyzer interface like below which tries for 3 times

 package testNG;  
 import org.testng.IRetryAnalyzer;  
 import org.testng.ITestResult;  
 public class MyRetry implements IRetryAnalyzer {  
      private int retryCount = 0;  
      private static final int maxRetryCount = 3;  
      public boolean retry(ITestResult result) {  
           if (retryCount < maxRetryCount) {  
                retryCount++;  
                return true;  
           }  
           return false;  
      }  
 }  

Monday, February 18, 2019

Retrieve data from excel , data provider and TestNG

 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.IOException;  
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
 import org.apache.poi.ss.usermodel.Row;  
 import org.apache.poi.ss.usermodel.Sheet;  
 import org.apache.poi.ss.usermodel.Workbook;  
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
 import org.testng.annotations.DataProvider;  
 public class PoiEx {  
      static Object[][] names = null;  
      static String filePath = "C:\\Users\\Mutturaj\\Desktop";  
      public static Object[][] readExcel(String filePath, String filename, String sheetName) throws IOException {  
           File file = new File(filePath + "\\" + filename);  
           FileInputStream fileInputStream = new FileInputStream(file);  
           Workbook workbook = null;  
           String fileextensionname = filename.substring(filename.indexOf("."));  
           if (fileextensionname.equals(".xlsx")) {  
                workbook = new XSSFWorkbook(fileInputStream);  
           } else if (fileextensionname.equals(".xls")) {  
                workbook = new HSSFWorkbook(fileInputStream);  
           }  
           // Get sheet by name  
           Sheet sheet = workbook.getSheet(sheetName);  
           // Count rows in the sheet  
           int rowCount = sheet.getLastRowNum();  
           int colCount = 2;  
           names = new Object[rowCount + 1][colCount];  
           for (int i = 0; i < rowCount + 1; i++) {  
                Row row = sheet.getRow(i);  
                int lastCellNumber = row.getLastCellNum();  
                for (int j = 0; j < lastCellNumber; j++) {  
                     names[i][j] = (row.getCell(j).getStringCellValue());  
                }  
           }  
           return names;  
      }  
      @DataProvider(name = "array")  
      public Object[][] nana() throws IOException {  
           Object[][] array = PoiEx.readExcel(filePath, "muttu.xlsx", "muttu");  
           return array;  
      }  
      
 }  



 import org.testng.annotations.AfterMethod;  
 import org.testng.annotations.AfterTest;  
 import org.testng.annotations.BeforeMethod;  
 import org.testng.annotations.BeforeTest;  
 import org.testng.annotations.Test;  
 public class NGTest1 {  
      @BeforeTest()  
      public void loadFile() {  
           System.out.println("File open....");  
      }  
      @BeforeMethod  
      public void openBrowser() {  
           System.out.println("Browser opened************");  
      }  
      @AfterMethod  
      public void closeBrowser() {  
           System.out.println("**************Close browser");  
      }  
      @Test(priority = 1, dataProvider = "array", dataProviderClass = PoiEx.class)  
      public void add(String first, String second) {  
           System.out.println(first);  
           System.out.println(second);  
      }  
      @Test(priority = 2)  
      public void substract() {  
           System.out.println("Substraction");  
      }  
      @Test(priority = 3)  
      public void Multiply() {  
           System.out.println("Multiply");  
      }  
      @Test(priority = 4)  
      public void Divide() {  
           System.out.println("Divide");  
      }  
      @AfterTest  
      public void closeFile() {  
           System.out.println("File closed.....");  
      }  
 }