Selenium is popular open source test automation solution. It supports a lot of programming languages, a few being java, csharp, ruby, perl etc. Although there are large number of test automation tools available in the market, many commercial and open source. But Selenium remains popular across the global market. The following graph depicts that
Selenium with Java is one of the most popular combinations, the other programming language combination with Selenium is gaining momentum as the roles of developers and testers are merging creating new job description of being a SDET. Selenium with c# has gained quite a popularity and risen in demand in the job market. And if you are a .net enthusiast, then it is a great time to know this tool to increase your market worth and grow in your career.
C# is a popular .net language using which a lot of applications are developed. Its syntax is close to that of the Java programming language, and the IDE Visual studio is most effective for this language. To set up our system to run our very first test in Selenium using C#, we will require the following set of tools
a. Visual Studio Community Edition - https://www.visualstudio.com/downloads/
b. The Selenium webdriver package from the NuGet package manager from the visual studio environment- https://www.nuget.org/packages/Selenium.WebDriver/
c. The Chromedriver package from the NuGet package manager from the visual studio environment- https://www.nuget.org/packages/Selenium.Chrome.WebDriver/
d. The application – http://5elementslearning.com/demosite, to create and run our very first selenium c# test on
e. The NUnit NuGet package - https://www.nuget.org/packages/NUnit/
We will be performing the following steps to generate our very first test for selenium with C#. Let us first understand the manual test case scenario. The scenario we are picking is of the Login Logout from the UAT application, using the credentials for user name – user@user.com, and password – “USER123”. The steps are –
a. Open application using url – http://5elementslearning.com/demosite
b. Click on the my account link
c. Type the username in the email address text box
d. Type the password in the password text box
e. Click the sign in button
f. Verify “My Account Information” text is available on page.
g. Click on the Log Off link
h. Click on the continue button
These are the steps along with verification for the login logout scenario from the application to be tested.
We create a console project in the visual studio, and then we add the NuGet packages for selenium webdriver, chrome webdriver and nunit in the environment.
Once these are installed, we create a class and add the following code in there
using System;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace ProjectSelenium5ElementsLearning.SeleniumPrograms.Day_10
{
[TestFixture]
public class LoginLogoutTest
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
[SetUp]
public void SetupTest()
{
driver = new FirefoxDriver();
baseURL = "http://5elementslearning.com/";
verificationErrors = new StringBuilder();
}
[Test]
public void TheLoginLogoutTest()
{
driver.Navigate().GoToUrl(baseURL + "/demosite/");
driver.FindElement(By.LinkText("My Account")).Click();
driver.FindElement(By.Name("email_address")).Clear();
driver.FindElement(By.Name("email_address")).SendKeys("user@user.com");
driver.FindElement(By.Name("password")).Clear();
driver.FindElement(By.Name("password")).SendKeys("USER123");
driver.FindElement(By.Id("tdb5")).SendKeys(Keys.Escape);
driver.FindElement(By.Id("tdb5")).Click();
if (driver.PageSource.Contains("My Account Information")) {
driver.FindElement(By.LinkText("Log Off")).Click();
driver.FindElement(By.LinkText("Continue")).Click();
Assert.True(true, "User login Success");
}
else
{
Assert.Fail("Login not successful");
}
}
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
[TearDown]
public void TeardownTest()
{
try
{
driver.Quit();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}}}
The above program will execute the login logout test on the application using the chrome browser. To understand and know more about the course of Selenium with C# which we offer, please refer to this url - http://5elementslearning.com/training-detail.php?t=NA==