title: Generic way to check browser options of running WebDriver instance
body: <p>I have a generic method which eventually returns either a ChromeDriver or FirefoxDriver instance with the provided arguments as options, for example using ChromeDriver:</p>
  
  <pre><code>// Example using ChromeDriver, but also applies to FirefoxDriver
  var options = new ChromeOptions();
  options.AddArguments("foo", "bar", "headless");
  return new ChromeDriver(options);
  </code></pre>
  
  <p>Now I want to test if the instance of the WebDriver actually has those provided arguments, but I haven't found a way to do this in a generic way.</p>
  
  <p>I tried the following code to check if the provided arguments were stored in the browser capabilities, but I couldn't find them there using ChromeDriver.</p>
  
  <pre><code>// Using cast because of generic method
  var capabilities = ((ChromeDriver)driver).Capabilities 
  </code></pre>
  
  <hr>
  
  <p><strong>Headless mode</strong></p>
  
  <p>One of the options / arguments I want to check is running in headless mode. I can do this just fine for Chrome and Firefox.</p>
  
  <p>To check headless mode for Chrome:</p>
  
  <pre><code>var userAgent = driver.ExecuteScript("return window.navigator.userAgent");
  </code></pre>
  
  <p>Which returns a string containing 'headless' when running in headless mode, and by using</p>
  
  <pre><code>var windowChrome = driver.ExecuteScript("return window.chrome");
  </code></pre>
  
  <p>Which returns null if running in headless mode, or an object if not running in headless.</p>
  
  <p>But these methods don't work for Firefox, window.chrome obviously doesn't work, but when comparing the userAgents between non-headless and headless for Firefox they are exactly the same:</p>
  
  <p><strong><em>Non-headless mode</em></strong></p>
  
  <blockquote>
    <p>Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0</p>
  </blockquote>
  
  <p><strong><em>Headless mode</em></strong></p>
  
  <blockquote>
    <p>Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0</p>
  </blockquote>
  
  <p>To check headless mode for Firefox, I could use this, but it's not available for other browsers</p>
  
  <pre><code>var headless = ((FirefoxDriver)driver).Capabilities.GetCapability("moz:headless");
  </code></pre>
  
  <p>I guess this shows how I can't rely on capabilities alone.</p>
  
  <hr>
  
  <p>The examples above are just for checking headless mode, but I want to find a way to check different arguments provided by <code>BrowserOptions</code> in a generic way, after I created an instance of the WebDriver. If there's a way using JavaScript that's fine, but I prefer using just Selenium WebDriver.</p>
  
  <p>This is an example how I currently check for headless mode, I want to be able to apply this to other <code>BrowserOptions</code> too, but I have to find them first :).</p>
  
  <pre><code>if (typeof(TWebDriver) == typeof(ChromeDriver))
      runsHeadless = driver.ExecuteScript&lt;string&gt;("return window.navigator.userAgent").ToLowerInvariant().Contains("headless");
  else if (typeof(TWebDriver) == typeof(FirefoxDriver))
      runsHeadless = Convert.ToBoolean(((FirefoxDriver)driver).Capabilities.GetCapability("moz:headless"));
  else
      throw new NotImplementedException($"WebDriver of type {typeof(TWebDriver)} is not yet supported.");
  
  Assert.That(runsHeadless);
  </code></pre>