I have a Java project containing junit tests that needs to be run on different test environments (Dev, Staging, etc.) via Jenkins.
How can I setup the building of the project to the different environments and how to pass the url, username and the password to maven?
Can I use maven 3 profiles to read the environment url, username and password from a property file?
Edit: I've added the profiles to the Project POM:
<profiles>
<profile>
<id>Integration</id>
</profile>
<profile>
<id>Staging</id>
</profile>
<profile>
<id>PP1</id>
</profile>
<profile>
<id>PP2</id>
</profile>
<profile>
<id>PP3</id>
</profile>
</profiles>
How to pass the url, username and the password to these profiles?
Currently the tests are acquiring the test environment details from a property file:
public class BoGeneralTest extends TestCase {
protected WebDriver driver;
protected BoHomePage boHomePage;
protected static Properties systemProps;
String url = systemProps.getProperty("Url");
String username = systemProps.getProperty("Username");
String password = systemProps.getProperty("Password");
int defaultWaitTime = Integer.parseInt(systemProps.getProperty("waitTimeForElements"));
static {
systemProps = new Properties();
try {
systemProps.load(new FileReader(new File("src/test/resources/environment.properties")));
} catch (Exception e) {
e.printStackTrace();
}
}
Edit 2:
The change implemented in the test runner class:
public class BoGeneralTest extends TestCase {
protected WebDriver driver;
protected BoHomePage boHomePage;
protected static Properties systemProps;
String url = systemProps.getProperty("Url");
String username = systemProps.getProperty("Username");
String password = systemProps.getProperty("Password");
int defaultWaitTime = Integer.parseInt(systemProps.getProperty("waitTimeForElements"));
String regUsername = RandomStringUtils.randomAlphabetic(5);
final static String appConfigPath = System.getProperty("appConfig");
static {
systemProps = new Properties();
try {
systemProps.load(new FileReader(new File(appConfigPath)));
} catch (Exception e) {
e.printStackTrace();
}
}
A Build profile is a set of configuration values, which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments. Profiles are specified in pom.
Jenkins can schedule your tests to run at specific time. You can save the execution history and Test Reports. Jenkins supports Maven for building and Testing a project in continuous integration.
I wouldn't include any properties in the POM but would use an external property file per environment instead, at least then you wouldn't need to touch the POM when properties change.
In your POM specify a profile which references a property file with your properties in:
<profiles>
<profile>
<id>staging</id>
<properties>
<app.config>/your/path/to/app.staging.properties</app.config>
</properties>
</profile>
</profile>
Then you can pass this into your Surefire configuration:
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<appConfig>${app.config}</appConfig>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
From within you tests you can then load the contents of the property file, e.g.:
final String appConfigPath = System.getProperty("appConfig");
// Load properties etc...
Actually, you could actually take this one step further... dump the Maven profiles completely and just specify -DappConfig=/your/path/to/app.staging.properties
in you Jenkins build configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With