你如何使用枚举与另一个枚举?

时间:2014-06-30 17:30:03

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

我正在制作一个selenium webdriver,我对我正在测试我的应用程序的环境和应用程序本身的枚举有一个枚举。我的问题是我无法弄清楚如何在app enum中使用环境枚举来打开它。我在两个地方发表评论,我认为我的问题是。我在第一条评论时收到错误,因此无论如何都会有所帮助。

public enum ENV
            {
                QA, STAGE, 
                PROD, DEV;
                 public String toString()
                {
                    switch(this)
                    {
                    case QA: return "http://****/qa_was8.html";
                    case STAGE: return "http://****/stage.html";
                    case PROD: return "http://****/prod.html";
                    case DEV: return "http://****/index.html";
                    default: return "http://****/qa_was8.html";
                    }


                }
            }



            public enum Application
            {
                ACCOUNTINVENTORY , AUDITACTIONITEMS;


                public static Application chooseApp(String args)
                {

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


                    switch(app)
                    {


                    case ACCOUNTINVENTORY:


                        driver.get(ENV.valueOf(args[1]));//What would go here
                        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.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("...");
                        }
                    }
                    return null;

                }
            }


public static void main(String[] args) 

{

    if(args.length != 0)
    {
        Application app = Application.chooseApp(args[1]);
        ENV env = ENV.valueOf(args[0]);
        if(app != null)
        {
            app.toString();
        }
        else if(env != null)
        {
            env.toString();// Or maybe is my problem here?
        }

    }

}

}

1 个答案:

答案 0 :(得分:0)

如果我理解了您的问题,那么您会遇到编译时错误,因为您试图将String args视为String[]中的chooseApp。我看到的最简单的解决方案是将整个args数组传递给chooseApp

public static Application chooseApp(String[] args)
  // ...
  driver.get(ENV.valueOf(args[1]));// <-- now you can access args[1]

然后

Application app = Application.chooseApp(args);