Skip to main content
 首页 » 编程设计

c#之使用 WebDriver、Nunit 和 C# 测试具有不同选项的多个浏览器

2024年06月20日50kuangbin

我正在尝试使用 WebDriver、Nunit 和 C# 在多个浏览器上运行测试。它正在运行,但我在 Chrome 中收到了烦人的安全警告。为了修复它,我需要使用“.AddArguments(”--test-type”);”重新创建驱动程序。但我只想在这次迭代浏览器 = Chrome 时这样做。这是我的代码。它可以工作,但它会先启动一个不需要的浏览器窗口。有人对此有任何想法吗?

   namespace SeleniumTests 
   { 
        [TestFixture(typeof(FirefoxDriver))] 
        [TestFixture(typeof(InternetExplorerDriver))] 
        [TestFixture(typeof(ChromeDriver))] 
 
        public class TestWithMultipleBrowsers<TWebDriver> where TWebDriver : IWebDriver, new() 
        { 
             private IWebDriver driver; 
 
             [SetUp] 
             public void CreateDriver() 
             { 
                  this.driver = new TWebDriver();  //Creates a window that needs to be closed and re-constructed 
 
                  if(driver is ChromeDriver) 
                  { 
                       driver.Quit();      //This kills the un-needed driver window created above 
                       var chromeOptions = new ChromeOptions(); 
                       chromeOptions.AddArguments("--test-type");  
                       driver = new ChromeDriver(chromeOptions); 
                  } 
             } 

请您参考如下方法:

为什么不在基类中简单地创建 chromedriver 呢?您还可以在那里使用 chromoptions 来传递必要的参数。然后使用

[TestFixture(typeof(FirefoxDriver))] 
[TestFixture(typeof(InternetExplorerDriver))] 
[TestFixture(typeof(ChromeDriver))] 

这也会避免不必要的代码重复和困惑。

我有驱动程序实例的完整实现 here