gpt4 book ai didi

java - 如何将枚举与命令行参数一起使用?

转载 作者:太空宇宙 更新时间:2023-11-04 06:42:44 24 4
gpt4 key购买 nike

public class WebDriver2 
{

public enum ENV
{
QA, STAGE,
PROD, DEV
}

public static class Environment
{
ENV env;
public Environment(ENV env)
{
this.env = env;
}

public void chooseEnvironment()
{
File file = new File("H:\\InternStuff\\Selenium\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath() );
WebDriver driver = new InternetExplorerDriver();
switch(env)
{
case QA:
driver.get("http://****/qa_was8.html");
break;
case STAGE:
driver.get("http://***/stage.html");
break;
case PROD:
driver.get("http://****/prod.html");
break;
case DEV:
driver.get("http://***/index.html");
break;
default:
String[] links;
links = new String[4];
links[0]= "http://****/qa_was8.html";
links[1]= "http://***/stage.html";
links[2]="http://****/prod.html";
links[3]="http://****/index.html";
for(int i = 0; i<links.length;i++)
{
driver.get(links[i]);

}
}
}
}
public enum App
{
ACCOUNTINVENTORY , AUDITACTIONITEMS
}
public static class Application{
App app;
public Application(App app)
{
this.app = app;
}
public void chooseApp()
{

File file = new File("H:\\InternStuff\\Selenium\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath() );
WebDriver driver = new InternetExplorerDriver();

switch(app)
{
case ACCOUNTINVENTORY:


driver.get("http://***/qa_was8.html");
driver.findElement(By.linkText("Account Inventory")).click();
driver.findElement(By.name("j_username")).sendKeys("****");
driver.findElement(By.name("j_password")).sendKeys("***");
driver.findElement(By.cssSelector("input[type='submit']")).click();
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
driver.findElement(By.className("inputText")).sendKeys("smith");
driver.findElement(By.className("commandExButton")).click();
break;
case AUDITACTIONITEMS:

driver.get("http://***/qa_was8.html");
driver.findElement(By.linkText("AuditAction Items")).click();
driver.findElement(By.name("j_username")).sendKeys("***");
driver.findElement(By.name("j_password")).sendKeys("***");
driver.findElement(By.cssSelector("input[type='submit']")).click();
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
driver.findElement(By.className("commandExButton")).click();
default:
System.out.println("Enter app name");
}

}
}

public static void main(String[] args)

{



}

}

我正在 eclipse 中工作,正在制作一个 selenium webdriver,我需要能够使用命令行参数,在其中为其提供应用程序名称和环境名称(均为枚举),然后它运行测试。我知道我需要在 eclipse 参数框中放一些东西运行|运行配置|争论但我不确定是什么。任何帮助,将不胜感激。

最佳答案

尝试:

public static void main(String[] args) {
try {
App argArg = App.valueOf(args[0]);
ENV argENV = ENV.valueOf(args[1]);
} catch(IllegalArgumentException e) {
printHelp();
System.exit(1);
}
...
}

printHelp() 中,您可以列出所有可能的值:

StringBuffer appParamHelp = new StringBuffer("Possible 'App' values are:");
for(App possibleAppVal : App.values) {
appParamHelp.append(" ");
appParamHelp.append(possibleAppVal.name());
}

关于java - 如何将枚举与命令行参数一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24458230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com