Re: [testng-users] Passing Parameters on the fly into A Factory

1,034 views
Skip to first unread message

Krishnan Mahadevan

unread,
Oct 15, 2012, 1:43:48 PM10/15/12
to testng...@googlegroups.com
Craig,
When you say parameters in maven what exactly are you referring to?
Are you referring to VM args?

On Monday, October 15, 2012, Craig Connell wrote:
I've looked around through the messages for something similar to this, but haven't been able to find it.

I have a test that I am running with a factory, primarily so I can change the name of the test runs in the UI.  The tests are being launched from within Jenkins as a maven build.  This all works perfectly fine, I am able to read any parameters out of the testng.xml configuration file, run, and have the test names in the report that I want.

Now that those test are running well, I have some users that want to launch these tests passing in a parameter from Jenkins / Maven.  I have several tests where I have done this before, but not with a factory.  When I use the factory I am able to read out any parameters that are set in the testng.xml config, but I can't seem to pick up any parameters passed in by Maven.

For instance, if I want to add a list of URLs to test, I can add

<parameter name = "specifiedUrls" value = "url1,url2"/>

and then use 

context.getCurrentXmlTest().getParameter("specifiedUrls");

I can get the specifiedUrls parameter. However, if I don't define that parameter in the config file, but try to pass it through maven, then I don't see the parameters.  Looking in the test context, like this:

context.getCurrentXmlTest().getAllParameters()

I get any parameters that I may have defined in the config file, but nothing from maven.

Am I missing something obvious?

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/puCyw-8HAvIJ.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

Yevhen Bilevych

unread,
Oct 15, 2012, 4:28:29 PM10/15/12
to testng...@googlegroups.com
Hi Craig, 

Could you please give more detail of how do you pass parameters from Jenkins to Maven? Snippet of pom file with specific plugins and their configuration would be sufficient.

Thanks, 
Yevhen 

Sent from my BlackBerry® PlayBook™
www.blackberry.com


From: "Craig Connell" <ccon...@lotame.com>
To: "testng...@googlegroups.com" <testng...@googlegroups.com>
Sent: October 15, 2012 8:33 PM
Subject: [testng-users] Passing Parameters on the fly into A Factory

Craig Connell

unread,
Jan 14, 2013, 9:30:48 AM1/14/13
to testng...@googlegroups.com
Sorry for the late response ...

Yehven - In Jenkins configure the build to be parameterized, with a parameter type of string and name of 'specifiedUrls'.  This works for other TestNG projects I have, but not when using a factory.

Krishnan - No, I don't mean VM parameters, I mean parameters that are made available to the build process.

You can see what I am talking about here: https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build

As I mentioned, this does work other TestNG builds where I am not using a factory, such as the following:

In Jenkins

Create a maven based project, in that project two build parameters are specified:

environment - dev, test, prod, for instance
browser - chrome, firefox, iexplore

The POM - Notice the system properties for environment and browser

....
<build>
<finalName>seleniumTest</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Skip the normal tests, we'll run them in the integration-test phase -->
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<systemProperties>
<property>
<name>environment</name>
<value>${environment}</value>
</property>
<property>
<name>browser</name>
<value>${browser}</value>
</property>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>integration-test</id>
<activation>
<property>
<name>!maven.test.skip</name>
</property>
</activation>
</profile>
</profiles>
....

test-ng.xml - Doesn't need to know anything about those parameters

....
<suite name="Web Regression Suite" parallel="none">

  <test name="Audience Statistics Tests" preserve-order="true">
    <classes>
      <class name="com.lotame.test.webDriverRegression.AudienceStatisticsSuiteGrid"/>
    </classes>
  </test> 

</suite>

.... 

This is the Class - Notice I pick up the two parameters environment and browser

....
@Parameters({"environment", "browser"})
@BeforeSuite
public static void initEnvironment(@Optional String environment , @Optional String browser) 
{
   
TestEnvironmentSetup setup  = new TestEnvironmentSetup(environment, browser);
driver = setup.getDriver();
env = setup.getEnvironment();
}

.....

Craig

Krishnan Mahadevan

unread,
Jan 27, 2013, 10:59:05 PM1/27/13
to testng...@googlegroups.com
Craig,
Its the same thing. The parameters that are made available to the build process in Jenkins are infact VM args and their values CAN be accessed within Java using System.getProperty(), atleast I remember being able to do this in my project currently.

So you are actually expecting getParameters() [which is designed to read values ONLY from the suite XML file] to actually read VM args [ which is what the parameters in a build process is actually]

That WILL NEVER WORK.
Here's what we did in our project [we had almost a similar kind of a requirement] wherein we need to be able to enable our users to provide values via :
1. Suite xml file
2. As environmental variables
3. As VM args (parameters in a jenkins build)

We designed our configuration such that, we first fetch all environment variables, store it in a configuration data structure, read the VM args and if there were values, we update our data structure, and finally read the suite xml and then again go back and update our configuration data structure.

AFAIK, this is the ONLY way in which you can achieve what you are looking for.



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/z1W882HXPccJ.

Craig Connell

unread,
Jan 28, 2013, 12:22:22 PM1/28/13
to testng...@googlegroups.com
One set of structural changes got it for me ...

First, I changed the way that I am using the factory, so now the factory is the class that gets associated with the test and creates the test class instances and left everything else the same.

Things that stay the same:
  • Jenkins Configuration
  • project POM
Things that change:
  • testng.xml becomes:
...
 
  <test name="Impersonated Audience Statistics Tests" preserve-order="true" group-by-instances="true">
    <classes>
      <class name="com.lotame.test.audiencestats.ImpersonationFactory"/>
    </classes>
   </test>  
...
  •  The factory class
In the factory class I now pass the parameters like this:

 @Parameters({"environment", "browser"})
 @Factory
 public Object[] createInstances(@Optional String environment, @Optional String browser)
 {
.... 
for (int row = 0; row < users.length; row++)
tests.add( new AudienceStatsImpersonation(common, users[row][0].toString()) );
                
logger.info("Created new test passing : " + users[row][0].toString());
                
}

 return tests.toArray(new Object[tests.size()]);

In this way I can launch however many instances of the AudienceStatsImpersonation class I need for testing, each one with a different parameter (user) but all using the same browser and environment that were passed from Jenkins.

Craig 
Reply all
Reply to author
Forward
0 new messages