November 21, 2019

All in one browser launch method in Selenium C#

  November 21, 2019
There are many ways to write a launch browser and driver initialization method in selenium.

When we are writing one ,there are several things we needs to consider.

Some of the major points will be :
  • Support of different browsers
  • Handling headless execution support
  • Capabilities for different browsers
  • Implementing implicit wait
  • Event Handling (Event firing web drivers)
  • Handling Selenium Grid in the same method
  • Returning Webdriver object
  • Passing argument to the method in a generalized manner
So below is a prototype method which covers almost all these capabilities.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
public IWebDriver LaunchBrowser(string sBrowserName)
{
 //Checking browser

 switch (sBrowserName.ToLower().Split('+')[0]) {
  case "firefox":
   //      var ffOptions = new FirefoxOptions();
   //      ffOptions.BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\firefox.exe";
   //  ffOptions.LogLevel = FirefoxDriverLogLevel.Default;
   //   ffOptions.Profile = new FirefoxProfile { AcceptUntrustedCertificates = true };
   //  var service = FirefoxDriverService.CreateDefaultService();

   FirefoxOptions FirefxOptions = new FirefoxOptions();

   if (sBrowserName.Contains("headless"))   {
    FirefxOptions.AddArguments("--headless");
    FirefxOptions.AddArguments("disable-gpu");
    FirefxOptions.AddArguments("window-size=1920,1080");
   }


   driver = new FirefoxDriver(FirefxOptions);

   break;

  case "ie":
   //Ignore Protected mode settings in IE                               

   var options = new InternetExplorerOptions
   {
    IntroduceInstabilityByIgnoringProtectedModeSettings = true,
    // ForceCreateProcessApi = true,
    EnableNativeEvents = true,                       
    RequireWindowFocus = true,
    IgnoreZoomLevel = true
   };
   driver = new InternetExplorerDriver(options);

   break;

  case "chrome":
   ChromeOptions ChrOptions = new ChromeOptions();

   if (sBrowserName.Contains("headless"))   {
    ChrOptions.AddArgument("--headless");
    ChrOptions.AddArguments("disable-gpu");
    ChrOptions.AddArguments("window-size=1920,1080");
   }
   driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), ChrOptions);
   driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);

   break;

  case "edge":
   // For use with RemoteWebDriver:
   EdgeOptions Edgoptions = new EdgeOptions();
   Edgoptions.AcceptInsecureCertificates = true;
   driver = new EdgeDriver(Edgoptions);

   break;


  case "remote":

   string sSeleniumGridServerPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\GridSetup\selenium-server-standalone-3.141.59.jar ";
   string sChromeDriverPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\chromedriver.exe";
   string sHubConfig = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\GridSetup\hub-conf.json";
   string sNodeConfig = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\GridSetup\win-node-conf.json";

   string strStartGrid;
   strStartGrid = @"/c java -jar " + sSeleniumGridServerPath + "-role hub -hubConfig " + sHubConfig + " -port 5555 -timeout 3000 -browserTimeout 3600";
   //   System.Diagnostics.Process.Start("CMD.exe", strStartGrid);

   string strAddNode;
   strAddNode = @"/c java -Dwebdriver.chrome.driver=" + sChromeDriverPath + " -jar " + sSeleniumGridServerPath + " -role node -nodeConfig " + sNodeConfig + " -hub http://localhost:5555/grid/register/";
   //    System.Diagnostics.Process.Start("CMD.exe", strAddNode);

   //    fnExplicitWait(15000);


   var uri = "http://localhost:5555/wd/hub";
   var capabilities = new ChromeOptions().ToCapabilities();

   var commandTimeout = TimeSpan.FromMinutes(5);
   driver = new RemoteWebDriver(new Uri(uri), capabilities, commandTimeout);

   break;

  default:

    Console.Out("Wrong browser type Entered : " + browser + ", Please select from Chrome/Chrome+headless/Firefox/Firefox+headless/IE");

   break;

 }
 Console.Out("Launched Browser " + browser);
 var firingDriver = new EventFiringWebDriver(driver);

 firingDriver.ExceptionThrown +=
 new EventHandler<WebDriverExceptionEventArgs>(firingDriver_ExceptionThrown);

 firingDriver.FindingElement +=
  new EventHandler<FindElementEventArgs>(firingDriver_FindingElement);

 driver = firingDriver;
 return driver;

}


Below are some of the definition of event handling methods we wrote above.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public void firingDriver_ExceptionThrown(object sender, WebDriverExceptionEventArgs e)
{

 Console.Out("e.ThrownException.Message);
}


public void firingDriver_FindingElement(object sender, FindElementEventArgs e)
{
 Console.Out("Finding Element : " + e.FindMethod);
}

But this scenario may vary on different aspects, so have to keep improvising by adding more functionalities in an optimized way.
logoblog

Thanks for reading All in one browser launch method in Selenium C#

Previous
« Prev Post

No comments:

Post a Comment

Bookmark this website for more workarounds and issue fixes.

Verify Zip file contents without extracting using C# + Selenium

While doing automation testing, we may get a scenario where we need to download a zip file and to validate the contents inside it. One way t...