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
Selenium.WebDriver
Selenium.WebDriver.ChromeDriver
Your first test case
The Web App has to be running for Selenium to work
[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"));