seleniumwithC#

Custom methods for Controls in Selenium with C# (Part 1)

In this post we will discuss writing custom methods for the HTML controls of web pages using Selenium with C# like

  • Textbox control
  • Button control
  • Drop Down list

But in this post we will deal with Set operation in the control like entering text, clicking button, selecting drop down etc. In next post, we will deal with Get operations of controls.

Purpose

The real purpose of writing custom methods is to

  • Simplify the coding (clean and neat)
  • Reuse the code (reduce redundancy of same code)
  • Easy to maintain and modify

Here is the complete video for the above discussion.

Here is the complete code for each and every controls

Text box/Text Area Control

Button/Checkbox/Option Click

Drop Down list box

Thanks for watching the video and reading the post!!

Please leave your comments and let me know if there is anything need to be improved in the post!!!

Thanks,
Karthik KK

19 comments

  1. Chethana says:

    i work with webpages which sometimes do not have the attributes name / id for the element – is there an option to find the element based on the class name ?

    • karthik KK says:

      Yap, you have that option, there is a method in By class, which help you to identify element using Class name

      Thanks,
      Karthik KK

  2. Zoltan Dobo says:

    Your test code with selenium should look like this, because this is the maintainable, neat and oop. Your approach leads to a function hell.

    public class LoginPage
    {
    private IWebDriver webDriver;

    [FindsBy(How = How.Id, Using = “PasswordID”)]
    public IWebElement Password { get; set; }

    [FindsBy(How = How.Id, Using =”cmdSubmitID”)]
    public IWebElement SubmitButton { get; set; }

    [FindsBy(How = How.Id, Using = “txtUserNameID”)]
    public IWebElement UserName { get; set; }

    public LoginPage() { }

    public LoginPage(IWebDriver webDriver)
    {
    this.webDriver = webDriver;

    if(!webDriver.Url.Contains(“Login.aspx”))
    {
    throw new StaleElementReferenceException(“This is not the login page”);
    }
    PageFactory.InitElements(webDriver, this);
    }

    public HomePage signIn(string username, string password)
    {
    UserName.SendKeys(username);
    Password.SendKeys(password);
    SubmitButton.Submit();

    HomePage homePage = new HomePage(webDriver);
    PageFactory.InitElements(webDriver, homePage);
    return homePage;
    }
    }

  3. biswajit says:

    I really thanks for this new idea about reusable code.

    When I implement it in my project, I have question, suppose if there is a change in element locater, thn we need to change all method name every where..

    It is not good idea..
    please guide me where even if the element locater, I need to update it in page object. rather then touching the script…

    • Karthik kk says:

      Thats not possible, since you are using C# properties with its name, if you change the property, then you must change everywhere, rather you can use refactor –> rename to rename all the referenced location, which should be easy !!!

      Hope it helps !!!

      Thanks,
      Karthik KK

  4. ThirupathiReddy says:

    I’m trying to select my drop down value by SelectByIndex (Custom Method). But Its not taking . please give me any Idea about this.
    Thanks in Advance

    Regards,
    Thirupathi.

  5. Michael Nhong says:

    love your training video. Thank you so much for all the tutorials. Do you have video/or can you do one video for Selenium in C# with jstree and drag/drop feature?

    Thanks again
    -Michael

    • Karthik kk says:

      You are welcome Michael, BTW I dont have any video for jstree, should make one if time permits me 🙂

      Thanks,
      Karthik KK

  6. Nadiya Elbaa says:

    Hi There,
    Thanks for the great videos. I am coming across an issue when I try to run this piece of code.
    public void ExecuteTest()
    {
    //Login
    SeleniumSetMethods.EnterText(“txtWebID”, “xxxxxxxxx”, “name”);
    //Password
    SeleniumSetMethods.EnterText( “txtPassword”, “xxxxxxxx”, “Name”);
    //Click
    SeleniumSetMethods.Click( “loginButton”, “Name”);
    Console.WriteLine(“The value from Username is : ” + SeleniumGetMethods.GetText(“txtWebID”, “Name”));
    Console.WriteLine(“The value from Password is : ” + SeleniumGetMethods.GetText(“txtPassword”, “Name”));
    I keep getting an error message saying Unable to find element with name == txtPassword,
    not sure why. Please help, I am new to selenium

    • Karthik kk says:

      You should switch to the iframe and perform operation, again it cannot sit on the controls method though.

      Thanks,
      Karthik KK

Leave a Reply

Your email address will not be published. Required fields are marked *