How to use getTestClass method of org.testng.Interface ITestResult class

Best Testng code snippet using org.testng.Interface ITestResult.getTestClass

Source:TestNGRunner.java Github

copy

Full Screen

...201 recordResult(result, ResultType.SUCCESS, result.getThrowable());202 }203 @Override204 public void onTestSkipped(ITestResult result) {205 @Nullable Throwable throwable = failedConfigurationTestClasses.get(result.getTestClass());206 if (throwable == null) {207 recordResult(result, ResultType.ASSUMPTION_VIOLATION, result.getThrowable());208 } else {209 recordResult(result, ResultType.FAILURE, throwable);210 }211 }212 @Override213 public void onTestFailure(ITestResult result) {214 recordResult(result, ResultType.FAILURE, result.getThrowable());215 }216 @Override217 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {218 recordResult(result, ResultType.FAILURE, result.getThrowable());219 }220 @Override221 public void onStart(ITestContext context) {222 // Create an intermediate stdout/stderr to capture any debugging statements (usually in the223 // form of System.out.println) the developer is using to debug the test.224 originalOut = System.out;225 originalErr = System.err;226 rawStdOutBytes = new ByteArrayOutputStream();227 rawStdErrBytes = new ByteArrayOutputStream();228 stdOutStream = streamToPrintStream(rawStdOutBytes, System.out);229 stdErrStream = streamToPrintStream(rawStdErrBytes, System.err);230 System.setOut(stdOutStream);231 System.setErr(stdErrStream);232 mustRestoreStdoutAndStderr = true;233 }234 @Override235 public void onFinish(ITestContext context) {236 if (mustRestoreStdoutAndStderr) {237 // Restore the original stdout/stderr.238 System.setOut(originalOut);239 System.setErr(originalErr);240 // Get the stdout/stderr written during the test as strings.241 stdOutStream.flush();242 stdErrStream.flush();243 mustRestoreStdoutAndStderr = false;244 }245 }246 private void recordResult(ITestResult result, ResultType type, Throwable failure) {247 String stdOut = streamToString(rawStdOutBytes);248 String stdErr = streamToString(rawStdErrBytes);249 String className = result.getTestClass().getName();250 String methodName = getTestMethodNameWithParameters(result);251 long runTimeMillis = result.getEndMillis() - result.getStartMillis();252 results.add(253 new TestResult(className, methodName, runTimeMillis, type, failure, stdOut, stdErr));254 }255 private String streamToString(ByteArrayOutputStream str) {256 try {257 return str.size() == 0 ? null : str.toString(ENCODING);258 } catch (UnsupportedEncodingException e) {259 return null;260 }261 }262 private PrintStream streamToPrintStream(ByteArrayOutputStream str, PrintStream fallback) {263 try {264 return new PrintStream(str, true /* autoFlush */, ENCODING);265 } catch (UnsupportedEncodingException e) {266 return fallback;267 }268 }269 @Override270 public void onConfigurationSuccess(ITestResult iTestResult) {}271 @Override272 public void onConfigurationFailure(ITestResult iTestResult) {273 failedConfigurationTestClasses.put(iTestResult.getTestClass(), iTestResult.getThrowable());274 }275 @Override276 public void onConfigurationSkip(ITestResult iTestResult) {}277 }278 private static class JUnitReportReporterWithMethodParameters extends JUnitReportReporter {279 @Override280 public String getTestName(ITestResult result) {281 return getTestMethodNameWithParameters(result);282 }283 }284}...

Full Screen

Full Screen

Source:ListenerWithReports.java Github

copy

Full Screen

...34 35 @Override36 public void onTestStart(ITestResult result) {37 System.out.println("-------------------------");38 System.out.println("Test " +result.getTestClass() + result.getName() + " has started");39 40 test=reports.startTest(result.getMethod().getMethodName());41 42 test.log(LogStatus.INFO, result.getMethod().getMethodName()+" Test has started");43 }44 45 @Override46 public void onTestSuccess(ITestResult result) {47 System.out.println("-------------------------");48 System.out.println("Test "+result.getTestClass() + result.getName() + " is sucessful ");49 50 test.log(LogStatus.PASS, result.getMethod().getMethodName()+" Test is passed"); 51 }52 53 @Override54 public void onTestFailure(ITestResult result) {55 System.out.println("-------------------------");56 System.out.println("Test " +result.getTestClass() + result.getName() + " has Failed");57 58 System.out.println("Error message is " + result.getThrowable()); 59 60 test.log(LogStatus.FAIL, result.getMethod().getMethodName()+" Test is Failed because of error " + result.getThrowable()); 61 62 srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);63 //FileUtils.copyFile(srcFile, new File("C:\\temp\\Demo_" +AddTime()+"_screenshot1.png"));64 65 try {66 67 FileUtils.copyFile(srcFile, new File("src/POM/test_" + result.getClass().getName()68 + AddTime()+"_screenshot.png"));69 70 } catch (IOException e) {71 // TODO Auto-generated catch block72 e.printStackTrace();73 }74 75 76 }77 78 @Override79 public void onTestSkipped(ITestResult result) {80 System.out.println("-------------------------");81 System.out.println("Test "+result.getTestClass() + result.getTestName() + " was skipped");82 System.out.println("Error message is " + result.getThrowable());83 test.log(LogStatus.SKIP, result.getMethod().getMethodName()+" Test is Skipped because of error" + result.getThrowable());84 }85 86 @Override87 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {88 // TODO Auto-generated method stub89 90 }91 92 @Override93 public void onStart(ITestContext context) {94 System.out.println("-------------------------");95 System.out.println("on start");...

Full Screen

Full Screen

Source:MobileScreenShotListener.java Github

copy

Full Screen

...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}...

