Creating Automated Browser Tests with Selenium in C#

What is Selenium?

Selenium is a portable framework for testing web applications. The tests can then run against most modern web browsers. Selenium runs on Windows, Linux, and macOS.

Some of the features we can do using Selenium WebDriver

  • Navigate to a specific page/forware/back
  • Click the button with an ID
  • Type text into the <input>
  • Get the text content of the SPAN that has a CSS class
  • Choose a radio button
  • Check a tick box
  • Get the title of the current page
  • Maximum the browser window
  • Take a screenshot

Selenium WebDriver Testing Architecture

The Limitations of Automated Browser Tests

  • Slower than other types of tests (unit tests)
  • Not a replacement of all manuall testing
  • Additional dependencies (Selenium, WebDriver…)

Setting up the test project

Install NuGet Packages

  1. Selenium.WebDriver
  2. Selenium.WebDriver.ChromeDriver

Your first test case

The Web App has to be running for Selenium to work

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
private string baseUrl = "http://localhost:29128/";

[Test]
[Category("Login")]
public void ShouldLogin()
{
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl(baseUrl);

var userNameBox = driver.FindElement(By.Id("username"));
userNameBox.SendKeys("admin");

Thread.Sleep(1000);

var passwordBox = driver.FindElement(By.Id("password"));
passwordBox.SendKeys("admin");

Thread.Sleep(1000);

var submitButton = driver.FindElement(By.Id("submit"));
submitButton.Click();

Thread.Sleep(1000);

var currentPageTitle = driver.Title;
Assert.That(currentPageTitle, Is.EqualTo("identityOne - Home Page"));
}
}

Get Page Title

1
2
3
4
5
6
7
8
9
10
11
[Test]
[Category("Test")]
public void GetPageTitle()
{
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl(baseUrl);

Assert.That("identityOne - Home Page", driver.Title);
}
}

Read current URL

1
2
3
4
5
6
7
8
9
10
11
[Test]
[Category("Test")]
public void ReadCurrentUrl()
{
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl(baseUrl);

Assert.That(baseUrl, driver.Url);
}
}

Reload current page / go backward/forward

1
2
3
4
5
6
7
8
9
10
11
12
13
[Test]
[Category("Test")]
public void ReloadCurrentPage()
{
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl(baseUrl);

driver.Navigate().Refresh();
driver.Navigate().Back();
driver.Navigate().Forward();
}
}

Manipulating HTML Elements

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
[Test]
[Category("Test")]
public void ReloadCurrentPage()
{
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl(baseUrl);

IWebElement textElement = driver.FindElement(By.Id("username")); // find element by ID
string usernameText = textElement.Text; // Get HTML element text

IWebElement buttonElement = driver.FindElement(By.Name("button")); // find element by Name
buttonElement.Click(); // Click a button or link

IWebElement linkElement = driver.FindElement(By.LinkText("link")); // find element by LinkText
linkElement.Click(); // Click a button or link

IWebElement buttonElement = driver.FindElement(By.CssSelector("body")); // find element by CssSelector

IWebElement buttonElement = driver.FindElement(By.ClassName("TestClass")); // find element by class name

IWebElement textElement = driver.FindElement(By.TagName("td")); // find element by tag name

IWebElement linkElement = driver.FindElement(By.PartialLinkText("Partial Text")); // find element by PartialLinkText

IWebElement linkElement = driver.FindElement(By.XPath("/html/body/div[4]/p/a")); // find element by XPath

// this relative XPath will find all <a> elements with its text contains 'some text'
IWebElement linkElement = driver.FindElement(By.XPath("//a[text()[contains(.,'some text')]]")); // find element by Relative XPath

// WebDriverWait is given a timeout value indicating how long to wait for the condition.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(1));
// Selenium will try to find the linkElement until the timeout value is reached
IWebElement linkElement = wait.Until(d => d.FineElement(By.LinkText("some text")));

// Selecting multiple elements
ReadOnlyCollection<IWebElement> tableCells = driver.FindElements(By.TagName("td"));

Assert.That("first cell", tableCells[0].Text);
}
}