如何通过控制台向方法发送参数?
我有一个如下所示的方法:
@Test
public void test() throws InterruptedException {
botClass.Desktop(driver, "same url");
}
我想发送String
通过控制台的 URL 参数。
例如,我希望能够输入 mvn clean test, www.google.com
在控制台中,程序应该连接到 Google 并执行测试。
这可能吗?如果是,请给我一些关于如何实现这一目标的建议。
最佳答案
这样做:
1) 在你的 scr/test/resouces 中放置一个 testConfig.properties:
url = ${url}
2)编写测试:
@Test
public void test() throws Exception{
String urlParam = null;
try {
Properties prop = new Properties();
InputStream input = null;
input = getClass().getClassLoader().getResourceAsStream("testConfig.properties");
prop.load(input);
urlParam = prop.getProperty("url");
}catch (Exception e){}
// use urkParam
}
3) 在您的 pom.xml 中添加以下内容:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
</plugin>
</plugins>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
4)运行maven如下:
mvn clean test resources:testResources -Durl=www.google.com
现在你将获得基于maven param的参数化url。
关于java - 如何使用 Maven/testng 将参数发送到方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41838670/