user3554072
user3554072

Reputation: 387

Run ant build.xml with "environment url" parameter passed

I am new to Ant and to programming in general.

What I have done:

I have written a test in Java using HtmlUnit. I am able to execute my test using an ant build. It works fine. I am also able to execute my ant script in Jenkins.

My problem:

I want to be able to run my tests on dev, stage and production environments without having to alter code. Can I pass these environment "URL's" as part of my ant build or is there another way to do this?

My code currently looks like this:

Configuration Class:

package com.environments;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Configuration {

public static String getUrl(String environment) throws IOException {

    Properties prop = new Properties();
    InputStream input = null;

    input = new FileInputStream("configuration/environment.properties");

    prop.load(input);
    String url = prop.getProperty(environment);

    return url;

}


public static String setEnvironment(String url) throws Exception {

    switch (url) {
        case "DEV": url = Configuration.getUrl("DEV"); break; 
        case "STAGE": url = Configuration.getUrl("STAGE"); break; 
        case "LIVE": url = Configuration.getUrl("LIVE"); break;         
    }

    return url;
}
}

Test class:

package com.course_landingpage;

import static com.thoughtworks.twist.core.execution.TwistVerification.verifyEquals;
import org.springframework.beans.factory.annotation.Autowired;
import com.environments.Configuration;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.thoughtworks.twist.core.execution.TwistScenarioDataStore;
import com.ui.pagetitles.PageTitles;

public class LandingPageCarouselTest {

public static String url;

@Autowired
private TwistScenarioDataStore scenarioStore;

public LandingPageCarouselTest() throws Exception {         
    url = Configuration.setEnvironment("DEV");
}


// Tests for the landing page carousel
public void verifyCarouselDisplayedWithCourseInformation() throws Exception {

    final WebClient webClient = new WebClient();
    final HtmlPage page = webClient.getPage(url + "/home.html");
    verifyEquals("Incorrect page title", PageTitles.CPD_LANDING_PAGE, page.getTitleText());

    //Click the Discover More button in the carousel 
    final HtmlAnchor anchor = page.getAnchorByText("DISCOVER MORE");
    anchor.click();

    // Click right arrow to view next course in carousel 
    final HtmlAnchor rightArrow = page.getAnchorByText(">");
    rightArrow.click();

    // Click left arrow to view previous course in carousel 
    final HtmlAnchor leftArrow = page.getAnchorByText("<");
    leftArrow.click();

    // Check the course title is correct in the carousel 
    DomElement element = page.getFirstByXPath("/html/body/section/article/ul/li/div/h2");
    final String course1Title = element.getTextContent();
    verifyEquals("Course text is incorrect", course1Title, Configuration.getCourseTitleInCarousel());

    webClient.closeAllWindows();
}

As you can see, currently when I want to run on my test on different environment, I set the environment in the constructor of my test class. Currently set to "DEV". But I can set it to "STAGE" or "LIVE" to test on the other environments.

Upvotes: 1

Views: 1242

Answers (1)

M A
M A

Reputation: 72874

If you are executing your Java test class using the java Ant task, you can pass the environment URL as a system property via the sysproperty nested element:

<java dir="${testdir}" classname="MainClass">
   <sysproperty key="env.url" value="${env.url}"></sysproperty>
   ...
</java>

You can access the value of the system property in your constructor like this:

url = Configuration.setEnvironment(System.getProperty("env.url"));

The ${env.url} Ant property can be fetched either from some configuration file that exists in the environment as another user mentioned in his comment, or it can be passed explicitly to your Ant buildfile when you want to run it: ant -f build.xml -Denv.url=DEV

Upvotes: 1

Related Questions