Best Testng code snippet using org.testng.Interface ITestResult.getMethod
Source:MobileScreenShotListener.java
...80 LogUtil.setLogString("\033[41;1mFailed " + System.getProperty(DriverConfig.ENV)81 + ", " + result.getName(), true);82 WaitUtil.mediumWait();83 try {84 operations.takeScreenShot(result.getMethod().getTestClass().getName()85 .substring(result.getMethod().getTestClass().getName().lastIndexOf('.') + 1)86 + "." + result.getName());87 } catch (final DeviceException e) {88 e.printStackTrace();89 }90 }91 /**92 * On test skipped.93 * @param iTestResult the i test result94 * @see org.testng.ITestListener#onTestSkipped(org.testng.ITestResult)95 */96 @Override97 public void onTestSkipped(final ITestResult iTestResult) {98 LogUtil.setLogString("\033[44;1mSkipped " + System.getProperty(DriverConfig.ENV) + " "99 + iTestResult.getName(), true);100 }101 /**102 * On test start.103 * @param iTestResult the i test result104 * @see org.testng.ITestListener#onTestStart(org.testng.ITestResult)105 */106 @Override107 public void onTestStart(final ITestResult iTestResult) {108 resetTestCaseData();109 setTestName(iTestResult.getMethod().getMethodName());110 }111 /**112 * On test success.113 * @param iTestResult the i test result114 * @see org.testng.ITestListener#onTestSuccess(org.testng.ITestResult)115 */116 @Override117 public void onTestSuccess(final ITestResult iTestResult) {118 String testClass = iTestResult.getTestClass().getName();119 testClass = testClass.substring(testClass.lastIndexOf('.') + 1, testClass.length());120 PageUtil.deleteScreenshot(testClass + "." + iTestResult.getMethod(),121 operations.getDeviceDriver());122 LogUtil.setLogString("\033[42;1mPassed " + System.getProperty(DriverConfig.ENV) + " "123 + iTestResult.getName(), true);124 }125}...
Source:ReportListener.java
...24 * @param result the result25 * @return the test name26 */27 public String getTestName(ITestResult result) {28 Test testAnnotation = result.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class);29 if (testAnnotation != null && !"".equals(testAnnotation.testName().trim())) {30 return testAnnotation.testName().trim();31 }32 return result.getTestName() != null ? result.getTestName()33 : result.getMethod().getConstructorOrMethod().getName();34 }3536 /**37 * Gets the main test name.38 *39 * @param result the result ,if not available return the Test class name40 * @return the main test name41 */42 public String getMainTestName(ITestResult result) {43 result.getMethod().isDataDriven();44 Test test = result.getTestClass().getRealClass().getDeclaredAnnotation(Test.class);45 if (test != null && !"".equalsIgnoreCase(test.testName().trim())) {46 return test.testName().trim();47 }48 return result.getTestClass().getRealClass().getSimpleName();49 }5051 /**52 * Gets the main test name.53 *54 * @param result the result ,if not available return the Test class name55 * @return the main test name56 */57 public String getMainTestDescription(ITestResult result) {58 Test test = result.getTestClass().getRealClass().getDeclaredAnnotation(Test.class);59 if (test != null && !"".equalsIgnoreCase(test.description().trim())) {60 return test.description().trim();61 }62 return "";63 }6465 /**66 * Gets the main test name.67 *68 * @param result the result ,if not available return the Test class name69 * @return the main test name70 */71 public String[] getTestGroups(ITestResult result) {72 return result.getMethod().getGroups();73 }7475 /**76 * Gets the test description.77 *78 * @param result the result79 * @return the test description80 */81 public String getTestDescription(ITestResult result) {82 return result.getMethod().getDescription() != null ? result.getMethod().getDescription() : getTestName(result);83 }8485 /* (non-Javadoc)86 * @see org.testng.ITestListener#onTestStart(org.testng.ITestResult)87 */88 @Override89 public void onTestStart(ITestResult result) {90 ExtentReportManager.startTest(getMainTestName(result), getMainTestDescription(result), getTestName(result),91 getTestDescription(result), getTestGroups(result));92 }9394 /* (non-Javadoc)95 * @see org.testng.ITestListener#onTestSuccess(org.testng.ITestResult)96 */
...
Source:MyTestNGAnnotationListener.java
...33 }34 }35 36 private boolean annotationPresent(IInvokedMethod method, Class clazz) {37 boolean retVal = method.getTestMethod().getConstructorOrMethod().getMethod().isAnnotationPresent(clazz) ? true : false;38 return retVal;39 }40 41 public void afterInvocation(IInvokedMethod method, ITestResult testResult) {42 if(method.isTestMethod()) {43 if(method.getClass().isAnnotationPresent(MyTestNGAnnotation.class)) {44 System.out.println("This gets invoked after every TestNG Test that has @MyTestNGAnnotation Annotation...");45 }46 if( !testSuccess ) {47 testResult.setStatus(ITestResult.FAILURE);48 }49 }50 }51 public void onTestStart(ITestResult result) {52 // TODO Auto-generated method stub53 54 }55 public void onTestSuccess(ITestResult result) {56 // TODO Auto-generated method stub57 58 }59 public void onTestFailure(ITestResult result) {60 // TODO Auto-generated method stub61 62 }63 public void onTestSkipped(ITestResult result) {64 // TODO Auto-generated method stub65 66 }67 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {68 // TODO Auto-generated method stub69 70 }71 public void onStart(ITestContext context) {try{72 // Method m = context.getCurrentXmlTest().getPackages().get(0).getXmlClasses().get(1).getClass().getDeclaredMethod("tMe", null);73 //System.out.println(context.getCurrentXmlTest().getPackages().get(0).getXmlClasses().get(1));74 for(ITestNGMethod m1 : context.getAllTestMethods()) {75 if(m1.getConstructorOrMethod().getMethod().isAnnotationPresent(MyTestNGAnnotation.class)) {76 //capture metadata information.77 DataSet.name = m1.getConstructorOrMethod().getMethod().getAnnotation(MyTestNGAnnotation.class).name();78 DataSet.city = m1.getConstructorOrMethod().getMethod().getAnnotation(MyTestNGAnnotation.class).city();79 DataSet.state = m1.getConstructorOrMethod().getMethod().getAnnotation(MyTestNGAnnotation.class).state();80 }81 // m.invoke(new TestCalc(), null);82 } 83 }catch(Exception e){e.printStackTrace();} 84 }85 public void onFinish(ITestContext context) {86 87 }88 89 public void transform(ITestAnnotation arg0, Class arg1, Constructor arg2,90 Method arg3) {91 92 }93}...
Source:TestngListener.java
...24 * @see org.testng.TestListenerAdapter#onTestFailure(org.testng.ITestResult)25 */26 @Override27 public void onTestFailure(ITestResult tr) {28 ITestNGMethod im = tr.getMethod();29 getCaseID(im);30 super.onTestFailure(tr);31 }32 /*33 * (non-Javadoc)34 * 35 * @see org.testng.TestListenerAdapter#onTestSkipped(org.testng.ITestResult)36 */37 @Override38 public void onTestSkipped(ITestResult tr) {39 ITestNGMethod im = tr.getMethod();40 getCaseID(im);41 super.onTestSkipped(tr);42 }43 /*44 * (non-Javadoc)45 * 46 * @see org.testng.TestListenerAdapter#onStart(org.testng.ITestContext)47 */48 @Override49 public void onStart(ITestContext testContext) {50 System.out.println("Start Test");51 super.onStart(testContext);52 }53 /*54 * (non-Javadoc)55 * 56 * @see org.testng.TestListenerAdapter#onFinish(org.testng.ITestContext)57 */58 @Override59 public void onFinish(ITestContext testContext) {60 // TODO Auto-generated method stub61 super.onFinish(testContext);62 }63 /*64 * (non-Javadoc)65 * 66 * @see org.testng.TestListenerAdapter#getFailedTests()67 */68 @Override69 public List<ITestResult> getFailedTests() {70 // TODO Auto-generated method stub71 return super.getFailedTests();72 }73 /*74 * (non-Javadoc)75 * 76 * @see org.testng.TestListenerAdapter#getPassedTests()77 */78 @Override79 public List<ITestResult> getPassedTests() {80 // TODO Auto-generated method stub81 return super.getPassedTests();82 }83 /*84 * (non-Javadoc)85 * 86 * @see org.testng.TestListenerAdapter#getSkippedTests()87 */88 @Override89 public List<ITestResult> getSkippedTests() {90 // TODO Auto-generated method stub91 return super.getSkippedTests();92 }93 /*94 * (non-Javadoc)95 * 96 * @see org.testng.TestListenerAdapter#onTestStart(org.testng.ITestResult)97 */98 @Override99 public void onTestStart(ITestResult result) {100 ITestNGMethod im = result.getMethod();101 getCaseID(im);102 super.onTestStart(result);103 }104 /**105 * Gets the case ID.106 *107 * @param im108 * the im109 * @return the case ID110 */111 private String[] getCaseID(ITestNGMethod im) {112 Method m = im.getConstructorOrMethod().getMethod();113 CaseId caseId = m.getAnnotation(CaseId.class);114 if (null != caseId) {115 for (String str : caseId.id()) {116 System.out.println("++++++++++++++++>>>>>>>>>>>>>\n\n\n" + str + "<<<<<<<\n\n");117 }118 return caseId.id();119 }120 return null;121 }122 /*123 * (non-Javadoc)124 * 125 * @see org.testng.TestListenerAdapter#getTestContexts()126 */...
Source:XrayListener.java
...26 * @see org.testng.IInvokedMethodListener#beforeInvocation(org.testng.IInvokedMethod, org.testng.ITestResult)27 */28 public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {29 if(method.isTestMethod() && annotationPresent(method, Xray.class) ) {30 testResult.setAttribute("requirement", method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(Xray.class).requirement()); 31 testResult.setAttribute("test", method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(Xray.class).test());32 testResult.setAttribute("labels", method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(Xray.class).labels());33 }34 }35 36 private boolean annotationPresent(IInvokedMethod method, Class clazz) {37 boolean retVal = method.getTestMethod().getConstructorOrMethod().getMethod().isAnnotationPresent(clazz) ? true : false;38 return retVal;39 }40 41 /* (non-Javadoc)42 * @see org.testng.IInvokedMethodListener#afterInvocation(org.testng.IInvokedMethod, org.testng.ITestResult)43 */44 public void afterInvocation(IInvokedMethod method, ITestResult testResult) {45 if(method.isTestMethod()) {46 if( !testSuccess ) {47 testResult.setStatus(ITestResult.FAILURE);48 }49 }50 }51 public void onTestStart(ITestResult result) {...
Source:TestJiraListener.java
...29/* Whenever there is a test failure, automatically it will come over here and arise a bug ticket */ 30 @Override31 public void onTestFailure(ITestResult result) {32 33 JiraPolicy jiraPolicy = result.getMethod().getConstructorOrMethod().getMethod().getAnnotation(JiraPolicy.class);34 boolean isTicketReady = jiraPolicy.logTicketReady();35 36 if (isTicketReady)37 { 38 // if isTicketReady is true raise Jira ticket:39 System.out.println("Is ticket ready for JIRA: " + isTicketReady);40 41 42 JiraServiceProvider jiraSp = new JiraServiceProvider("https://team-1610640570267.atlassian.net/",43 "joseph.ajayi@fbistech.com", "4N31bYXRZNKIk1E6It4i38FB", "BEAM");44 String issueSummary = result.getMethod().getConstructorOrMethod().getMethod().getName()45 + " - Got failed due to some assertion or exception under Jira integration with Selenium Test Script. "46 + "Create an automated ticket for failed test script";47 String issueDescription = result.getThrowable().getMessage() + "\n";48 issueDescription.concat(ExceptionUtils.getFullStackTrace(result.getThrowable()));49 jiraSp.createJiraTicket("Bug", issueSummary, issueDescription, "Joseph Ajayi [FBISTech - IT]");50 }51 52 }53 @Override54 public void onTestSkipped(ITestResult result) {55 // TODO Auto-generated method stub56 }57 @Override58 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {...
Source:TestNameListener.java
...27 private void setTestNameInXml(ITestResult tr) {28 try {29 Field mMethod = TestResult.class.getDeclaredField("m_method");30 mMethod.setAccessible(true);31 mMethod.set(tr, tr.getMethod().clone());32 Field mMethodName = BaseTestMethod.class.getDeclaredField("m_methodName");33 mMethodName.setAccessible(true);34 mMethodName.set(tr.getMethod(), tr.getTestName());35 } catch (IllegalAccessException ex) {36 Reporter.log(ex.getLocalizedMessage(), true);37 } catch (NoSuchFieldException ex) {38 ex.getMessage();39 }40 }4142 /*43 * (non-Javadoc)44 *45 * @see org.testng.TestListenerAdapter#onTestSuccess(org.testng.ITestResult)46 */47 @Override48 public void onTestSuccess(ITestResult tr) {
...
Source:SkippedExceptionTest.java
...23 test.run();24 List<ITestResult> confSkips= listener.getConfigurationSkips();25 List<ITestResult> testSkips= listener.getSkippedTests();26 Assert.assertEquals(testSkips.size(), 1);27 Assert.assertEquals(testSkips.get(0).getMethod().getMethodName(), "dummyTest");2829 Assert.assertEquals(confSkips.size(), 1);30 Assert.assertEquals(confSkips.get(0).getMethod().getMethodName(), "configurationLevelSkipException");31 }323334 @Test35 public void skippedExceptionInTestMethods() {36 TestListenerAdapter listener= new TestListenerAdapter();37 TestNG test= new TestNG(false);38 test.addListener(listener);39 test.setTestClasses(new Class[] {TestSkippedExceptionTest.class});40 test.run();41 List<ITestResult> skips= listener.getSkippedTests();42 List<ITestResult> failures= listener.getFailedTests();43 List<ITestResult> passed = listener.getPassedTests();44 Assert.assertEquals(skips.size(), 1);45 Assert.assertEquals(failures.size(), 1);46 Assert.assertEquals(passed.size(), 1);47 Assert.assertEquals(skips.get(0).getMethod().getMethodName(), "genericSkipException");48 Assert.assertEquals(failures.get(0).getMethod().getMethodName(), "timedSkipException");49 Assert.assertEquals(passed.get(0).getMethod().getMethodName(), "genericExpectedSkipException");50 }51}
...
getMethod
Using AI Code Generation
1import org.testng.ITestResult;2import org.testng.TestListenerAdapter;3public class TestListener extends TestListenerAdapter {4 public void onTestFailure(ITestResult tr) {5 System.out.println("Failed: " + tr.getMethod().getMethodName());6 }7 public void onTestSkipped(ITestResult tr) {8 System.out.println("Skipped: " + tr.getMethod().getMethodName());9 }10 public void onTestSuccess(ITestResult tr) {11 System.out.println("Passed: " + tr.getMethod().getMethodName());12 }13}14import org.testng.ITestResult;15import org.testng.TestListenerAdapter;16public class TestListener extends TestListenerAdapter {17 public void onTestFailure(ITestResult tr) {18 System.out.println("Failed: " + tr.getMethod().getMethodName());19 }20 public void onTestSkipped(ITestResult tr) {21 System.out.println("Skipped: " + tr.getMethod().getMethodName());22 }23 public void onTestSuccess(ITestResult tr) {24 System.out.println("Passed: " + tr.getMethod().getMethodName());25 }26}27import org.testng.ITestResult;28import org.testng.TestListenerAdapter;29public class TestListener extends TestListenerAdapter {30 public void onTestFailure(ITestResult tr) {31 System.out.println("Failed: " + tr.getMethod().getMethodName());32 }33 public void onTestSkipped(ITestResult tr) {34 System.out.println("Skipped: " + tr.getMethod().getMethodName());35 }36 public void onTestSuccess(ITestResult tr) {37 System.out.println("Passed: " + tr.getMethod().getMethodName());38 }39}40import org.testng.ITestResult;41import org.testng.TestListenerAdapter;42public class TestListener extends TestListenerAdapter {
getMethod
Using AI Code Generation
1ITestResult result = Reporter.getCurrentTestResult();2String methodName = result.getMethod().getMethodName();3String className = result.getTestClass().getName();4Object[] parameters = result.getParameters();5Throwable throwable = result.getThrowable();6String testName = result.getTestName();7ITestContext context = result.getTestContext();8String host = result.getHost();9String instanceName = result.getInstanceName();10Object instance = result.getInstance();11long startMillis = result.getStartMillis();12long endMillis = result.getEndMillis();13Date startDate = new Date(result.getStartMillis());14Date endDate = new Date(result.getEndMillis());
getMethod
Using AI Code Generation
1public void test2() {2 Assert.fail();3}4public void test3() {5 Assert.fail();6}7public void test4() {8 Assert.fail();9}10public void test5() {11 Assert.fail();12}13public void test6() {14 Assert.fail();15}16public void test7() {17 Assert.fail();18}19public void test8() {20 Assert.fail();21}22public void test9() {23 Assert.fail();24}25public void test10() {26 Assert.fail();27}28public void test11() {29 Assert.fail();30}31public void test12() {32 Assert.fail();33}34public void test13() {35 Assert.fail();36}
getMethod
Using AI Code Generation
1public void testMethod(){2Assert.assertEquals("TestNG is working fine", "TestNG is working fine");3}4public void testMethod1(){5Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");6}7public void testMethod2(){8Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");9}10public void testMethod3(){11Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");12}13public void testMethod4(){14Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");15}16public void testMethod5(){17Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");18}19public void testMethod6(){20Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");21}22public void testMethod7(){23Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");24}25public void testMethod8(){26Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");27}28public void testMethod9(){29Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");30}31public void testMethod10(){32Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");33}34public void testMethod11(){35Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");36}37public void testMethod12(){38Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");39}40public void testMethod13(){41Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");42}43public void testMethod14(){44Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");45}46public void testMethod15(){47Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");48}49public void testMethod16(){50Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");51}52public void testMethod17(){53Assert.assertEquals("TestNG is working fine", "TestNG is not working fine");54}55public void testMethod18(){56Assert.assertEquals("TestNG is working fine", "TestNG
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!