Automation testing came in as a huge relief to manual testers. Not only is manual testing more prone to errors, but it is also extremely time consuming and especially tedious in testing multilingual websites. Automation testing helps increase test coverage and speed of execution. The first and most important step to getting started with automation testing is to select a test tool. In this article, we will discuss about the automation test tool, Selenium. With the help of some simple examples, you will learn how Selenium works. 

Selenium Automation

What is Selenium?

Selenium is a free and open-source automated testing framework that was created in 2004. It is used to validate web applications and automate web interactions across different browsers and platforms. You can use multiple programming languages like Java, C#, Python, php, Perl or Ruby to write short or complex test scripts. With cross browser support and parallel testing capabilities, Selenium has proven to be one of the most useful and popular automated testing tools today. 

How to use Selenium

Although, the learning curve for Selenium is pretty steep, getting support from its active community is easy. Let’s get started with Selenium with these steps:

  1. Install Visual Studio ‚Äď you can install any version you want. I have used Visual Studio 2019.¬†
  2. Create a project and name it. I have named it as SeleniumTesting. 
  3. Add the NuGet packages, Selenium web driver and selenium support to your project. 
  4. Next, add the Driver for the browser on which u want to perform testing. For example, if u want to test on a Chrome browser, install the selenium.webdriver.chrome driver NuGet.

Login Page Testing ‚Äď An Example

For better understanding of how Selenium works, let us go through a simple (C#) code to test a login page. On successful execution of this test script, you should get a ‚ÄúLogin Successful‚ÄĚ message printed on the console.¬†

class MainClass
{
    static IWebDriver driver = new ChromeDriver(); // Initializing the driver 
    static IWebElement element; // Initialize the element
    public static void Main()
    {
        string url = "https://www.dummyURL.com"; // declare a variable for url. This way, you can use this code for some other project just by changing the url if the element’s name is the same
        string Textbox = "edit-name";// element username field
        string PasswordTxt = "edit-pass";// element password field
        string button = "edit-submit";// element submit button
        driver.Navigate().GoToUrl(url);// using driver navigate to that url
        element= driver.FindElement(By.Id(Textbox));// find username field(element)
¬†¬†¬†¬†¬†¬†¬†¬†element.SendKeys("[email protected]");// send username to username field
        element= driver.FindElement(By.Id(PasswordTxt));// find password field
        element.SendKeys("12345");// send password
        element= driver.FindElement(By.Id(button));// find submit button
        element.Click();// send click event to click the button
        string currenturl = driver.Url;// compare if the current url and the declared url are the same. If they are the same, Login will fail. If urls are different, login will be successful
        if (url == currenturl)
        {
            Console.WriteLine("Login Unsuccessful");
        }
        else
        {
           System.Console.WriteLine("Login Successful");
        }    
        driver.Quit();// close the driver
    }
}

Testing for Multiple H1 Tags in a webpage

As an SEO best practice, a webpage should have only one H1 tag defined. This test script will help in listing out the H1 tags if used multiple times.


IList link = driver.FindElements(By.TagName("h1"));  // declare a list element to the elements with h1 html tag. Find the h1 element by using the FindElements function using tagname as criteria.
        foreach (IWebElement e in link) // loop on the list to print all the h1 elements found by the driver.
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine(e.Text);
        }
Contact us

LET'S DISCUSS YOUR IDEAS. 
WE'D LOVE TO HEAR FROM YOU.

CONTACT US