Last active
May 8, 2019 13:39
-
-
Save hulagabal/764044a815a4296157e1adc7a31a5d33 to your computer and use it in GitHub Desktop.
Data-provider via Annotation Transformer TestNG Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package selenium; | |
import java.io.File; | |
import org.apache.poi.ss.usermodel.DataFormatter; | |
import org.apache.poi.ss.usermodel.Sheet; | |
import org.apache.poi.ss.usermodel.Workbook; | |
import org.apache.poi.ss.usermodel.WorkbookFactory; | |
import org.testng.annotations.DataProvider; | |
public class MyDataProvider { | |
static String[][] data; | |
@DataProvider(name = "mydata") | |
public static Object[][] supplyData() throws Exception { | |
return getdata(); | |
} | |
private static String[][] getdata() throws Exception { | |
Workbook workbook = WorkbookFactory.create(new File("C:\\Users\\Mitturaj.h\\Desktop\\data - Copy.xls")); | |
Sheet sheet = workbook.getSheetAt(0); | |
int rows = sheet.getLastRowNum() + 1; | |
System.out.println(rows); | |
int cols = sheet.getRow(0).getLastCellNum(); | |
System.out.println(cols); | |
data = new String[rows][cols]; | |
for (int x = 0; x < rows; x++) { | |
for (int y = 0; y < cols; y++) { | |
DataFormatter formatter = new DataFormatter(); | |
String user = formatter.formatCellValue(sheet.getRow(x).getCell(y)); | |
System.out.println(user); | |
data[x][y] = user; | |
} | |
} | |
return data; | |
} | |
public static void main(String[] args) throws Exception { | |
getdata(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package selenium; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Method; | |
import org.testng.IAnnotationTransformer; | |
import org.testng.annotations.ITestAnnotation; | |
public class MyListener implements IAnnotationTransformer { | |
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { | |
if (testMethod.getName().equals("method3")) { | |
annotation.setPriority(0); | |
annotation.setDataProviderClass(selenium.MyDataProvider.class); | |
annotation.setDataProvider("mydata"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package selenium; | |
import org.testng.annotations.Test; | |
public class SeleniumExample { | |
@Test(priority = 2) | |
public void method1() { | |
System.out.println("Method1"); | |
} | |
@Test(priority = 1) | |
public void method2() { | |
System.out.println("Method2"); | |
} | |
@Test(priority = 0) | |
public void method3(String fname, String lname, String number) { | |
System.out.println("Method3"); | |
System.out.println(fname + " " + lname + " " + number); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> | |
<suite name="Suite" parallel="none"> | |
<listeners> | |
<listener class-name="selenium.MyListener"/> | |
</listeners> | |
<test name="Test"> | |
<classes> | |
<class name="selenium.SeleniumExample"/> | |
</classes> | |
</test> <!-- Test --> | |
</suite> <!-- Suite --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment