Best Testng code snippet using org.testng.collections.Lists.newArrayList
Source: Model.java
...20 private Map<ITestResult, String> m_testResultMap = Maps.newHashMap();21 private Map<ISuite, ResultsByClass> m_failedResultsByClass = Maps.newHashMap();22 private Map<ISuite, ResultsByClass> m_skippedResultsByClass = Maps.newHashMap();23 private Map<ISuite, ResultsByClass> m_passedResultsByClass = Maps.newHashMap();24 private List<ITestResult> m_allFailedResults = Lists.newArrayList();25 // Each suite is mapped to failed.png, skipped.png or nothing (which means passed.png)26 private Map<String, String> m_statusBySuiteName = Maps.newHashMap();27 private SetMultiMap<String, String> m_groupsBySuiteName = Maps.newSetMultiMap();28 private SetMultiMap<String, String> m_methodsByGroup = Maps.newSetMultiMap();29 public Model(List<ISuite> suites) {30 m_suites = suites;31 init();32 }33 public List<ISuite> getSuites() {34 return m_suites;35 }36 private void init() {37 int testCounter = 0;38 for (ISuite suite : m_suites) {39 List<ITestResult> passed = Lists.newArrayList();40 List<ITestResult> failed = Lists.newArrayList();41 List<ITestResult> skipped = Lists.newArrayList();42 Map<String, ISuiteResult> suiteResults = suite.getResults();43 for (ISuiteResult sr : suiteResults.values()) {44 ITestContext context = sr.getTestContext();45 m_testTags.put(context.getName(), "test-" + testCounter++);46 failed.addAll(context.getFailedTests().getAllResults());47 skipped.addAll(context.getSkippedTests().getAllResults());48 passed.addAll(context.getPassedTests().getAllResults());49 IResultMap[] map =50 new IResultMap[] {51 context.getFailedTests(), context.getSkippedTests(), context.getPassedTests()52 };53 for (IResultMap m : map) {54 for (ITestResult tr : m.getAllResults()) {55 m_testResultMap.put(tr, getTestResultName(tr));56 }57 }58 }59 // Process them in the order passed, skipped and failed, so that the failed60 // icon overrides all the others and the skipped icon overrides passed.61 // Passed62 {63 ResultsByClass rbc = new ResultsByClass();64 for (ITestResult tr : passed) {65 rbc.addResult(tr.getTestClass().getRealClass(), tr);66 updateGroups(suite, tr);67 }68 m_passedResultsByClass.put(suite, rbc);69 }70 // Skipped71 {72 ResultsByClass rbc = new ResultsByClass();73 for (ITestResult tr : skipped) {74 m_statusBySuiteName.put(suite.getName(), "skipped");75 rbc.addResult(tr.getTestClass().getRealClass(), tr);76 updateGroups(suite, tr);77 }78 m_skippedResultsByClass.put(suite, rbc);79 }80 // Failed81 {82 ResultsByClass rbc = new ResultsByClass();83 for (ITestResult tr : failed) {84 m_statusBySuiteName.put(suite.getName(), "failed");85 rbc.addResult(tr.getTestClass().getRealClass(), tr);86 m_allFailedResults.add(tr);87 updateGroups(suite, tr);88 }89 m_failedResultsByClass.put(suite, rbc);90 }91 m_model.putAll(suite, failed);92 m_model.putAll(suite, skipped);93 m_model.putAll(suite, passed);94 }95 }96 private void updateGroups(ISuite suite, ITestResult tr) {97 String[] groups = tr.getMethod().getGroups();98 m_groupsBySuiteName.putAll(suite.getName(), Arrays.asList(groups));99 for (String group : groups) {100 m_methodsByGroup.put(group, tr.getMethod().getMethodName());101 }102 }103 public ResultsByClass getFailedResultsByClass(ISuite suite) {104 return m_failedResultsByClass.get(suite);105 }106 public ResultsByClass getSkippedResultsByClass(ISuite suite) {107 return m_skippedResultsByClass.get(suite);108 }109 public ResultsByClass getPassedResultsByClass(ISuite suite) {110 return m_passedResultsByClass.get(suite);111 }112 public String getTag(ITestResult tr) {113 return m_testResultMap.get(tr);114 }115 public List<ITestResult> getTestResults(ISuite suite) {116 return m_model.get(suite);117 }118 public static String getTestResultName(ITestResult tr) {119 StringBuilder result = new StringBuilder(tr.getMethod().getMethodName());120 Object[] parameters = tr.getParameters();121 if (parameters.length > 0) {122 result.append("(");123 StringBuilder p = new StringBuilder();124 for (int i = 0; i < parameters.length; i++) {125 if (i > 0) p.append(", ");126 p.append(Utils.toString(parameters[i]));127 }128 if (p.length() > 100) {129 String s = p.toString().substring(0, 100);130 s = s + "...";131 result.append(s);132 } else {133 result.append(p.toString());134 }135 result.append(")");136 }137 return result.toString();138 }139 public List<ITestResult> getAllFailedResults() {140 return m_allFailedResults;141 }142 public static String getImage(String tagClass) {143 return tagClass + ".png";144 }145 public String getStatusForSuite(String suiteName) {146 String result = m_statusBySuiteName.get(suiteName);147 return result != null ? result : "passed";148 }149 public List<String> getGroups(String name) {150 List<String> result = Lists.newArrayList(m_groupsBySuiteName.get(name));151 Collections.sort(result);152 return result;153 }154 public List<String> getMethodsInGroup(String groupName) {155 List<String> result = Lists.newArrayList(m_methodsByGroup.get(groupName));156 Collections.sort(result);157 return result;158 }159 public List<ITestResult> getAllTestResults(ISuite suite) {160 return getAllTestResults(suite, true /* tests only */);161 }162 public List<ITestResult> getAllTestResults(ISuite suite, boolean testsOnly) {163 List<ITestResult> result = Lists.newArrayList();164 for (ISuiteResult sr : suite.getResults().values()) {165 result.addAll(sr.getTestContext().getPassedTests().getAllResults());166 result.addAll(sr.getTestContext().getFailedTests().getAllResults());167 result.addAll(sr.getTestContext().getSkippedTests().getAllResults());168 if (!testsOnly) {169 result.addAll(sr.getTestContext().getPassedConfigurations().getAllResults());170 result.addAll(sr.getTestContext().getFailedConfigurations().getAllResults());171 result.addAll(sr.getTestContext().getSkippedConfigurations().getAllResults());172 }173 }174 return result;175 }176}...
...16import org.testng.collections.Maps;17public class AlphabeticalMethodInterceptor implements IMethodInterceptor {18 @Override19 public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {20 List<Object> instanceList = Lists.newArrayList();21 Map<Object, List<IMethodInstance>> map = Maps.newHashMap();22 for (IMethodInstance mi : methods) {23 Object instance = mi.getInstance();24 if (!instanceList.contains(instance)) {25 instanceList.add(instance);26 }27 List<IMethodInstance> l = map.computeIfAbsent(instance, k -> Lists.newArrayList());28 l.add(mi);29 }30 Comparator<IMethodInstance> comparator = Comparator.comparing(o -> o.getMethod().getMethodName());31 List<IMethodInstance> result = Lists.newArrayList();32 for (Object instance : instanceList) {33 List<IMethodInstance> methodlist = map.get(instance);34 IMethodInstance[] array = methodlist.toArray(new IMethodInstance[methodlist.size()]);35 Arrays.sort(array, comparator);36 result.addAll(Arrays.asList(array));37 }38 System.out.println("AlphabeticalMethodInterceptor: "+result);39 return result;40 }41}...
Source: XmlRun.java
...21 xsb.pop("run");22 }23 return xsb.toXML();24 }25 private List<String> m_excludes = Lists.newArrayList();26 public List<String> getExcludes() {27 return m_excludes;28 }29 @OnElement(tag = "exclude", attributes = "name")30 public void onExclude(String name) {31 m_excludes.add(name);32 }33 private List<String> m_includes = Lists.newArrayList();34 public List<String> getIncludes() {35 return m_includes;36 }37 @OnElement(tag = "include", attributes = "name")38 public void onInclude(String name) {39 m_includes.add(name);40 }41}...
...10 return groupMethodsByInstance(methods);11 }12 /** The default method interceptor which sorts methods by instances (i.e. by class). */13 private List<IMethodInstance> groupMethodsByInstance(List<IMethodInstance> methods) {14 List<Object> instanceList = Lists.newArrayList();15 Map<Object, List<IMethodInstance>> map = Maps.newLinkedHashMap();16 for (IMethodInstance mi : methods) {17 Object instance = mi.getInstance();18 if (!instanceList.contains(instance)) {19 instanceList.add(instance);20 }21 List<IMethodInstance> l = map.computeIfAbsent(instance, k -> Lists.newArrayList());22 l.add(mi);23 }24 List<IMethodInstance> result = Lists.newArrayList();25 for (Object instance : instanceList) {26 result.addAll(map.get(instance));27 }28 return result;29 }30}
Source: BaseSample.java
...4import org.testng.collections.Lists;5import org.testng.collections.Maps;6import java.util.List;7public class BaseSample {8 public static List<String> m_methods = Lists.newArrayList();9 protected void add(String m) {10 String s = m;11// System.out.println("BaseSample recording " + this + " " + s);12 synchronized(m_methods) {13 m_methods.add(s);14 }15 }16 @BeforeClass17 public void bc() {18 m_methods = Lists.newArrayList();19 }20 @Test21 public void f1() { add("f1"); }22 @Test23 public void f2() { add("f2"); }24 @Test25 public void f3() { add("f3"); }26 @Test27 public void f4() { add("f4"); }28 @Test29 public void f5() { add("f5"); }30 @Test31 public void f6() { add("f6"); }32 @Test...
How to pass any UUID in a function in unit testing in mockito?
Eclipse: Blacklist types from code completion
integration testing with JPA and Spring
How to override TestNG parameters from the calling program
Is it possible to use the TestNG plugin with Netbeans 6.8?
TestNG Using @Factory and @DataProvider
Run TestNG suite from maven getting error:maven-surefire-plugin:test failed: testSuiteXmlFiles0 has null value
Is there anyway to find all tests that are disabled in a TestNG suite?
How to create 'testng.xml' using Eclipse
Running specific tests with TestNG programmatically
For any other type you can use any()
. This will match an argument of given type:
any(UUID.class)
You can find more information in the documentation.
Check out the latest blogs from LambdaTest on this topic:
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Locators Tutorial.
Selenium automation is not just about navigating and clicking through the links. Yes, you can use it to switch tabs, find broken links, or even drag and drop directly. But there is a lot more to it. An essential aspect of test automation is passing values to the text fields. In this article, we’ll delve into how Selenium helps us pass values to text fields using the Selenium sendKeys() method. Let us start on our journey to learn how to sendkeys in Selenium.
PDF documents are small-sized, highly secure files. Almost all businesses use PDFs for processing their files. The reason being a distinguishing feature of maintaining format regardless of the tool used to access PDF files. It’s no surprise that all our invoices, official documents, contractual documents, boarding pass, bank statements, etc. are usually in PDF format.
Being an automation tester, we do realize that in a release cycle, time is always of the essence.! Selenium test automation helps to save us a considerable amount of time in our test cycles. However, it is pivotal to note the way through which you are executing your Selenium testing scripts. Which frameworks are you using? Are you doing it with an in-house infrastructure or with an online Selenium Grid? Are you making use of build automation tools or not?!
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Jenkins 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.
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!!