Please post your Web Driver questions in official Web Driver forum

Saturday, October 29, 2016

Are your selenium element locators static ?

I usually declare element locator static as well as the helper method using them, since element locators is a class variable or constant and is not dependent on a specific instance of class. For example in the following code snippet, element locators - imageElement and teamDeletionSucessMessage are static. Also, method - isTeamDeletionSucessMessageDisplayed is static -


Screenshot from 2016-04-19 15:59:39.png


From one of the SO answer, following can be the requirement to make method static -

  1. If you are writing utility classes and they are not supposed to be changed.
  2. If the method is not using any instance variable(s) or instance method(s). {some thing I consider most important when declaring a variable static}
  3. If any operation is not dependent on instance creation.
  4. If there is some code that can easily be shared by all the instance methods, extract that code into a static method.
  5. If you are sure that the definition of the method will never be changed or overridden. As static methods can not be overridden.
What is your take on this? Are your element locators also static (or even static final)?
Fork me on GitHub