2

So I have a class and some methods inside, many of this methods interact directly with the drive and I want to add --headless in the browser options.

But if I star the browser at the beginning I cant add options

public class Class_Login()
{
    EdgeOptions options = new();
    options.AddArgument("--headless");
    EdgeDriver driver = new EdgeDriver(options);
...

here I got an error, so I was reading the documentation and I found that the options need to be used inside an method, so I did this:

public void Initiate()
{
    EdgeOptions options = new();
    options.AddArgument("--headless");
    EdgeDriver driver = new EdgeDriver(options);
}

And it work if i use the driver inside the method, but i need to use it in others methods too, is there a way for doing this? can someone help or indicate some meterial?

1
  • use the class constructor for your AddArgument and when you call new EdgeDriver(). Usually you could initialize everything in the class itself, but calling new on EdgeDriver will call its constructor and actually execute the driver executable... You may want to only declare in the class, and then initialize in the constructor. Once you declare, i.e.: EdgeDriver driver; in the class, driver becomes scoped to the whole class. You can then reference/initialize that class-level member from with the function by using: driver = new EdgeDriver()..., same for options declared at class level. Commented Mar 21 at 19:23

1 Answer 1

2

In C#, a class cannot contain code except within a method or when declaring a property.

Here's how I set up the driver in my framework that's built using NUnit.

WebDriverHelper.cs

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;

namespace SeleniumFramework.Common
{
    class WebDriverHelper
    {
        /// <summary>
        /// Gets the name of the browser from the NUnit TestContext.
        /// </summary>
        /// <returns>The browser name.</returns>
        public static Browser GetBrowserFromTestContext()
        {
            string browserValue = TestContext.Parameters.Get("browser")!;
            if (Enum.TryParse(browserValue, true, out Browser browser))
            {
                return browser;
            }

            throw new ArgumentException($"<{browserValue}> is not a defined browser type.");
        }

        /// <summary>
        /// Gets the desired Selenium driver from the config.
        /// </summary>
        /// <returns>A driver instance.</returns>
        public static IWebDriver GetDriver()
        {
            Browser browser = GetBrowserFromTestContext();
            switch (browser)
            {
                case Browser.Chrome:
                    return new ChromeDriver();
                case Browser.Edge:
                    return new EdgeDriver();
                default:
                    throw new ArgumentException($"{browser} is not defined in GetDriver().");
            }
        }

        /// <summary>
        /// A list of supported browsers.
        /// </summary>
        public enum Browser
        {
            Chrome,
            Edge
        }
    }
}

TestContext.Parameters.Get("browser")!; pulls the value from the local.runsettings file, e.g.

<RunSettings>
  <RunConfiguration>
    <TargetPlatform>x64</TargetPlatform>
    <TargetFrameworkVersion>net7.0</TargetFrameworkVersion>
  </RunConfiguration>
  <TestRunParameters>
    <Parameter name="browser" value="chrome" />
    <Parameter name="environment" value="prod" />
  </TestRunParameters>
</RunSettings>

BaseTest.cs

using OpenQA.Selenium;
using SeleniumFramework.Common;

namespace SeleniumFramework.Tests
{
    public class BaseTest
    {
        /// <summary>
        /// The thread-safe driver instance.
        /// </summary>
        public ThreadLocal<IWebDriver> Driver = new ThreadLocal<IWebDriver>();

        /// <summary>
        /// Holds the URL for the landing page.
        /// </summary>
        public string Url;

        [OneTimeSetUp]
        public void OneTimeSetup()
        {
            Url = // get from config file;
        }

        [SetUp]
        public void Setup()
        {
            Driver.Value = WebDriverHelper.GetDriver();
            Driver.Value.Url = Url;
            Driver.Value.Manage().Window.Maximize();
        }

        [TearDown]
        public void TearDown()
        {
            Driver.Value?.Quit();
        }

        [OneTimeTearDown]
        public void OneTimeTearDown()
        {
            Driver.Dispose();
        }
    }
}

In your case, you would add browser options in GetDriver() in WebDriverHelper.cs.

4
  • you can declare and initialize in the class. (as you did here: "public ThreadLocal<IWebDriver> Driver = new ThreadLocal<IWebDriver>();") So the wording of "a class can't contain code" is a little off methinks. Commented Mar 21 at 19:34
  • @browsermator Yeah... I thought about that and I know that's not completely accurate but I couldn't think of a better way of phrasing it that's more correct. I'm open to a better description, if you have one.
    – JeffC
    Commented Mar 21 at 20:02
  • @browsermator What about this, "In C#, a class cannot contain code except within a method or when declaring a property."? I updated my answer with this phrase... I'm guessing it's still not all encompassing but that should be closer and less incorrect.
    – JeffC
    Commented Mar 21 at 20:03
  • yeah, that's better... I can't think of much better without making it pretty long. (and I'm not sure I'd be all that accurate with my words either) Commented Mar 21 at 20:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.