how does this small c# code work

I joined a new team that automates using c# and selenium. I cant follow how this line works:

   driver.FindElement(Elements.OkLink).click()

I know about why driver and FindElement are used . but i just cant figure out how Elements.OkLink is used. What is it a variable/object/method

This below line is where OkLink is actualy defined.

    public class Elements
    {
        public static By OkLink = By.LinkText("Ok");

    }

I found out the following definition from Selenium documentation:

  By.LinkText Method    

  Syntax:

   public static By LinkText(
string linkTextToFind
    )

   Return Value: A By object the driver can use to find the elements.

By is actually a class . Is LinkText a method. I thought methods are similar to functions. How is ClassName MethodName is used here.

Jon Skeet
people
quotationmark

Is LinkText a method.

Yes.

I thought methods are similar to functions.

That's right.

How is ClassName MethodName is used here.

In the declaration? That just indicates what the method returns. So to pull this declaration apart:

public static By LinkText(string linkTextToFind)
  • public means it can be called by any code, in any assembly
  • static means the method is associated with the type rather than with any particular instance of the type. (It doesn't depend on an instance.) That's why it's called using the class name rather than via an instance - By by = By.LinkText.)
  • By is the return type - the method will return a value of type By - a reference to a By object, or a null reference.
  • LinkText is the name of the method
  • string linkTextToFind is a parameter of type string with name linkTextToFind

people

See more on this question at Stackoverflow