Showing posts with label C# - Selenium. Show all posts

Below is the code to fetch the value of dropdown in Excel sheet and to send the value to Excel sheet using C#.


Below is the code to fetch the value of dropdown in Excel sheet and to send the value to Excel sheet using C#.

We may sometimes need to Unlock or Unprotect the Excel Workbook and also Excel Worksheet to view and then perform some read, write opera...


We may sometimes need to Unlock or Unprotect the Excel Workbook and also Excel Worksheet to view and then perform some read, write operations using C#, for which below code will be very

We are using the concept of Page Object Model, where we will have a Page class for every page in the application and action class to p...



We are using the concept of Page Object Model, where we will have a Page class for every page in the application and action class to perform action on that particular page class and a Test class which in turn will merge those actions and execute as single Test.

Page Class – For every web page in our application we will have an individual Page class, which will store all the WebElement’s of that particular page.

Action Class – For Every Page class created we will have Action class, which will have methods to perform action on the WebElement’s that are stored in Page class.

Test Class – For Every Page class and Action class we will again have a single Test class, which will merge all the action methods in Action class and let us execute as single Test.


Scenario Automated Below
  • Open Chrome Browser
  • Enter URL to login to Rediffmail
  • Enter Username and Password
  • Click Login Button
  • Close the Opened Browser.


Complete Source Code

Page Class


LoginPage.cs
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Support.PageObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Protractor;


namespace Purush_Framework
{
    class LoginPage:Base
    {

        public LoginPage()
        {

        }

       
        public static IWebElement Usernametxt
        {
            get
            {
               
                return driver.FindElement(By.Id("login1"));
            }
        }
        public static IWebElement Passwordtxt
        {
            get
            {
              
                return driver.FindElement(By.Id("password"));
            }
        }
        public static IWebElement Loginbtn
        {
            get
            {
                return driver.FindElement(By.Name("proceed"));
            }
        }

        
    }
}


Action Class

LoginAction.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Purush_Framework
{
    using System.Threading;
    class LoginAction
    {

        public LoginAction EnterUsernameTxt()
        {
            LoginPage.Usernametxt.SendKeys("Purush@rediffmail.com");
            return new LoginAction();
        }

        public LoginAction EnterPasswordTxt()
        {
            LoginPage.Passwordtxt.SendKeys("123");
            return new LoginAction();
        }

        public LoginAction ClickLoginBtn()
        {
            LoginPage.Loginbtn.Click();
            return this;
        }

     
    }
}



Test Class

LoginTest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.PageObjects;
using OpenQA.Selenium.Support.UI;

namespace Purush_Framework
{

    class LoginTest
    {
        LoginAction LoginObj = new LoginAction();
        public void Login()
        {
            LoginObj.EnterUsernameTxt()  // Fluency Method Concept is used
            .EnterPasswordTxt()
            .ClickLoginBtn();
           
        }

        
        public void EndTest()
        {
         
            Base.driver.Quit();
     
        }
      
    }
}


Base Class

Base.cs
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Protractor;

namespace Purush_Framework
{
    public class Base
    {
        public static IWebDriver driver = new ChromeDriver();
       
        public Base()
        {
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("https://mail.rediff.com/cgi-bin/login.cgi");
           
        }   
    }
  }

If we want to run our tests in parallel on different browser or same browser, we can make use of NUnit's Parallelizable attribute. ...


If we want to run our tests in parallel on different browser or same browser, we can make use of NUnit's Parallelizable attribute.

Below is the complete code, In the below example, There are 2 tests configured to run on IE and Chrome Browser, but if we need some more browser we can add that too, as per the requirements or even stick to just one browser and still do parallel execution.

We would not close Driver instances in times, when we run our Automation Tests, and we will forget to end those tasks even from Taskbar ...


We would not close Driver instances in times, when we run our Automation Tests, and we will forget to end those tasks even from Taskbar and ultimately, it will stack up in Taskbar and start eating/consuming Memory, which will in turn make all other application run slow. 
So it is always good to handle it in our Tests and close any driver instances that are left open.

WebDriver supports class called “SelectElement” in C#, which provides useful methods for interacting with Drop-Down. User can perform op...


WebDriver supports class called “SelectElement” in C#, which provides useful methods for interacting with Drop-Down. User can perform operations to select a value from drop-down or even de-select the value from drop-down using it.

Example Below - "\r\n                     \r\n                         \r\n                         \r\n                       ...



Example Below -
"\r\n                    \r\n                        \r\n                        \r\n                            \r\n                            \r\n                                10\r\n                                \r\n            
                \r\n                                25\r\n   

IWebDriver Driver = new ChromeDriver (); Screenshot screenshot = (( ITakesScreenshot )Driver).GetScreenshot(); screnshot.SaveAsFi...


IWebDriver Driver = new ChromeDriver();
Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
screnshot.SaveAsFile(@"C:\Purush", ScreenshotImageFormat.Jpeg);

Types of Wait in Selenium Using C# Implicit wait Explicit wait Fluent wait Why "Wait" is required in Selenium


Types of Wait in Selenium Using C#

  • Implicit wait
  • Explicit wait
  • Fluent wait

Why "Wait" is required in Selenium