我有一个基于以下代码的问题:
public class LoginCaptchaChrome {
public static void main(String[] args) throws IOException, InterruptedException{
String tc = args[0];
String address = args[1];
String test_data = args[2];
String test_result = args[3];
System.setProperty("webdriver.chrome.driver", "C:\Users\Lam Chio Meng\Desktop\work\chromedriver_win32\chromedriver.exe");
//Do other stuff
}
//runTest is called from a different class
public static void runTest(String string0, String string, String string1) throws InterruptedException, IOException{
WebDriver login = new ChromeDriver();
System.out.println(login);
login.get(address);
//Do other things
}
}
我通过命令提示符从执行期间传递的参数中获得tc,address,test_data
和test_result
的值。现在,我想将address
值传递给位于runTest方法中的login.get(address)
。
我现在无法做到这一点,因为我知道要做到这一点,变量address
必须在主方法之外声明。我不能在主方法之外声明address
变量,因为它正在从命令提示符接收参数。请记住,方法runTest已经被分配为接受来自不同类的另一个方法的值。
address
值从主要方法传递到runTest方法中的address
变量的建议。您可以在class LoginCaptchaChrome
中声明一个字段。声明一个可以在main方法中访问的静态字段。
请记住,非静态字段需要访问类的实例(对象),静态字段不需要实例,可以通过静态上下文访问。
现在一些代码示例:
public class LoginCaptchaChrome {
public static Optional<String> addressOptional = Optional.empty
public static void main(String[] args) throws IOException, InterruptedException{
String tc = args[0];
String address = args[1];
String test_data = args[2];
String test_result = args[3];
//using Optional since it will simply handling value not present cases
addressOptional = Optional.of(args[4]);
System.setProperty("webdriver.chrome.driver", "C:\Users\Lam Chio Meng\Desktop\work\chromedriver_win32\chromedriver.exe");
//Do other stuff
}
//runTest is called from a different class
public static void runTest(String string0, String string, String string1) throws InterruptedException, IOException{
WebDriver login = new ChromeDriver();
System.out.println(login);
//Using this you throw an error when no address is supplied via cmd ,
//if address is supplied then you will get it here
login.get(addressOptional.orElseThrow(()->new NoSuchElement("address is not provided")))
//Do other things
}
}
如果您不熟悉java 8中的optional,请阅读它,它可以帮助您避免null并采用更实用的方法。
可选文档
为了保持简单:在LoginCaptchaChrome
: [public|private] static String address;
中创建静态字段
public class LoginCaptchaChrome {
static String address = ""; // <-- private if only set from main, otherwise public
public static void main(String[] args) throws IOException, InterruptedException{
String tc = args[0];
address = args[1];
String test_data = args[2];
String test_result = args[3];
System.setProperty("webdriver.chrome.driver", "C:\Users\Lam Chio Meng\Desktop\work\chromedriver_win32\chromedriver.exe");
//Do other stuff
}
//runTest is called from a different class
public static void runTest(String string0, String string, String string1) throws InterruptedException, IOException{
WebDriver login = new ChromeDriver();
System.out.println(login);
login.get(address);
//Do other things
}
}
另一个选择是在main()
中使用System propertiesSystem.setProperty("LoginCaptchaChrome.address", address);
或命令行:
-DLoginCaptchaChrome.address=<address>`
并在登录
时使用login.get(System.getProperty("LoginCaptchaChrome.address"));
HTH