2016-10-10 44 views
1

我是使用硒進行自動化測試的新手。這就是爲什麼,我面臨着從用戶輸入中獲取瀏覽器和URL的問題。像,當執行我的代碼,第一個用戶輸入choosen爲您的組織和第二個是choosen爲您的網址像谷歌,Facebook等我如何選擇瀏覽器,然後從用戶選擇我的具體網址

public class Pages { 


private enum Organization { 
    Firefox, Chrom 
} 
static Organization obj; 
private static Pages Chrom; 
private static Scanner reader; 
private static Scanner reader_1; 
public static void main(String[]args) throws InterruptedException { 

    char ch; 
    String org; 
do 
{ 

System.out.println(""); 
reader = new Scanner(System.in); 
System.out.println("Please Enter The Browser Name :"); 
org = reader.nextLine(); 
obj=Organization.valueOf(org); 
switch(obj) 
{ 
case Firefox: 
    Pages.Firefoxdriver(); 
    break; 
case Chrom: 
    Pages.Chromedriver(); 
    break; 
    default: 
     System.out.println("You Have Selected Wrong Browser "); 
} 
reader_1 = new Scanner(System.in); 
System.out.println("Please a key followed by ENTER: \n "); 
ch = reader_1.next().charAt(0); 

} while (ch != 'q'); 
} 

public static void Firefoxdriver(){ 
    WebDriver driver = new FirefoxDriver(); 
    driver.get("http://qa-0.ls.vu"); 
} 

public static void Chromedriver(){ 
    System.setProperty("webdriver.chrome.driver","E:\\workspace\\chromeDriver\\chromeDriver.exe"); 
    ChromeDriver driver = new ChromeDriver(); 
    driver.get("http://qa-0.ls.vu"); 

} 

public void IEdriver(){ 
    File file =new File("C:/Users/mkv/workspace/ServerDrivers/IEDriverServer.exe"); 
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath()); 
    WebDriver driverIE=new InternetExplorerDriver(); 
    driverIE.navigate().to("http://www.msn.com"); 



} 
} 
+0

你會通過 「選擇您的組織」 做 – thebadguy

回答

1

我固定的幾個問題和重組的代碼。這是使用Selenium的非典型方式。大多數腳本不會詢問用戶要使用的瀏覽器等。如果拼錯瀏覽器名稱,您也會遇到問題。我希望(?)你只是爲了好玩而嘗試Selenium。

一些我固定的東西:

  1. 您可以重複reader
  2. 去掉了一些額外的變量等
  3. 分居用戶輸入轉化爲一個驅動器到一個函數,getDriver(),並返回一個WebDriver實例可以重用。
  4. 將IE添加到enumgetDriver()
  5. 修復了一些拼寫錯誤和拼寫錯誤。

public class Pages 
{ 
    private static WebDriver driver; 
    private static Scanner reader; 

    public static void main(String[] args) throws InterruptedException 
    { 
     char ch; 
     do 
     { 
      System.out.println(); 
      reader = new Scanner(System.in); 
      System.out.print("Please enter the browser name: "); 
      driver = getDriver(reader.nextLine()); 
      driver.get("http://qa-0.ls.vu"); 
      System.out.print("Press q to quit followed by ENTER: "); 
      reader = new Scanner(System.in); 
      ch = reader.next().charAt(0); 
     } while (ch != 'q'); 
    } 

    public static WebDriver getDriver(String browserName) 
    { 
     WebDriver driver = null; 
     Browsers browser = Browsers.valueOf(browserName.toUpperCase()); 
     switch (browser) 
     { 
      case FIREFOX: 
       System.out.println("Launching Firefox"); 
       driver = new FirefoxDriver(); 
       break; 
      case CHROME: 
       System.out.println("Launching Chrome"); 
       System.setProperty("webdriver.chrome.driver", "E:\\workspace\\chromeDriver\\chromeDriver.exe"); 
       driver = new ChromeDriver(); 
       break; 
      case IE: 
       System.out.println("Launching IE"); 
       System.setProperty("webdriver.ie.driver", "C:/Users/mkv/workspace/ServerDrivers/IEDriverServer.exe"); 
       driver = new InternetExplorerDriver(); 
       break; 
      default: 
       System.out.println("You have selected a browser that isn't available."); 
     } 

     return driver; 
    } 

    private enum Browsers 
    { 
     FIREFOX, CHROME, IE 
    } 
} 
相關問題