在xUnit中使用Selenium进行跨浏览器测试

时间:2018-11-30 12:40:26

标签: selenium testing selenium-webdriver cross-browser xunit

我想使用Selenium和xUnit在不同的浏览器上运行单元测试。我在整个研究过程中都找不到合适的解决方案。我发现nUnit有一些变体,但它们不符合我的需求。

解决方案应在三种不同的浏览器(IE,Chrome,Firefox)中执行所有测试。此外,浏览器的数量应可自定义。

有什么合适的解决方案吗?

2 个答案:

答案 0 :(得分:1)

现在没有完美的方法来执行此操作,但是您应该尝试使用Watin.core软件包。 它将支持IE和firefox,但不支持chrome。

答案 1 :(得分:1)

我找到了一个解决方案,它实际上不能同时在多个浏览器中执行测试,但是当您手动执行项目并动态获取驱动程序类型时,该解决方案可以工作:

switch (Configuration["DriverType"])
            {
                case "firefox":
                    var firefoxService = FirefoxDriverService.CreateDefaultService(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "geckodriver.exe");
                    firefoxService.FirefoxBinaryPath = Configuration["FirefoxPath"];

                    FirefoxOptions firefoxOptions = new FirefoxOptions();

                    firefoxOptions.SetPreference("browser.download.dir", Configuration["DownloadPath"]);
                    firefoxOptions.SetPreference("browser.download.useDownloadDir", true);
                    firefoxOptions.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");

                    Driver = new FirefoxDriver(firefoxService, firefoxOptions);

                    break;

                case "chrome":
                    var chromeOptions = new ChromeOptions();
                    chromeOptions.AddUserProfilePreference("safebrowsing.enabled", true);

                    Driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), chromeOptions);

                    break;
            }

我使用PowerShell脚本运行.dll,并通过该脚本将驱动程序类型写入appsettings.json文件。示例:

$file = Get-Content "appsettings.json" -raw | ConvertFrom-Json
$file.DriverType = "chrome"