我有基于以下代码的问题:
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)
。
我现在无法做到这一点因为我知道要发生这种情况,必须在main方法之外声明变量address
。我不能在main方法之外声明address
变量,因为它从命令提示符接收争论。请记住,方法runTest已被指定为接受来自不同类的另一个方法的值。希望有关于如何将main方法中的address
值传递给runTest方法中的address
变量的建议。
答案 0 :(得分:0)
您可以在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中的可选项,它可以帮助您避免空值并采用更实用的方法。
答案 1 :(得分:0)
为了简单起见:在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.setProperty("LoginCaptchaChrome.address", address);
或在命令行上:
-DLoginCaptchaChrome.address=<address>`
并在登录时使用
login.get(System.getProperty("LoginCaptchaChrome.address"));
HTH