Storing locators of web elements: public static final String or Enum?

340 views
Skip to first unread message

T.J. Maher

unread,
Jun 19, 2015, 1:51:54 AM6/19/15
to seleniu...@googlegroups.com
Greetings! I wanted to get people's opinion on how people tend to store locators and global variables such as a username or password:

Let's say you had a login page at a website, such as Dave Haeffner's test site, https://the-internet.herokuapp.com/login

The three main web elements you may want to select for, are:
  • Username textbox: id = username // username is tomsmith
  • Password textbox: id= password // password is SuperSecretPassword!
  • Login Button: cssSelector is [type='submit'][class='radius']
If you were using Selenium / Java and a Page Object, for instance, LoginPage.java, how would you best store the locators? Or the username and password global variables? 

Would you store the global variables in:
  • public static final String userName = "tomsmith"?
  • public static final String password = "SuperSecretPassword!"?
I have also seen it as:

public enum UserEnum {

TOM_SMITH("tomsmith","SuperSecretPassword!");

String userName;
String password;

private UserEnum(String UserName, String Password){
this.userName=UserName;
this.password=Password;
}

public String getUserName(){
return userName;
}

public String getPassword(){
return password;

}


And for storing the locators, in  LoginPage.java I have in the bottom of the class:

public enum LoginPageEnum {

USERNAME("[name='username']"),
PASSWORD("[name='password']"),
LOGIN_BUTTON("[type='submit'][class='radius']");

String id;

private LoginPageEnum(String Id){
this.id = Id;
}

public String getId(){
return id;
}

public By selector() {
return By.cssSelector(getId());




... How have you done it? I was just trying to compare notes, and figuring out the pros and cons of one test philosophy over another. 

Right now, I am at the stage of learning that I am just ridiculously happy simply when the test runs! 



- T.J. Maher
  Sr. QA Engineer, Fitbit
  // 15+ yrs manual tester, Automation Engineer for [ 3 ] months and counting

Shawn McCarthy

unread,
Jun 19, 2015, 11:43:44 AM6/19/15
to seleniu...@googlegroups.com
If the username/password are always the same, I would store them in the Login page object. However, they can are different for all the different tests, I would store them in the Tests themselves, and pass them into the page object from my test.

As for the locators, I store them (usually) as private Bys in my page object. Sometimes when I need a dynamic locator (like if I were using nth-child or something, which I don't use anymore since it won't work with IE8), I might store the locator as a String with the prefix or suffix of the type of locator it represents (css, xpath, etc). Than in my method that uses that locator, I can easily modify it to include the integer for nth-child.

Hope this helps! And this is just my way of doing it, not necessarily the best.
Reply all
Reply to author
Forward
0 new messages