How can I set browser capabilities and initialize Selenium Webdriver based on Specflow tags?

107 Views Asked by At

Background: My company uses a Standard/Parent Test Automation framework and all teams customise their test automation frameworks based on the Parent Framework.

My framework inherits the Parent framework and Selenium webdriver is initialized in the Parent framework. I pass arguments to the webdriver using a config file which has the browser capabilities defined in it.

The driver gets initialized in a [BeforeScenario] method in the Parent framework. However, I want to set the browser capability based on certain conditions in the scenario.

For ex: I want to run some of the scenarios in "Headless" mode and others in "Headed" mode. I'm trying to do this by reading the scenario tags associated with the scenario and based on that I set the browser capability. However, my browser has already been initialized in the Parent framework with scenario-context and I cant change it during runtime. What are my options?

@Headless
Scenario Outline: Run this test in Headless mode
        Given I login to the application
    When I enter the details
    Then I validate the details are correct

I tried using [BeforeTestRun] hook but that seems to get initilized after the Webdriver has been initialized as well.


Adding driver initialization code:

Below code is in Parent framework:

[BeforeScenario(Order = 0)]
public void BeforeScenario()
{
    _SeleniumDriverClass = new SeleniumDriver(_scenarioContext);
    _scenarioContext.Set<SeleniumDriver>(_SeleniumDriverClass, "seleniumDriverClass");
    _scenarioContext.Set<ConfigModel>(AppSettingsReader.getConfigModel(), "configModel");
    _SeleniumDriverClass.InitialiseWebDriver();
}

public class SeleniumDriver
{
    private readonly JObject _obj = JObject.Parse(File.ReadAllText("config/browserconfig.json"));
    public IWebDriver Driver { get; set;}

    private readonly ScenarioContext _scenarioContext;
    public SeleniumDriver(ScenarioContext scenarioContext)
    {
        _scenarioContext = scenarioContext;
    }

    public void InitialiseWebDriver()
    {
        ConfigModel configModel = _scenarioContext.Get<ConfigModel>("configModel");

        Driver = LoadBrowserCapabilities(configModel.BrowserType);            
        
        _scenarioContext.Set<IWebDriver>(Driver, "WebDriver");
    }

    
    public IWebDriver LoadBrowserCapabilities(string browserToRun)
    {

        string browserTagFromJson = null;
        var arguments = new string[] { };



                var chromeOptions = new ChromeOptions();
                browserTagFromJson = "Chrome";
                arguments = SetLocalBrowserSpecificCapabilities(browserTagFromJson);
                
                chromeOptions.AddUserProfilePreference("download.directory_upgrade", true);
                chromeOptions.AddUserProfilePreference("download.default_directory",Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "temp"));                
                chromeOptions.AddArguments($"--browser.download.dir={Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "temp")}");

                if (arguments.Length > 0)
                    foreach (var arg in arguments)
                    {
                        chromeOptions.AddArguments(arg);
                    }
              
                Driver = new ChromeDriver(AppDomain.CurrentDomain.BaseDirectory, chromeOptions);           
         
        }

        bool isMaxmized = (bool)_obj.SelectToken("BrowserConfig.Capabilities." + browserTagFromJson + ".LocalBrowser.maximize");
        if (isMaxmized == true) Driver.Manage().Window.Maximize();
        Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds((int)_obj.SelectToken("BrowserConfig.ImplicitWait"));
        Driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds((int)_obj.SelectToken("BrowserConfig.PageLoad"));
        return Driver;
    }

Below config file is in my Framework:

browserconfig.json:

    {
      "BrowserConfig": {    
        "ImplicitWait": 60,
        "PageLoad": 60,
            "Capabilities": {
          "Chrome": {
            "LocalBrowser": {
              "arguments": "--headless,--no-sandbox,--window-size=1920x1080,--start-maximized,--incognito,disable-features=DownloadBubble,disable-features=DownloadBubbleV2,--no-sandbox,--disable-dev-shm-usage",
              "maximize": true
            }
          }  
    }
    
    Appsettings.json:
    
        {  
          "BrowserConfigLocation": "config\\browserconfig.json",
        }
0

There are 0 best solutions below