Selenium WebDriver Factory选择相同的驱动程序

时间:2019-09-18 09:19:11

标签: c# selenium selenium-webdriver

我正在使用带有Selenium Webdriver的Visual Studio制作WinForms应用程序。我正在为每个测试使用不同的浏览器运行一些测试,这些测试是通过单击不同的winforms按钮启动的。

我创建了一个Webdriver Factory类,如下所示:

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)
                    {
                        FirefoxProfile profile = new FirefoxProfile();
                        FirefoxOptions options = new FirefoxOptions();
                        options.Profile = profile;
                        options.BrowserExecutableLocation = $@"{EcrisAdreseFrameW.Properties.Settings.Default["caleAppData"]}\Internet JS4\App\firefox64\firefox.exe";
                        FirefoxDriverService service = FirefoxDriverService.CreateDefaultService($@"{EcrisAdreseFrameW.Properties.Settings.Default["caleAppData"]}");
                        driver = new FirefoxDriver(service, options);
                        Drivers.Add("Firefox", driver);
                    }
                    break;

                case "Chrome":
                    if (driver == null)
                    {
                        var service = ChromeDriverService.CreateDefaultService($@"{EcrisAdreseFrameW.Properties.Settings.Default["caleAppData"]}\ChromeDriver");
                        var options = new ChromeOptions();
                        service.HideCommandPromptWindow = true;
                        options.BinaryLocation = $@"{EcrisAdreseFrameW.Properties.Settings.Default["caleAppData"]}\GoogleChromePortable\App\Chrome-bin\chrome.exe";

                        options.AddUserProfilePreference("disable-popup-blocking", "true");
                      driver = new ChromeDriver(service, options);

                      Drivers.Add("ChromeAscuns", driver);
                    }
                    break;
            }
        }

第一个按钮的代码:

BrowserFactory.InitBrowser("Firefox");
BrowserFactory.Driver.Navigate().GoToUrl("http://www.google.com");

第二个按钮的代码:

BrowserFactory.InitBrowser("Chrome");
BrowserFactory.driver.Navigate().GoToUrl("http://www.bing.com");

当我单击任意按钮时,它将启动正确的Web驱动程序(第一个按钮为Firefox,第二个按钮为Chrome)。但是,当我单击另一个按钮时,它使用的是与第一次出现相同的Webdriver浏览器。

代码中我缺少什么?

1 个答案:

答案 0 :(得分:0)

您似乎想要缓存Web驱动程序并重新使用它们。问题是您要检查是否已创建“驱动程序”,如果尚未创建,请将该实例缓存在字典中。

在输入switch语句之前,需要先查阅字典,如果已经存在使用该名称的Web驱动程序,则过早退出该方法。您还需要假设当您陷入switch语句时,您将总是正在创建一个新的Web驱动程序。

public static void InitBrowser(string browserName)
{
    if (Drivers.ContainsKey(browserName))
    {
        // Switch to existing web driver
        driver = Drivers[browserName];

        return;
    }

    // Initialize new web driver
    switch (browserName)
    {
        case "Firefox":
            FirefoxProfile profile = new FirefoxProfile();
            FirefoxOptions options = new FirefoxOptions();
            options.Profile = profile;
            options.BrowserExecutableLocation = $@"{EcrisAdreseFrameW.Properties.Settings.Default["caleAppData"]}\Internet JS4\App\firefox64\firefox.exe";
            FirefoxDriverService service = FirefoxDriverService.CreateDefaultService($@"{EcrisAdreseFrameW.Properties.Settings.Default["caleAppData"]}");
            driver = new FirefoxDriver(service, options);
            Drivers.Add("Firefox", driver);
            break;

        case "Chrome":
            var service = ChromeDriverService.CreateDefaultService($@"{EcrisAdreseFrameW.Properties.Settings.Default["caleAppData"]}\ChromeDriver");
            var options = new ChromeOptions();
            service.HideCommandPromptWindow = true;
            options.BinaryLocation = $@"{EcrisAdreseFrameW.Properties.Settings.Default["caleAppData"]}\GoogleChromePortable\App\Chrome-bin\chrome.exe";
            options.AddUserProfilePreference("disable-popup-blocking", "true");
            driver = new ChromeDriver(service, options);
            Drivers.Add("ChromeAscuns", driver);
            break;
    }
}