【问题标题】:Run ant build.xml with "environment url" parameter passed运行 ant build.xml 并传递“环境 url”参数
【发布时间】:2014-08-13 17:27:06
【问题描述】:

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

我做了什么:

我使用 HtmlUnit 在 Java 中编写了一个测试。我可以使用 ant 构建来执行我的测试。它工作正常。我还可以在 Jenkins 中执行我的 ant 脚本。

我的问题:

我希望能够在开发、阶段和生产环境中运行我的测试,而无需更改代码。我可以将这些环境“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”以在其他环境中进行测试。

【问题讨论】:

  • 一个常用的标准,就是每个环境都有一个config.properties。 config.properties 可以包含一个属性,指示当前环境是什么。你有没有考虑过这样的事情?
  • ok 有道理,那么我将如何在我的 ant 构建中使用该属性?我想在我的 ant 构建运行之前更改该属性值,所以如果我可以将它作为参数传入会很好
  • 您不会在您的 ant 构建中访问它,而是以编程方式访问它。但是,有人给出了一个更好的答案来做你想做的事情。

标签: java selenium ant jenkins build.xml


【解决方案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 构建文件:ant -f build.xml -Denv.url=DEV

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多