Automation testing using chrome selenium

First, we will understand what is chrome selenium.

  • Selenium is an open-source automated testing framework that validates our web application across different browsers. It is a powerful tool for automation testing which supports multiple browsers and operating systems. We can use any of the programming languages like C#, Java, Python, etc to create selenium test scripts. To use selenium in our project first we have to download the chrome driver according to a version of google chrome.
    You can download it from here 

Now we can see what is automation testing.

  • Automation testing is a technique to test your application with the help of tools, scripts, and software instead of humans testing them manually. automation focuses on replacing manual human activity with devices that enhance the efficiency of software. Automation is used to automate repetitive tasks and other testing tasks which are difficult to perform manually.

Here, I will show you how you can perform a login, redirect to another page, and store your username in the hidden field.

Let’s begin,

Step 1:Open visual studio and create a new project select unit test project

Step 2: Add chrome driver to the project folder

Step 3:Install NuGet package Selenium.WebDrive

Step 4:Intialize the chrome driver using the below code

using OpenQA.Selenium.Chrome;
[TestClass]
    public class UnitTest1
    {
        private ChromeDriver driver;

        [TestInitialize]
        public void ChromeDriverInitialize()
        {
            try
            {
                driver = new ChromeDriver(@"D:\Niki\TestProject1\ChromeDriver");  //Path For ChromeDriver.
            }
            catch (System. Exception ex)
            {

            }

        }

Now copy-paste the below code to login into the system using chrome selenium.

[TestMethod]
        public void LoginAutomation()
        {
            var URL = "https://localhost:44372/";
            driver.Navigate().GoToUrl(URL);
            var UserName = driver.FindElement(By.Id("txtusername"));
            var Password = driver.FindElement(By.Id("txtpass"));
            var LoginButtonClik = driver.FindElement(By.Id("btnLogin"));
            UserName.SendKeys("example@gmail.com"); //Username Or EmailId
            Password.SendKeys("12345"); //Password 
            LoginButtonClik.Click(); //Click Your Button
            Thread.Sleep(5000);
            var NewUrl = "https://localhost:44372/Home/Index";
            driver.Navigate().GoToUrl(NewUrl); // Go To Your Home Page
            string Uname = "example@gmail.com";
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            ((IJavaScriptExecutor)driver).ExecuteScript("document.getElementsByName('hdnUserId').item(0).value = '" + Uname + "';");
            Thread.Sleep(3000);
        }

In the above code, you can see that how we get the element by using id attribute and how to set the value in that element by using SendKeys.

Here I am setting the value of a hidden field using IJavaScriptExecutor.
IJavaScriptExecutor is an interface that is used to execute javascript with selenium.

If you want to get id of textbox or any other input you can also use FindElementById,FindElementByClassName,FindElementByXpath.

Example of FindElementById,FindElementByClassName,FindElementByXPath
var UserName = driver.FindElement(By.Id(“txtusername”));
var UserName = driver.FindElement(By.ClassName(“txtusername”));
var UserName = driver.FindElement(By.XPath(“txtusername”));

To Find Xpath, Please follow like below.

Step 5:To execute unit test click Test->TestExplorer

explore your test case and run or debug your test method if you have to execute all your cases then you have to click “Run All Tests”.

Note: Test would start the IIS server on their own so that visual studio does not run the test while the website is running/debugging so, you have to load two different visual studios one to start the server(for your localhost) and another for a test.
 

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories