2014-06-30 103 views
0

我正在做一個硒webdriver,我有一個枚舉爲我測試我的應用程序的環境和應用程序本身的枚舉。我的問題是我無法弄清楚如何在應用程序枚舉中使用環境枚舉來打開它。我在我認爲是我的問題的兩個地方發表了評論。我在第一條評論中發現錯誤,因此無論如何將會有所幫助。你如何使用枚舉與另一個枚舉?

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? 
     } 

    } 

} 

}

+0

你能發佈的錯誤? – dkatzel

+0

我在代碼中的第一條評論中有args [1]下面有一條紅線,它表示「表達式的類型必須是數組類型,但它解析爲String」。 – John

+0

您是否有意將chooseApp的參數設置爲String []參數? – wckd

回答

0

如果我理解你的問題,你有一個編譯時錯誤,因爲你正試圖把String args作爲chooseApp一個String[]。我看到的最簡單的方法是通過整個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); 
+0

這幾乎完全奏效,非常感謝。但是當我運行它而不是嘗試使用環境枚舉來打開正確的網站時,它正在使用url的應用程序名稱。因此,不是打開http://****/qa_was8.html,而是試圖打開accountinventory。我的論點是正確的,所以我知道不是這樣。 – John

+0

@Zach嘗試'driver.get(ENV.valueOf(args [0]))'而不是'driver.get(ENV.valueOf(args [1]))''。如果這不能解決它,請發表你的論點,因爲我只是想從你的代碼中猜測出來。另外,我建議你將來嘗試調試器。 –

+0

再次感謝,但現在有一個錯誤行下得到說,「類型Webdriver中的方法get(String)不適用於參數(WebDriver2.ENV) – John