我想使用 Selenium 和 xUnit 在不同的浏览器上运行我的单元测试。我在整个研究过程中找不到合适的解决方案。我发现了 nUnit 的一些变体,但它们不符合我的需要。
- > https://blog.testproject.io/2017/02/09/cross-browser-testing-selenium-webdriver/
- > Cross-Browser testing with Selenium WebDriver using C#
解决方案应在三种不同的浏览器(IE、Chrome、Firefox)中执行所有测试。浏览器的数量也应该是可定制的。
有什么好的解决办法吗?
最佳答案
我找到了一个解决方案,它实际上不会同时在多个浏览器中执行测试,但是当您手动执行项目并动态获取驱动程序类型时它会起作用:
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"
关于selenium - 使用 Selenium 在 xUnit 中进行跨浏览器测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53557794/