Full Screen

Full Screen

Source:ReportListener.java Github

copy

Full Screen

...40 * @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(); ...

Full Screen

Full Screen

Source:ScreenShotListener.java Github

copy

Full Screen

...57 */58 @Override59 public void onTestFailure(ITestResult result) {60 WaitUtil.mediumWait();61 PageUtil.saveScreenshot(result.getMethod().getTestClass().getName()62 .substring(result.getMethod().getTestClass().getName().lastIndexOf('.') + 1)63 + "." + result.getName(), DriverConfig.getDriver());64 }65 /**66 * On test skipped.67 * @param iTestResult the i test result68 * @see org.testng.ITestListener#onTestSkipped(org.testng.ITestResult)69 */70 @Override71 public void onTestSkipped(ITestResult iTestResult) {72 DriverConfig.setLogString("\033[44;1mSkipped " + System.getProperty(DriverConfig.ENV) + " " + System.getProperty(DriverConfig.BROWSER) + " " + iTestResult.getName(), true);73 }74 /**75 * On test start.76 * @param iTestResult the i test result...

Full Screen

Full Screen

Source:TestListener.java Github

copy

Full Screen

...21 }22 public void onTestStart(ITestResult result) {23 ExtentReportListener.getStartTestName(result);24 ExtentReportListener.status(LogStatus.INFO, "Execution of Test is started");25 Logs.INFO("******** Test Execution Started : " + result.getTestClass().getRealClass().getSimpleName() + " ********");26 ExtentReportListener.flush();27 }28 public void onTestSuccess(ITestResult result) {29 Logs.INFO("******** Test Execution Successfully Completed : " + result.getTestClass().getRealClass().getSimpleName() + " ********");30 31 ExtentReportListener.status(LogStatus.INFO, "TestCase execution is Completed");32 ExtentReportListener.endTest();33 ExtentReportListener.flush();34 }35 public void onTestFailure(ITestResult result) {36 Logs.INFO("!!!!!!!!! Test Execution Failed : " + result.getTestClass().getRealClass().getSimpleName() + " !!!!!!!!!");37 ExtentReportListener.status(LogStatus.FAIL, "Test Case is failed");38 39 try { 40 filepath = extReport.CaptureScreenshot(result.getTestClass().getRealClass().getSimpleName().substring(0, 6));41 } catch (Exception ex) {42 Logs.ERROR("Exception in capturing screenshot - " + ex.getMessage());43 }44 if (result.getThrowable() != null) {45 message = result.getThrowable().getMessage();46 try {47 } catch (Exception ex) {48 Logs.ERROR(ex.getMessage());49 }50 51 ExtentReportListener.status(LogStatus.FAIL, "Test Case is failed due to: " + message);52 }53 ExtentReportListener.endTest();54 ExtentReportListener.flush();55 }56 public void onTestSkipped(ITestResult result) {57 58 Logs.INFO("Test Skipped : " + result.getTestClass().getRealClass().getSimpleName());59 ExtentReportListener.status(LogStatus.SKIP, "TestCase is Skipped");60 61 try {62 filepath = extReport.CaptureScreenshot(result.getTestClass().getRealClass().getSimpleName().substring(0, 6));63 } catch (Exception ex) {64 Logs.INFO("Exception in capturing screenshot - " + ex.getMessage());65 }66 if (result.getThrowable() != null) {67 message = result.getThrowable().getMessage();68 try {69 } catch (Exception ex) {70 Logs.INFO(ex.getMessage());71 }72 ExtentReportListener.status(LogStatus.SKIP, "Test Case is Skipped due to: " + message);73 }74 ExtentReportListener.endTest();75 ExtentReportListener.flush();76 }...

Full Screen

Full Screen

Source:Listeners_Example.java Github

copy

Full Screen

...17 }18 public void onTestFailure(ITestResult result) {19 System.err.println("I failed");20 //get class name of failed test21 System.out.println(result.getTestClass() + " name of class");22 //get method name of failed test23 System.out.println(result.getName() + " name of failed method");24 }25 public void onTestSkipped(ITestResult result) {26 }27 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {28 }29 public void onTestFailedWithTimeout(ITestResult result) {30 this.onTestFailure(result);31 }32 public void onStart(ITestContext context) {33 }34 public void onFinish(ITestContext context) {35 }...

Full Screen

Full Screen

Source:CustomListener1.java Github

copy

Full Screen

...8import org.testng.ITestResult;9public class CustomListener1 implements IInvokedMethodListener {10 @Override11 public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { //it will run before every method (all method present in the class which every class going to implement it)12 System.out.println("beforeInvocation: " + testResult.getTestClass().getName() + 13 " => " + method.getTestMethod().getMethodName() );14 }15 @Override16 public void afterInvocation(IInvokedMethod method, ITestResult testResult) {17 System.out.println("AfterInvocation: " + testResult.getTestClass().getName() + 18 " => " + method.getTestMethod().getMethodName() );19 }20}...

Full Screen

Full Screen

getTestClass

Using AI Code Generation

copy

Full Screen

1import org.testng.ITestResult;2import org.testng.TestListenerAdapter;3import org.testng.annotations.Test;4public class TestClassListener extends TestListenerAdapter {5 public void onTestStart(ITestResult tr) {6 System.out.println("Test started");7 }8 public void onTestSuccess(ITestResult tr) {9 System.out.println("Test Success");10 }11 public void onTestFailure(ITestResult tr) {12 System.out.println("Test Failure");13 }14 public void onTestSkipped(ITestResult tr) {15 System.out.println("Test Skipped");16 }17 public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {18 System.out.println("Test Failed But Within Success Percentage");19 }20 public void onStart(ITestContext testContext) {21 System.out.println("Test Started");22 }23 public void onFinish(ITestContext testContext) {24 System.out.println("Test Finished");25 }26}27import org.testng.annotations.Test;28public class TestClass {29 public void testMethod() {30 System.out.println("Test Method");31 }32}

Full Screen

Full Screen

getTestClass

Using AI Code Generation

copy

Full Screen

1import org.testng.ITestResult2import org.testng.ITestContext3import org.testng.ITestNGMethod4import org.testng.ITestClass5import org.testng.annotations.Test6import org.testng.annotations.DataProvider7public class TestNGTest {8 @Test(dataProvider = "testData")9 public void testMethod1(String param1, String param2) {10 println "TestNGTest.testMethod1() param1 = ${param1} param2 = ${param2}"11 }12 @DataProvider(name = "testData")13 public Object[][] testData() {14 }15 public void testMethod2() {16 println "TestNGTest.testMethod2()"17 }18}19import org.testng.ITestResult20import org.testng.ITestContext21import org.testng.ITestNGMethod22import org.testng.ITestClass23import org.testng.annotations.Test24import org.testng.annotations.DataProvider25public class TestNGTest {26 @Test(dataProvider = "testData")27 public void testMethod1(String param1, String param2) {28 println "TestNGTest.testMethod1() param1 = ${param1} param2 = ${param2}"29 }30 @DataProvider(name = "testData")31 public Object[][] testData() {32 }33 public void testMethod2() {34 println "TestNGTest.testMethod2()"35 }36}37import org.testng.ITestResult38import org.testng.ITestContext39import org.testng.ITestNGMethod40import org.testng.ITestClass41import org.testng.annotations.Test42import org.testng.annotations.DataProvider43public class TestNGTest {44 @Test(dataProvider = "testData")45 public void testMethod1(String param1, String param2) {46 println "TestNGTest.testMethod1() param1 = ${param1} param2 = ${param2}"47 }48 @DataProvider(name = "testData")49 public Object[][] testData() {50 }

Full Screen

Full Screen

TestNG tutorial

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.

Chapters

  1. JUnit 5 vs. TestNG: Compare and explore the core differences between JUnit 5 and TestNG from the Selenium WebDriver viewpoint.
  2. Installing TestNG in Eclipse: Start installing the TestNG Plugin and learn how to set up TestNG in Eclipse to begin constructing a framework for your test project.
  3. Create TestNG Project in Eclipse: Get started with creating a TestNG project and write your first TestNG test script.
  4. Automation using TestNG: Dive into how to install TestNG in this Selenium TestNG tutorial, the fundamentals of developing an automation script for Selenium automation testing.
  5. Parallel Test Execution in TestNG: Here are some essential elements of parallel testing with TestNG in this Selenium TestNG tutorial.
  6. Creating TestNG XML File: Here is a step-by-step tutorial on creating a TestNG XML file to learn why and how it is created and discover how to run the TestNG XML file being executed in parallel.
  7. Automation with Selenium, Cucumber & TestNG: Explore for an in-depth tutorial on automation using Selenium, Cucumber, and TestNG, as TestNG offers simpler settings and more features.
  8. JUnit Selenium Tests using TestNG: Start running your regular and parallel tests by looking at how to run test cases in Selenium using JUnit and TestNG without having to rewrite the tests.
  9. Group Test Cases in TestNG: Along with the explanation and demonstration using relevant TestNG group examples, learn how to group test cases in TestNG.
  10. Prioritizing Tests in TestNG: Get started with how to prioritize test cases in TestNG for Selenium automation testing.
  11. Assertions in TestNG: Examine what TestNG assertions are, the various types of TestNG assertions, and situations that relate to Selenium automated testing.
  12. DataProviders in TestNG: Deep dive into learning more about TestNG's DataProvider and how to effectively use it in our test scripts for Selenium test automation.
  13. Parameterization in TestNG: Here are the several parameterization strategies used in TestNG tests and how to apply them in Selenium automation scripts.
  14. TestNG Listeners in Selenium WebDriver: Understand the various TestNG listeners to utilize them effectively for your next plan when working with TestNG and Selenium automation.
  15. TestNG Annotations: Learn more about the execution order and annotation attributes, and refer to the prerequisites required to set up TestNG.
  16. TestNG Reporter Log in Selenium: Find out how to use the TestNG Reporter Log and learn how to eliminate the need for external software with TestNG Reporter Class to boost productivity.
  17. TestNG Reports in Jenkins: Discover how to generate TestNG reports in Jenkins if you want to know how to create, install, and share TestNG reports in Jenkins.

Certification

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.

YouTube

Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful