我想制作一个可运行的jar来测试网站上的连接。我想用" $ java -jar myjar.jar"传递参数。 shell命令,参数是伪和我要测试的传递。
所以,我有一个完全通过selenium(插件firefox)生成的junit示例类
CheckLog.java
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class CheckLog {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://example.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testl() throws Exception {
// some tests
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
我的Main.java文件
import java.io.Console;
import java.util.Scanner;
import org.junit.runner.JUnitCore;
public class Main {
/**
* @param args
*/
public static void main(String[] args){
if (args.length == 0){
Console console = System.console();
String pseudo = new String(console.readLine("Pseudo : "));
String pass = new String(console.readPassword("Pass : "));
}
JUnitCore jCore = new JUnitCore();
jCore.run(CheckLog.class);
}
}
我的问题是:如何从主类中将一些参数传递给测试类? (像伪和通过)
~UPLOAD~
我会这样:
(1) - 可以从shell命令
获取参数和
(2) - 可以从Main类中获取参数(如果我没有从shell命令传递任何参数,jar会询问参数)
这篇文章帮助我(1)https://stackoverflow.com/a/19530408/2137454。但是,也许有更合适的方法吗?