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

标签 selenium testing selenium-webdriver cross-browser xunit

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

解决方案应在三种不同的浏览器(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/

相关文章:

java - 为什么设置 expireAfterAccess 不起作用?

php - Selenium 2 (WebDriver) 和 Phpunit?

python - 我怎样才能等到一个元素获得或失去一个类别?

java - Selenium 无法处理大表行数

java - 使用 Selenium 在 Chrome 上处理 "Your connection to this site is not private"弹出窗口

python - 无法创建着色器缓存条目 - 在通过其 Css 选择器定位元素时出错

linux - Headless Firefox 和 Geb/Selenium 的登录问题

unit-testing - 不使用部分模拟?他们在网络应用程序中也意味着吗?

windows - 在多个 Windows 和 Outlook 版本上测试插件的好方法是什么?

c# - 无法从程序集 'OpenQA.Selenium.Internal.IWrapsElement' 加载类型 'WebDriver, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null'