I have a query regarding testng module. My requirement is to invoke different classes based on the parameter provided in the testng.xml
Testng.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name="selenium.test" value="Web" />
<parameter name="selenium.testtype" value="Sanity" />
<parameter name="selenium.env" value="UAT" />
<parameter name="selenium.browser" value="IE" />
<parameter name="selenium.pbrowser" value="Mozilla" />
<test name =>
<classes>
<class name="test.DriverTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
1) In the above code snippet, if my test type is sanity, I want to invoke Sanity.java file where as if other test type is regression, I want to invoke Regression.java
2) Can we pass parameter value as input to other XML tags in testng.xml Example: If then i want this value to feeded dyanamically based on parameter value
3) Also from this parameter input how to invoke parallel browser testing?
TestNG provides you with a concept called groups which should solve this for you. For every
@Test
method, add thegroups
attribute to it and then, in your suite xml file, you can pick and choose whichever groups you would like to run. Refer the official documentation here for more information.Not sure what you are after here, but the parameters in a testng suite xml file are always static in nature. They cannot be changed unless and until you alter the suite xml file (or) you make use of a TestNG listener such as
org.testng.IAlterSuiteListener
wherein you fudge the parameters based on some thing else. Here's a sample that shows what I am talking aboutA test class which expects a parameter to be injected
This listener looks for JVM arguments that begin with
jvm_
and whose name matches with the parameters in the suite xml and if found, it updates the parameter value with what was sent in the JVM argument. So using this way, you can literally achieve what you are after.Here's how the suite xml looks like
As you can see, I have defined a value of Unknown for the parameter name
So if you just right click on the suite xml and run it from your favorite IDE, the output would look like below:
But if you run the same suite, by passing in the JVM argument
-Djvm_name
, then the output would be different. Here's a run output wherein I am passing in the value as Rambo :-Djvm_name=Rambo
I would alter this a bit and instead suggest that you read the parallel mode as a JVM argument and based on that change your parallel execution strategy. You basically employ the same sort of a
IAlterSuiteListener
implementation as I shared above and it could look like below :So you can now change your parallel mode by passing it via the JVM argument. For e.g.,
-DparallelMode=methods
to run all your@Test
methods inparallel