运行ant build.xml并传递“environment url”参数

时间:2014-06-23 15:36:29

标签: java selenium ant jenkins build.xml

我是Ant的新手和一般的编程。

我做了什么:

我使用HtmlUnit在Java中编写了一个测试。我能够使用ant build执行我的测试。它工作正常。我也能够在Jenkins中执行我的蚂蚁脚本。

我的问题:

我希望能够在开发,舞台和制作环境中运行我的测试,而无需更改代码。我可以将这些环境“URL”作为我的ant构建的一部分传递,还是有其他方法可以做到这一点?

我的代码目前看起来像这样:

配置类:

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;
}
}

测试类:

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();
}

正如您所看到的,当前我想在不同环境中运行测试时,我在我的测试类的构造函数中设置了环境。目前设置为“DEV”。但我可以将其设置为“STAGE”或“LIVE”以在其他环境中进行测试。

1 个答案:

答案 0 :(得分:1)

如果使用java Ant任务执行Java测试类,则可以通过sysproperty嵌套元素将环境URL作为系统属性传递:

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

您可以在构造函数中访问系统属性的值,如下所示:

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

可以从环境中存在的某个配置文件中获取${env.url} Ant属性,作为其注释中提到的另一个用户,或者当您想要运行它时,可以将其显式传递给Ant构建文件:{ {1}}