java


How to get #Test methods from the test classes, when running TestNG programmatically


I'm creating an application to programmatically execute our java webdriver selenium scripts. We use TestNG.
I'm able to launch the classes with a virtual XML file. This works:
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("browser", "Firefox");
suite.setParameters(parameters);
classes.add(new XmlClass("package.classname"));
test.setXmlClasses(classes) ;
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
Since the above method uses the package.classname to execute the class, I need a way to get all our classes; both their package and their class name (We have hundreds of packages).
I use this code to grab all the package.classnames. In simply scans the /bin/ directory of the project, converting the folder structure into package names and removes the .class extension from the class file. This works, too:
//scans the /bin directory of project to grab all packages/classes
String binPath = "V:\\Selenium\\Scripts\\bin\\";
File dir = new File(binPath);
List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
List<String> classNames = new ArrayList<String>(300);
for (File f : files) {
//instead of "V:\Selenium\Scripts\bin\package\classname.class"
//I just end up with "package.classname"
classNames.add(extractClassNameFromPath(f.getPath()));
}
So at this point, I'm able to get a String list of all my class names, and I can put any of them into the classes.add(new XmlClass("package.classname")); line, allowing me run any of our test classes programmatically. Great!
With all that said, here is the problem I'm facing now:
The classes have different #Test methods. By launching just the class, all of the #Test methods will be executed. I want to be able to extract the available #Test methods in that class and present them to the user so they can choose which ones they want.
I have no idea how to extract the #Test methods from our test classes.
You can use reflections library and its MethodAnnotationsScanner:
Reflections reflections = new Reflections("my.package");
Set<Method> resources =
reflections.getMethodsAnnotatedWith(org.testng.Test.class);

Related Links

RPN Calculator Using Stacks
3D Fourier Transform using JTransforms in Android Studio
Converting RGB byte array to BufferedImage
Creating a term-by-file document matrix from a set of documents
Android POST request with JSON
Headless Selenium test which fills out a form?
How can I extract url from json data with escaped “\” in java
How to make the following sliders for a RGB program
Do/while loop not returning to the top?
How to split array in Rx filter
Dreaded 500 error trying to get MySql info with hibernate and poster into thymeleaf :(
I can view leaderboard but i cannot add any score in it
I have a file that i am trying to read through and get the information out of, but i keep getting this
detecting clicks not working
android Jackson #JsonIgnore property
p:ajax refreshes whole page - JSF - primefaces [duplicate]

Categories

HOME
java
ibm-bluemix
cloud
oracle11g
single-sign-on
reserved
hp-exstream
hashmap
onedrive
syntax
grep
framework7
google-docs
gis
paw-app
amortized-analysis
twitter-bootstrap-4
survey
icloud
callback
retrofit
ojdbc
swagger-ui
upload
facebook-messenger-bot
session-variables
gorm
vifm
hex-editors
percona
pc
invantive-sql
normalizr
google-cloud-speech
conemu
riot.js
accessor
pass-by-reference
zurb-foundation-6
tooltipster
assistant
log4js-node
windows-server-2000
google-cloud-nl
media-queries
binary-data
bosh
column-family
commit
vsts-build-task
mapbox-gl
mmenu
jquery-bootgrid
vapor
ssjs
opshub
elasticsearch-plugin
eclipse-scout
texmaker
no-www
angular2-meteor
cubic-spline
avconv
revapi
python-c-api
python-webbrowser
integrity
git-diff
carrot
forever
windows-mobile-6.5
quartz-composer
topbeat
instant
removechild
azure-virtual-network
asp.net-4.5
sdf
wapiti
freelancer.com-api
kendonumerictextbox
tcpserver
t4mvc
citrus-pay
purge
emailrelay
mmc
c++03
cdt
google-reader
heisenbug
doskey
rtmfp
oracle-warehouse-builder
imdbpy
mcts
flexmojos
oam
access-rights
isnullorempty
postgresql-performance
dotnethighcharts
custom-backend
coercion
floating
cxxtest
coda-slider
gin
nsviewanimation
rescale
ctp4
sector
putchar
ubuntu-9.04
mdac

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App