In the previous page, we reviewed where to find Selenium (Se) and how the basic browser extension (Selenium IDE) works. Now we will move to the more advanced features of Se, the Selenium Web Driver with C#. Note that The WebDriver can be used in a host of languages including Java, Javascript, Python, and Ruby.
First Visual Studio Community Edition, or Eclipse or other IDE should be installed. You can download Visual Studio here. The WebDriver plugins can be downloaded from the site or as nu-get packages in Visual Studio.
Open Visual Studio, create a new console project called SeTest. The project should be C# (though any .NET language should work).
The nuget package manager may be used to install the supporting packages. Within Visual Studio, you can use the console npm, the GUI npm, or the download above.
The following links describe the packages to be installed and list dependencies.
The Selenium.Webdriver can be downloaded here. To install in npm, use command PM> Install-Package Selenium.WebDriver -Version 3.141.0
The Selenium.WebDriverBackedSelenium can be downloaded here. To install in npm, use command PM> Install-Package Selenium.WebDriverBackedSelenium -Version 3.141.0
To install Selenium.Support, navigate to here. Or use npm, with command PM> Install-Package Selenium.Support -Version 3.141.0
Web Browser Driver
A final step requires the installation of the browser driver. We will use Chrome for this example, which can be downloaded here. Take note of the location of the driver once installed. This path is necessary for the testing.
First Selenium C# test
For the first test sample, we will setup the solution to open the browser and load a site. The following code can be copied into Program.cs. You may otherwise download the code.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
// The browser driver chosen previously
namespace SeTest
{
public class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver(@"C:\Users\CJ\Downloads\chromedriver_win32");
driver.Url = "https://www.jerrodfelice.com";
driver.Close();
}
}
}
The Libraries of Selenium
The libraries here are massive and have many classes and methods, but the API can be viewed here and when taken in sections, the library can be learned quickly.
There are three main portions of commands within the library worth noting. The first is the browser commands, seen in the first test above. The second are the web element commands, used for filling in text boxes and other DOM manipulation. The third are the drop down commands used, for, shockingly, drop downs!
A Second Selenium Test
The second test continues focusing on the browser commands. In this test, we will load our site, get the title of the site and check it is correct, refresh the site and again test the title.
static void Main(string[] args) {
IWebDriver driver = new ChromeDriver(@"C:\Users\CJ\Downloads\chromedriver_win32");
driver.Url = "https://www.jerrodfelice.com";
string webTitle = driver.Title;
driver.Navigate().Refresh();
string webTile2 = driver.Title;
if(webTile2 == webTitle)
{
//refresh works
}
driver.Close();
}
To download the code, click here.
Next we will explore Unit Tests and web elements commands and drop down commands.