Best Testng code snippet using org.testng.Interface ITestContext.getCurrentXmlTest
Source:Config.java
...59 60 @BeforeTest(alwaysRun = true)61 public void fetchSuiteConfiguration(ITestContext testContext)throws IOException 62 {63 testUrl = testContext.getCurrentXmlTest().getParameter("url");64 System.out.println(testUrl);65 seleniumHub = testContext.getCurrentXmlTest().getParameter("selenium.host");66 seleniumHubPort = testContext.getCurrentXmlTest().getParameter("selenium.port");67 targetBrowser = testContext.getCurrentXmlTest().getParameter("browser");68 }69 70 /**71 * Make Browser Launch from This method.72 * 73 * @param method74 * @throws MalformedURLException75 */76 77 @BeforeMethod(alwaysRun = true)78 public void setUp(Method method) throws MalformedURLException 79 {80 currentTest = method.getName(); // get Name of current test.81 URL remote_grid = new URL("http://" + seleniumHub + ":" + seleniumHubPort + "/wd/hub");...
Source:TestListener.java
...36 // TODO Auto-generated catch block37 e1.printStackTrace();38 }39 Map<String, String> params = new HashMap<String, String>();40 params = iTestResult.getTestContext().getCurrentXmlTest().getAllParameters();41 String imagePath = "Screenshots" + File.separator + params.get("platformName")42 + "_" + params.get("deviceName") + File.separator + base.getDateTime() + File.separator43 + iTestResult.getTestClass().getRealClass().getSimpleName() + File.separator + iTestResult.getName() + ".png";44 String completeImagePath = System.getProperty("user.dir") + File.separator + imagePath;45 try {46 FileUtils.copyFile(file, new File(imagePath));47 Reporter.log("This is the sample screenshot");48 Reporter.log("<a href='"+ completeImagePath + "'> <img src='"+ completeImagePath + "' height='400' width='400'/> </a>");49 ExtentReport.getTest().log(Status.PASS, "Test Passed");50 } catch (IOException e) {51 // TODO Auto-generated catch block52 e.printStackTrace();53 }54 }55 @Override56 public void onTestFailure(ITestResult iTestResult) {57 if (iTestResult.getThrowable() != null) {58 StringWriter sw = new StringWriter();59 PrintWriter pw = new PrintWriter(sw);60 iTestResult.getThrowable();61 }62 byte[] encoded = null;63 Map<String, String> params = new HashMap<String, String>();64 params = iTestResult.getTestContext().getCurrentXmlTest().getAllParameters();65 String imagePath = "Screenshots" + File.separator + params.get("platformName")66 + "_" + params.get("deviceName") + File.separator + base.getDateTime() + File.separator67 + iTestResult.getTestClass().getRealClass().getSimpleName() + File.separator + iTestResult.getName() + ".png";68 String completeImagePath = System.getProperty("user.dir") + File.separator + imagePath;69 ExtentReport.getTest().fail("Test Failed",70 MediaEntityBuilder.createScreenCaptureFromPath(completeImagePath).build());71 ExtentReport.getTest().fail("Test Failed",72 MediaEntityBuilder.createScreenCaptureFromBase64String(new String(encoded, StandardCharsets.US_ASCII)).build());73 ExtentReport.getTest().fail(iTestResult.getThrowable());74 }75 @Override76 public void onTestSkipped(ITestResult iTestResult) {77 ExtentReport.getTest().log(Status.SKIP, "Test Skipped");78 }...
Source:BaseParpareTestPc.java
...29 seleniumUtil = new SeleniumUtil();30 //è¿éå¾å°contextå¼31 this.testContext = context;32 //ä»testng.xmlæ件ä¸è·åæµè§å¨çå±æ§å¼33 String browserName = context.getCurrentXmlTest().getParameter("browserName");34 timeOut = Integer.valueOf(context.getCurrentXmlTest().getParameter("timeOut"));35 testPcUrl = context.getCurrentXmlTest().getParameter("testPcUrl");36 method = "/user/AdminLoginInterface.htm";37 38 try{39 //å¯å¨æµè§å¨ï¼æå¼æµè§å¨ï¼è¾å
¥æµè¯å°åï¼å¹¶æå¼400*700ççªå£40// seleniumUtil.launchBrowserPc(browserName, context, testwwwUrl, timeOut);41 //å¯å¨æµè§å¨ï¼æå¼æµè§å¨ï¼è¾å
¥æµè¯å°åï¼å¹¶æ大åçªå£42 seleniumUtil.launchBrowserPc(browserName, context, testPcUrl, method,timeOut);43 }catch(Exception e){44 logger.error("æµè§å¨ä¸è½æ£å¸¸å·¥ä½ï¼è¯·æ£æ¥æ¯ä¸æ¯è¢«æå¨å
³éæè
å
¶ä»åå ",e);45 }46 //设置ä¸ä¸ªtestngä¸ä¸æå±æ§ï¼å°driveråèµ·æ¥ï¼ä¹åå¯ä»¥ä½¿ç¨contextéæ¶åå°ï¼ä¸»è¦æ¯æä¾arrow è·ådriver对象使ç¨çï¼å 为arrowæªå¾æ¹æ³éè¦ä¸ä¸ªdriver对象47 testContext.setAttribute("SELENIUM_DRIVER", seleniumUtil.driver);48 }49 ...
Source:ITestListenerImpl.java
...44// e1.printStackTrace();45//46// }47// Map<String, String> params = new HashMap<String, String>();48// params = iTestResult.getTestContext().getCurrentXmlTest().getAllParameters();49//50// String imagePath = "Screenshots" + iTestResult.getTestClass().getRealClass().getSimpleName() + File.separator + iTestResult.getName() + ".png";51//52//53//54// String completeImagePath = System.getProperty("user.dir") + File.separator + imagePath;55//56//57//58//59// try {60// FileUtils.copyFile(file, new File(imagePath));61// Reporter.log("This is the sample screenshot");62// Reporter.log("<a href='"+ completeImagePath + "'> <img src='"+ completeImagePath + "' height='400' width='400'/> </a>");63// ExtentReportListener.getTest().log(Status.PASS, "Test Passed");64// } catch (IOException e) {65// // TODO Auto-generated catch block66// e.printStackTrace();67// }68 }69 @Override70 public void onTestFailure(ITestResult iTestResult) {71// if (iTestResult.getThrowable() != null) {72// StringWriter sw = new StringWriter();73// PrintWriter pw = new PrintWriter(sw);74// iTestResult.getThrowable();75// }76// byte[] encoded = null;77// Map<String, String> params = new HashMap<String, String>();78// params = iTestResult.getTestContext().getCurrentXmlTest().getAllParameters();79//80// String imagePath = "Screenshots" + File.separator + params.get("platformName")81// + "_" + params.get("deviceName") + File.separator + base.getDateTime() + File.separator82// + iTestResult.getTestClass().getRealClass().getSimpleName() + File.separator + iTestResult.getName() + ".png";83//84//85//86// String completeImagePath = System.getProperty("user.dir") + File.separator + imagePath;87//88//89// ExtentReportListener.getTest().fail("Test Failed",90// MediaEntityBuilder.createScreenCaptureFromPath(completeImagePath).build());91// ExtentReportListener.getTest().fail("Test Failed",92// MediaEntityBuilder.createScreenCaptureFromBase64String(new String(encoded, StandardCharsets.US_ASCII)).build());...
Source:BaseParpareTestWeiXin.java
...29 seleniumUtil = new SeleniumUtil();30 //è¿éå¾å°contextå¼31 this.testContext = context;32 //ä»testng.xmlæ件ä¸è·åæµè§å¨çå±æ§å¼33 String browserName = context.getCurrentXmlTest().getParameter("browserName");34 timeOut = Integer.valueOf(context.getCurrentXmlTest().getParameter("timeOut"));35 testWXUrl = context.getCurrentXmlTest().getParameter("testWXUrl");36 method = "/user/AdminLoginInterface.htm";37 38 try{39 //å¯å¨æµè§å¨ï¼æå¼æµè§å¨ï¼è¾å
¥æµè¯å°åï¼å¹¶æå¼å¾®ä¿¡çªå£40 seleniumUtil.launchBrowserWeiXin(browserName, context, testWXUrl,method, timeOut);41 }catch(Exception e){42 logger.error("æµè§å¨ä¸è½æ£å¸¸å·¥ä½ï¼è¯·æ£æ¥æ¯ä¸æ¯è¢«æå¨å
³éæè
å
¶ä»åå ",e);43 }44 //设置ä¸ä¸ªtestngä¸ä¸æå±æ§ï¼å°driveråèµ·æ¥ï¼ä¹åå¯ä»¥ä½¿ç¨contextéæ¶åå°ï¼ä¸»è¦æ¯æä¾arrow è·ådriver对象使ç¨çï¼å 为arrowæªå¾æ¹æ³éè¦ä¸ä¸ªdriver对象45 testContext.setAttribute("SELENIUM_DRIVER", seleniumUtil.driver);46 }47 48 @AfterClass49 public void endTest(){...
Source:MyTestNGAnnotationListener.java
...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 }...
Source:CommonBase.java
...13 private Map<String, String> allParams = new HashMap<>();14 private ThreadLocal<Map<String, String>> params = ThreadLocal.withInitial(() -> new HashMap<>());15 @BeforeClass16 protected void beforeClassGetAllParam(ITestContext context) {17 allParams = context.getCurrentXmlTest().getAllParameters();18 }19 public Map<String, String> getAllParams() {20 return allParams;21 }22 //è¿æ¯æ¯ä¸ªç¨ä¾ç¨æ¥è·ååæ°çæ¹æ³23 @BeforeMethod24 protected void beforeMethodGetParam(Method method, Object[] objects) {25 Map<String, String> temp = new HashMap<>();26 Parameter[] parameters = method.getParameters();27 if (parameters == null) {28 return;29 } else {30 for (int i = 0; i < parameters.length; i++) {31 boolean ignore = false;...
Source:BaseClass.java
...10 public static Xls_Reader xls = new Xls_Reader(Constants.XLS_FILE_PATH);11 public static void defineExtent(ITestContext iTestContext) {12 // public ExtentReports rep=null;13 ExtentReports rep = ExtentManager.getInstance();14 ExtentTest parentTest = rep.startTest(iTestContext.getCurrentXmlTest().getClass().getSimpleName());15 parentTest.log(LogStatus.INFO, "Total Number of TestCases : " + iTestContext.getAllTestMethods().length);16 parentTest.assignCategory("Functional Testing");17 }18}...
getCurrentXmlTest
Using AI Code Generation
1package com.javacodegeeks.testng.maven;2import org.testng.ITestContext;3import org.testng.annotations.Test;4public class TestNGMavenExample {5 public void testMethodOne(ITestContext context) {6 System.out.println("TestNG Maven Example");7 System.out.println(context.getCurrentXmlTest().getParameter("browser"));8 }9}10public interface ITestContext {
getCurrentXmlTest
Using AI Code Generation
1public void testMethod(ITestContext context) {2 System.out.println(context.getCurrentXmlTest().getName());3}4public void testMethod(ISuite suite) {5 System.out.println(suite.getCurrentXmlTest().getName());6}7public void testMethod() {8 System.out.println(TestUtils.getCurrentTestName());9}
getCurrentXmlTest
Using AI Code Generation
1public void test1(ITestContext context) {2 String testName = context.getCurrentXmlTest().getName();3 System.out.println("Test name is " + testName);4}5public void test2(ITestContext context) {6 String testName = context.getCurrentXmlTest().getName();7 System.out.println("Test name is " + testName);8}9public void test3(ITestContext context) {10 String testName = context.getCurrentXmlTest().getName();11 System.out.println("Test name is " + testName);12}13public void test4(ITestContext context) {14 String testName = context.getCurrentXmlTest().getName();15 System.out.println("Test name is " + testName);16}17public void test5(ITestContext context) {18 String testName = context.getCurrentXmlTest().getName();19 System.out.println("Test name is " + testName);20}21public void test6(ITestContext context) {22 String testName = context.getCurrentXmlTest().getName();23 System.out.println("Test name is " + testName);24}25public void test7(ITestContext context) {26 String testName = context.getCurrentXmlTest().getName();27 System.out.println("Test name is " + testName);28}29public void test8(ITestContext context) {30 String testName = context.getCurrentXmlTest().getName();31 System.out.println("Test name is " + testName);32}33public void test9(ITestContext context) {34 String testName = context.getCurrentXmlTest().getName();35 System.out.println("Test name is " + testName);36}37public void test10(ITestContext context) {38 String testName = context.getCurrentXmlTest().getName();39 System.out.println("Test name
getCurrentXmlTest
Using AI Code Generation
1public void testMethod(ITestContext context) {2 XmlTest currentXmlTest = context.getCurrentXmlTest();3 System.out.println(currentXmlTest.getParameter("param1"));4}5public void testMethod(ITestContext context) {6 ISuite currentSuite = context.getSuite();7 System.out.println(currentSuite.getName());8}9public void testMethod(ITestContext context) {10 String testName = context.getTestName();11 System.out.println(testName);12}13public void testMethod(ITestContext context) {14 ISuite currentSuite = context.getSuite();15 System.out.println(currentSuite.getName());16}17public void testMethod(ITestContext context) {18 ISuite currentSuite = context.getSuite();19 System.out.println(currentSuite.getName());20}21public void testMethod(ITestContext context) {22 ISuite currentSuite = context.getSuite();23 System.out.println(currentSuite.getName());24}25public void testMethod(ITestContext context) {26 ISuite currentSuite = context.getSuite();27 System.out.println(currentSuite.getName());28}29public void testMethod(ITestContext context) {30 ISuite currentSuite = context.getSuite();31 System.out.println(currentSuite.getName());32}33public void testMethod(ITestContext context) {34 ISuite currentSuite = context.getSuite();35 System.out.println(currentSuite.getName());36}37public void testMethod(ITestContext context) {38 ISuite currentSuite = context.getSuite();39 System.out.println(currentSuite.getName());40}41public void testMethod(ITestContext context) {42 ISuite currentSuite = context.getSuite();
getCurrentXmlTest
Using AI Code Generation
1String testName = testContext.getCurrentXmlTest().getName();2System.out.println("Test Name: " + testName);3Map<String, String> testParameters = testContext.getCurrentXmlTest().getAllParameters();4System.out.println("Test Parameters: " + testParameters);5String[] testGroups = testContext.getCurrentXmlTest().getIncludedGroups();6System.out.println("Test Groups: " + testGroups);7List<XmlClass> testMethods = testContext.getCurrentXmlTest().getXmlClasses();8System.out.println("Test Methods: " + testMethods);9List<XmlSuite> testSuites = testContext.getCurrentXmlTest().getSuites();10System.out.println("Test Suites: " + testSuites);11String testSuiteName = testContext.getCurrentXmlTest().getSuite().getName();12System.out.println("Test Suite Name: " + testSuiteName);13Map<String, String> testSuiteParameters = testContext.getCurrentXmlTest().getSuite().getAllParameters();14System.out.println("Test Suite Parameters: " + testSuiteParameters);15List<XmlClass> testSuiteListeners = testContext.getCurrentXmlTest().getSuite().getListeners();16System.out.println("Test Suite Listeners: " + testSuiteListeners);17int testSuiteThreadCount = testContext.getCurrentXmlTest().getSuite().getThreadCount();18System.out.println("Test Suite Thread Count: " + testSuiteThreadCount);19String testSuiteParallelMode = testContext.getCurrentXmlTest().getSuite().getParallel();20System.out.println("Test Suite Parallel Mode: " + testSuiteParallelMode);21String testSuiteOutputDirectory = testContext.getCurrentXmlTest().getSuite().getOutputDirectory();22System.out.println("Test Suite Output Directory: " + testSuiteOutputDirectory);23String[] testSuiteGroups = testContext.getCurrentXmlTest().getSuite().getIncludedGroups();24System.out.println("Test Suite Groups: " + testSuiteGroups);25String testSuiteXmlFile = testContext.getCurrentXmlTest().getSuite().getFileName
getCurrentXmlTest
Using AI Code Generation
1public void testOne(ITestContext context) {2 String testName = context.getCurrentXmlTest().getName();3 System.out.println("Test name: " + testName);4}5public void testOne(ITestContext context) {6 String suiteName = context.getSuite().getName();7 System.out.println("Suite name: " + suiteName);8}9public void testOne(ITestContext context) {10 String contextName = context.getName();11 System.out.println("Context name: " + contextName);12}13public void testOne(ITestContext context) {14 Date startDate = context.getStartDate();15 System.out.println("Start date: " + startDate);16}17public void testOne(ITestContext context) {18 Date endDate = context.getEndDate();19 System.out.println("End date: " + endDate);20}21public void testOne(ITestContext context) {22 IResultMap passedTests = context.getPassedTests();23 System.out.println("Passed tests: " + passedTests);24}25public void testOne(ITestContext context) {26 IResultMap failedTests = context.getFailedTests();27 System.out.println("Failed tests: " + failedTests);28}
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!!