Пример #1
0
        public static void ReadConfig()
        {
            if (Current != null)
            {
                throw new InvalidOperationException("Context is already initialized");
            }

            Current = new WebDriverContext(DriverConfiguration.GetConfiguration(), EnvironmentsConfiguration.GetConfiguration());
        }
Пример #2
0
        private IWebDriver CreateDriver()
        {
            IWebDriver webDriver;

            switch (_driverConfiguration.TargetDriver)
            {
            case DriverNames.IE:
                webDriver = new InternetExplorerDriver(new InternetExplorerOptions()
                {
                    IgnoreZoomLevel = true,
                    IntroduceInstabilityByIgnoringProtectedModeSettings = true
                });
                break;

            case DriverNames.Firefox:
                var firefoxProfile = new FirefoxProfile();
                firefoxProfile.AddExtension(@"JSErrorCollector.xpi");
                if (!String.IsNullOrEmpty(DriverConfiguration.GetConfiguration().DownloadDir))
                {
                    firefoxProfile.SetPreference("browser.download.dir", DriverConfiguration.GetConfiguration().DownloadDir);
                    firefoxProfile.SetPreference("browser.helperApps.alwaysAsk.force", false);
                    firefoxProfile.SetPreference("browser.download.folderList", 2);
                    firefoxProfile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
                    firefoxProfile.SetPreference("browser.download.useDownloadDir", true);
                    firefoxProfile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "image/png, application/vnd.ms-excel");
                }
                webDriver = new FirefoxDriver(firefoxProfile);
                break;

            case DriverNames.Chrome:
                webDriver = new OpenQA.Selenium.Chrome.ChromeDriver();
                break;

            case DriverNames.PhantomJS:
                var options = new PhantomJSOptions();
                options.AddAdditionalCapability("user-agent", PhantomUserAgent);
                webDriver = new PhantomJSDriver(options);
                break;

            case DriverNames.Safari:
                webDriver = new OpenQA.Selenium.Safari.SafariDriver();
                break;

            default:
                throw new NotImplementedException();
            }

            return(webDriver);
        }
        protected void TakeScreenshot(WebDriverContext instance)
        {
            var cfg = DriverConfiguration.GetConfiguration();

            if (!cfg.TakeScreenshots)
            {
                return;
            }

            var testName = TestContext.CurrentContext.Test.Name;
            var invalids = Path.GetInvalidFileNameChars();

            testName = invalids.Aggregate(testName, (current, inv) => current.Replace(inv.ToString(), string.Empty));

            if (testName.Length > 200)
            {
                testName = testName.Substring(0, 200) + "...";
            }

            var path = Path.Combine(
                cfg.ScreenshotDir,
                testName);

            var di = new DirectoryInfo(path);

            if (!di.Exists)
            {
                Directory.CreateDirectory(path);
            }

            path = Path.Combine(
                di.FullName,
                StartTime.ToString(DateFormat) + ".png");

            instance.TakeScreenshot().SaveAsFile(
                path,
                ImageFormat.Png);
        }