Repository navigation
Initialize Web Driver
You can initialize the web driver either using your custom methods within your project or use methods from Selenium.Essentials.
BrowserHelper is the class which helps in initializing different web drivers
Local web drivers are those that run on the same machine where the test is being executed. Various browsers, such as Chrome, Firefox, and Internet Explorer, can be initialized using local web drivers.
For example, to open a Chrome browser, you can use the following command. This will launch Chrome with the default ChromeOptions. Ensure that the chromedriver.exe file is in the directory where your test runs. Alternatively, you can add it as a NuGet package reference in your test project.
var driver = BrowserHelper.GetChromeBrowser();Advanced Option: If you want to change or have your own ChromeOptions', you can set the property ChromeOptions' before you initialize the driver. For example, the below code will set your custom chrome options and then initialize the driver.
SeleniumDriverCapabilitiesProvider capabilities = new();
capabilities.ChromeOptions = yourMethodToGetChromeOptions();
var driver = BrowserHelper.GetChromeBrowser();You can also call GetDriver() method to create the driver based on your configuration. You need to pass the browser type enum.
BrowserHelper.GetDriver(BrowserType.Chrome)The same can be initialized or applied to Firefox, Internet Explorer, Edge and Safari browsers.
Remote web drivers are used to run tests on browsers located on a remote machine, such as when using Selenium Grid, Sauce Labs, or BrowserStack. These drivers allow you to execute tests across various environments without being limited to the local machine.
To initialize a remote web driver, you require to pass three properties
- Remote Hub Url
- Command Timeout for the remote web driver
- Capabilities for the remote web driver (Dictionary which contains all the capabilities that need to be passed to the remote driver)
Selenium.Essentials uses a c# model that holds the remote driver details RemoteDriverAccessModel
For example, to initialize a sauce lab web automation test, which internally uses a selenium grid.
- Initialize the
RemoteDriverAccessModel- Set
RemoteHubUrl - Set
CommandTimeoutInSeconds - Set
Capabilities
- Set
- Invoke
BrowserHelper.GetDriver(browerType, isRemote: true, remoteDriverModel)
var sauceLabCapabilities = new Dictionary<string, string>
{
{ "build", buildNumber }, //buildNumber is a local field which get the value of the current build number
{ "tunnel-identifier", jobNumber }, //jobNumber is a local field which get the current job number
{ "username", sauceUsername }, //sauceUsername is a local field which contains the sacuce lab user name which is required to run test in sauce labs
{ "accessKey", sauceAccessKey }, //sauceAccessKey is a local field which contains the sacuce lab accesskey which is required to run test in sauce labs
{ "name", testName } //your test name
};
RemoteDriverAccessModel remoteDriverModel = new()
{
RemoteHubUrl = "http://ondemand.saucelabs.com:80/wd/hub",
CommandTimeoutInSeconds = 60,
Platform = browserCapability.PlatformName,
BrowserVersion = browserCapability.Version,
Capabilities = new Dictionary<string, object> { { "sauce:options", sauceLabCapabilities } }
};
var driver = BrowserHelper.GetDriver(browserCapability.BrowserName, isRemote: true, remoteDriverModel); //initialize the remote driver