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

时间:2014-06-27 18:03:11

标签: java selenium enums webdriver command-line-arguments

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参数框中添加一些内容 运行|运行配置|争论,但我不知道是什么。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

尝试:

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());
}
相关问题