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

Cross-Browser-Testing in xUnit with Selenium

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

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

有什么好的解决方法吗?

现在还没有完美的方法,但你应该尝试 Watin.core Package. 这个包将支持 IE 和 firefox 浏览器,如果你想在 chrome 浏览器中执行测试,那么你使用 Watin.core 包就不走运了。

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

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"