2017-06-04 37 views
-1

我要你的幫助,我的問題,請您: 從我的測試類,我嘗試運行我的測試代碼:如何設置爲下面的代碼的對象的實例?

[setUp] 
public void setup() 
{ 
BrowserFactory.InitBrowser("Chrome"); 
} 

但是,它失敗了,因爲它沒有初始化的「驅動器」變量。

(我得到了味精信息:System.NullReferenceException:將webdriver的瀏覽器實例未初始化,您應先調用方法InitBrowser。)

下面InitBrowser(代碼)功能,因爲我從http://toolsqa.com/selenium-webdriver/c-sharp/browser-factory-or-webdriver-factory/

using OpenQA.Selenium; 
using OpenQA.Selenium.Chrome; 
using OpenQA.Selenium.Firefox; 
using OpenQA.Selenium.IE; 
using System; 
using System.Collections.Generic; 

namespace _AutomationTests.WrapperFactory 
{ 
    public class BrowserFactory 
    { 
     private static readonly IDictionary<string, IWebDriver> Drivers = new Dictionary<string, IWebDriver>(); 
     private static IWebDriver driver; 

    public static IWebDriver Driver 
    { 
     get 
     { 
      if (driver == null) 
       throw new NullReferenceException("The WebDriver browser instance was not initialized. You should first call the method InitBrowser."); 
      return driver; 
     } 
     private set 
     { 
      driver = value; 
     } 
    } 

    public static void InitBrowser(string browserName) 
    { 
     switch (browserName) 
     { 
      case "Firefox": 
       if (Driver == null) 
       { 
        driver = new FirefoxDriver(); 
        Drivers.Add("Firefox", Driver); 
       } 
       break; 

      case "IE": 
       if (Driver == null) 
       { 
        driver = new InternetExplorerDriver(@"C:\PathTo\IEDriverServer"); 
        Drivers.Add("IE", Driver); 
       } 
       break; 

      case "Chrome": 
       if (Driver == null) 
       { 
        driver = new ChromeDriver();//@"C:\PathTo\CHDriverServer" 
        Drivers.Add("Chrome", Driver); 
       } 
       break; 
     } 
    } 

    public static void LoadApplication(string url) 
    { 
     Driver.Url = url; 
     driver.Manage().Window.Maximize(); 
     driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()"); 
    } 

    public static void CloseAllDrivers() 
    { 
     foreach (var key in Drivers.Keys) 
     { 
      Drivers[key].Close(); 
      Drivers[key].Quit(); 
     } 
    } 
} 
複製

}

非常感謝!

+0

你可以將你當前的代碼(用於測試和InitBrowser)粘貼到你的問題中嗎? – mjwills

+0

是的。我使用你的第一個建議,並解決了上述問題。但現在它失敗了LoadApplication()函數,「Driver.Url = url;」,我得到了這個錯誤信息:「參數'url'不能爲空。」當被調用的語句是:BrowserFactory.LoadApplication(ConfigurationManager.AppSettings [「https://www.google.co.il/webhp?tab=ow&authuser=0&ei=Tnk2WZmXFMP7-AGGhIvwBQ&ved=0EKkuCAQoAQ」]); 你能幫我嗎? –

+0

順便說一下,我發現在這個鏈接相同的Q:[鏈接](https://stackoverflow.com/questions/41104389/selenium-webdriver-the-webdriver-browser-instance-was-not-initialized-when-usin ) –

回答

1

代碼中存在一個錯誤。

您應該更改這些行:

if (Driver == null)

這樣:

if (driver == null)

另一種方法是從一開始發生變化:

get 
    { 
     if (driver == null) 
      throw new NullReferenceException("The WebDriver browser instance was not initialized. You should first call the method InitBrowser."); 
     return driver; 
    } 

到:

get 
    { 
     return driver; 
    } 
+1

這是正確的解決方案,我採用與OP相同的代碼,並且知道這些是錯誤的,但不知道哪個部分要完全修復。 – Raimonds

0

3月,嘗試粘貼項目的完整源代碼。項目中的ChromeDriver(),FirefoxDriver(),InternetExplorerDriver()的C#類在哪裏?你有預先編碼嗎? 這裏IWebDriver驅動程序是一個屬性,而類是在靜態初始化加載

空檢查是不是在公共靜態IWebDriver驅動

public BrowserFactory() 
     { 
      Driver = new FirefoxDriver(); 
     } 

     public static IWebDriver Driver 
     { 
      get 
      { 

       return driver; 
      } 
      private set 
      { 
       driver = value; 
      } 
     } 

IWebDriver驅動器的屬性設置爲一個實例的吸氣所需或構造函數如:

甚至在驅動程序可以被初始化之前,調用get屬性來評估空值檢查爲真&,從而拋出異常。

問題要問自己:

  1. 您已經添加庫文件爲火狐,Chrome & IE?
  2. 它是你的項目配置文件的一部分嗎?
  3. 如果沒有添加,添加的最佳方式是在Visual Studio中使用Nuget包管理器 4.這些引用應該是您項目的一部分。讓我發佈一個類似項目的屏幕截圖,我試過 References to library files 5.查看Nuget Package Manager的圖片 Click Manage Nuget Packages 6。單擊管理的NuGet軟件包,瀏覽可用的軟件包

  4. 最後,你不設置驅動器的新實例

[上一頁]

{ 
    class Driver 
    { 
     public enum Browser 
     { 
      chrome, 
      firefox, 
      ie, 
     } 
     public IWebDriver getDriver(string browser_type) 
     { 
      Browser parsed_browser_type; IWebDriver driver=null; 
      bool passed_type = browser_type != null ? true : false; 
      if (passed_type) 
      { 
       Enum.TryParse(browser_type, out parsed_browser_type); 

       switch (browser_type.ToLower()) 
       { 

        case "chrome": 
         driver= new ChromeDriver(new ChromeOptions { Proxy = null }); 
         break; 
        case "firefox": 
         driver= new FirefoxDriver(); 
         break; 
        case "ie": 
         driver= new InternetExplorerDriver(new InternetExplorerOptions { Proxy = null }); 
         break; 
        default: 
        case "": 
         throw new Exception("Browser cannot be null"); 
       } 
      } 
      return driver; 
     } 
    } 
} 

[/預]

相關問題