2016-08-19 91 views
3

调用变量我有基于以下代码的问题:从主要方法

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_datatest_result。现在,我想将address的值传递给位于runTest方法中的login.get(address)

我现在无法这样做,因为我知道要发生这种情况,变量address必须在主方法之外声明。我不能在主方法外部声明address变量,因为它从命令提示符处接收参数。请记住runTest方法已被分配为接受来自不同类的另一个方法的值。希望能向大家提供如何在runTest方法中将主要方法中的address值传递给address变量的建议。

+1

“我不能声明地址变量的主要方法外,因为它是从命令提示接收arguements。”, - 如何分配'main'方法中的变量限制它不被声明为字段? – user3707125

+0

嗨用户,感谢您的反馈。我相信这是因为如果我在主方法之外声明它们,我将无法接受从命令提示符执行期间传递的争论。这有助于澄清?如果我在这方面有所欠缺,我很抱歉。基本上,我会将这段代码导出到jar文件中,并且将通过java -jar c:\ Test \ LoginCaptchaChrome test1 www.test.com c:\ testdata \ data.xlsx c:\ testresult \ result.xlsx – lamCM

+0

“如果我在主要方法之外声明它们,我将无法接受从命令提示符执行期间传递的争论。“ - 为什么? – user3707125

回答

0

您可以在class LoginCaptchaChrome中声明一个字段。声明一个可以在主要方法中访问的静态字段。

记住非静态字段需要一个要访问的类(一个对象)的实例,静态字段不需要实例并且可以通过静态上下文访问。

现在一些代码示例:

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 documentation

+1

为什么(错误)使用'Optional'如果'address'实际上是强制性的? - 也可以使用'if'或者使用断言手动抛出异常。 - 另外,使用'Optional'的检查已经失败,如果它被设置为空字符串''“'。 –

+0

@MarkusMitterauer:很好,马克,我没有意识到它会失败,因为我认为你的方法使用if和检查条件是一个更好的主意。我在回答中的主要目标是获得使用静态字段的想法。使用可选项显然是可选项(无双关意) – faizan

+0

Downvoter谨慎解释? – faizan

0

为了保持简单:LoginCaptchaChrome创建静态字段:

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