我对Ant和编程都是新手。
我做了什么:
我写了一个测试在Java中使用HtmlUnit。我可以使用ant构建来执行我的测试。它工作得很好。我也能够在Jenkins中执行我的ant脚本。
我的问题:
我希望能够在开发,阶段和生产环境中运行我的测试,而无需更改代码。我可以通过这些环境的"URL"作为我的蚂蚁构建的一部分,或者有另一种方法来做到这一点?
我的代码现在看起来是这样的:
配置类:
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",以便在其他环境中进行测试。
如果您正在使用java
Ant任务执行Java测试类,您可以通过sysproperty
嵌套元素将环境URL作为系统属性传递:
<java dir="${testdir}" classname="MainClass">
<sysproperty key="env.url" value="${env.url}"></sysproperty>
...
</java>
你可以像这样在构造函数中访问system属性的值:
url = Configuration.setEnvironment(System.getProperty("env.url"));
${env.url}
Ant属性可以从环境中存在的一些配置文件中获取,如另一个用户在他的评论中提到的,或者当您想要运行它时,它可以显式传递给您的Ant构建文件:ant -f build.xml -Denv.url=DEV